1

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?

3
  • You have fetchApi(''); in your file so it always calls itself additionally. Commented Mar 29, 2020 at 17:58
  • @user3783243 what can I do if I don't want to run algorithms inside first if statement from another php files? I have no ideas what to do if fetchApi(' ') runs too when I include the file.. Commented Mar 29, 2020 at 18:06
  • 1
    @user3783243 thank you for great comment.... your first comment helped me to solve my problem. Thank you. I placed fetchApi(' ') inside if(empty($_GET)) and it works great!! Commented Mar 29, 2020 at 18:20

1 Answer 1

1

Answering to my question myself. It had nothing to do with if(empty(....)) The reason why I got that error was

fetchApi('');

was always being called when I include fetch.php Thank you user3783243 for your comment.

And I solved this problem so

if(empty($_GET)){    
    fetchApi('');
}

fetchApi('') will only run when user hasn't made a GET request.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.