解析Google Images API Json PHP

Hey, well I'm trying to use google images api with PHP, and I'm really not sure what to do.

This is basically what I have right now:

$jsonurl = "https://ajax.googleapis.com/ajax/services/search/images?v=1.0&q=test";
$json = file_get_contents($jsonurl,0,null,null);
$json_output = json_decode($json);

Where would I go from there to retrieve the first image url?

With a minor change to the last line of your code sample, the following will output the url of the first image in the result set.

<?php
$jsrc = "https://ajax.googleapis.com/ajax/services/search/images?v=1.0&q=test";
$json = file_get_contents($jsrc);
$jset = json_decode($json, true);
echo $jset["responseData"]["results"][0]["url"];
?>

For security reasons, most server configurations won't let you use file_get_contents on a remote file (different domain name). It would potentially allow a hacker to load code from anywhere on the Internet to your site, then execute it.

Even if your server configuration does allow for it, then I wouldn't recommend using it for this purpose. The standard tool for retrieving remote HTTP data is cURL, and there are plenty of good tutorials out there doing exactly what you should do in this case.

So, let's say you've successfully used cURL to retrieve the JSON array.

$json_output = json_decode($json); // Now the JSON is an associative array

foreach ($json_output['responseData']['results'] as $result)
{
    echo $result['url'] . '<br />';
}

Of course, you don't have to echo the URL there; you can do whatever you need to with the value.

I have to say, this is 10 shades of awesome.. But I come with bad news (don't shoot the messenger..)

Important: The Google Image Search API has been officially deprecated as of May 26, 2011. It will continue to work as per our deprecation policy, but the number of requests you may make per day may be limited.

That is, as they same, lame.
I feel as if Google might have hired one-too-many layed-off-from-IBM-types... as they seem to be killing off all their "cool" API's.

They launch services haphazardly, promising this and that and the other thing... but then some middle-manager gets screamed at after realizing (ta-da!) that XYZ project doesn't generate income (like image results without ads, lol) and then... they axe it..

Lesson: Don't get married (aka build your software or service) around any google API you can't replace on-the-fly at a moment's notice... Now, I'm no LTS-junkie - I'm just bitter because I'd much rather get my Google results via XML or JSON than the icky HTML-soup they throw at you...

One Question @Marcel... How can I get an array, or at least multiple JSON result responses using that same "formula". I thought maybe the 1 meant "result 1", but alas, no... Is their a "trick" to generate a content stream ala a Picasa xml feed?