I have been working on this problem for several hours but I couldn't figure out why empty() in if statement doesn't work as I expected.
fetch.php
header('Content-Type: application/json');
function fetchApi($param1){
$apiKey="SOME TEXT WHICH I WILL NOT POST HERE";
$curl = curl_init();
if(empty($param1)){
$body = json_decode(file_get_contents('php://input'), true);
$bodyObject = (object) $body;
if($bodyObject->target=="main"){
$bodyObject->link = $bodyObject->link . "random?number=4".$apiKey;
curl_setopt($curl, CURLOPT_URL, $bodyObject->link);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($curl);
echo $output;
}
}else{
echo $param1;
}
curl_close($curl);
}
fetchApi('');
this is my fetch.php code, I need to run fetchApi in two different situations. The first situation is when index.html is loaded. Javascript will automatically fetch to fetch.php and runs function fetchApi with empty string as parameter. Have tested hundreds time and it works fine.
But the problem is, I need to run this function in search.php too, when user searches something.
form in index.html
<form action="search.php" method="GET">
<label for="check">ingradients</label><input id="check"type="checkbox"/>
<input type="text" name="search" placeholder="SEARCH"/>
</form>
search.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<?php
$searchId = $_GET["search"];
include ('fetch.php');
fetchApi($searchId);
?>
</body>
</html>
as you can see, in search.php I include fetch.php and calls function fetchApi with argument $_GET["search"] , but I get this error when I search something.
Notice: Undefined property: stdClass::$target in C:\xampp\htdocs\spoonacular\fetch.php on line 16
I can't understand why php shows such error. I passed an argument, it's not empty, why php still runs algorithms inside if(empty($param1)){}? I tried if($param1==''){} also but it doesn't work too.
How to do if I want totally ignore everything inside if(empty($param1)) if function parameter is not empty string or NULL?
Aucun commentaire:
Enregistrer un commentaire