Search code examples
phpapiyelp

Experimenting with Yelp API PHP


Getting the Error Message - Notice: Trying to get property of non-object in I'm trying to experiment/learn with the YELP API but I'm getting stuck with a simple error message, I can't seem to figure out. Following this: https://github.com/Yelp/yelp-api/tree/master/v2/php

function query_api($term, $location) {     
$response = json_decode(search($term, $location));
$business_id = $response->businesses[0]->id;

print sprintf(
    "%d businesses found, querying business info for the top result \"%s\"\n\n",         
    count($response->businesses),
    $business_id
);

$response = get_business($business_id);

print sprintf("Result for business \"%s\" found:\n", $business_id);
print "$response\n";

}

Calling the function

$longopts  = array(
"term::",
"location::",
);

$options = getopt("", $longopts);

$term = $options['term'] ?: '';
$location = $options['location'] ?: '';

query_api($term, $location);   

Solution

  • This notice indicates that $response is not an object and you are trying to access a property businesses that doesn't exist.
    Use var_dump($response) to get information about this variable.