1
votes

I am using Youtube data api v3 to retrieve data for search query using the below url https://www.googleapis.com/youtube/v3/search?part=snippet&q=text&key=apikey&maxResults=25.

I am getting json response, while i am using json decode to parse the json data, i am getting empty result, can any one tell me how to retrieve the data

I am using below php code to parse

$videourl="https://www.googleapis.com/youtube/v3/search?part=snippet&q=hello&key=apikey&maxResults=25";
            $json = file_get_contents($videourl);
            $data = json_decode($json,true);
print_r($data);

obtaining the json response from the below url: https://www.googleapis.com/youtube/v3/search?part=snippet&q=hello&key=apikey&maxResults=25

2
What is the value of the returned JSON from Google? - BenM
I am getting title,description,image details in json dictionary format - lalith458
Please post the returned JSON and your PHP code. - BenM
check my modified question - lalith458
what do you mean by empty result? what exactly is the output? - basic_space

2 Answers

0
votes
if (extension_loaded('curl')) {
    # create a new cURL resource
    $ch = curl_init();

    # set URL and other appropriate options
    curl_setopt($ch, CURLOPT_URL, "http://googleapis.com/.....");
    curl_setopt($ch, CURLOPT_HEADER, 0);
    # Setting cURL's option to return the webpage data
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);

    # grab URL and pass it to the browser
    if($json = curl_exec($ch)) {
        if(!($data = @json_decode($json)) instanceof stdClass) {
            trigger_error('Unable to decode json. '. print_r(error_get_last(), true));
        }
    }

    else trigger_error('CUrl Error:'.curl_error($ch));

    # close cURL resource, and free up system resources
    curl_close($ch);

}

else trigger_error('CUrl unsupported', E_USER_WARNING);
-1
votes

You should test the return value of file_get_contents. Be careful that this function will return FALSE in case of error (and not NULL). So you should test the return value with === FALSE in order to not be confused by empty values.

In your case, if you are trying to access the exact url that you posted in your question, google is returning a code 400 (bad request), this is why you get a return of FALSE.