1

Here is a URL→ https://ajax.googleapis.com/ajax/services/search/images?v=1.0&q=snoopy&rsz=1

and this is my php code

<?php
    $url = "https://ajax.googleapis.com/ajax/services/search/images?"."v=1.0&q=snoopy";

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $body = curl_exec($ch);
    curl_close($ch);

    $json = json_decode($body);
?>

I wanna echo all of the image urls from its result
(For example: http://img2.wikia.nocookie.net/__cb20110331075248/peanuts/images/6/62/Snoopy.gif )

But I have no idea how to echo them.
Hope someone could help me, thanks!

1
  • if the link comes as a response then just echo $body :-? Commented Nov 13, 2014 at 13:28

2 Answers 2

2

Maybe like this:

<?php
    $url = "https://ajax.googleapis.com/ajax/services/search/images?" .
   "v=1.0&q=barack%20obama&userip=INSERT-USER-IP";

   // sendRequest
   // note how referer is set manually
   $ch = curl_init();
   curl_setopt($ch, CURLOPT_URL, $url);
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
   curl_setopt($ch, CURLOPT_REFERER, 'http://example.com');
   $body = curl_exec($ch);
   curl_close($ch);

   // now, process the JSON string
    $pics = json_decode($body);
    $ret='';
    if($pics){
       foreach($pics->responseData->results as $pic)
          $ret .= $pic->url.'<br>';
    }
    echo $ret;
?>
Sign up to request clarification or add additional context in comments.

17 Comments

Fatal error: Cannot use object of type stdClass as array on line ... this is what I get after using your code ;)
Why -1? i dont know what the api gives you back.
You would if you would actually access the URL that the OP provided ;)
just for future reference: {"responseData": {"results":[{"GsearchResultClass":"GimageSearch","width":"608","height":"766"...
Much better :) sorry to be such a pain but I really like PHP when it's done right :)
|
1

in order to echo the exact result just use:

echo $body; //echoes json string

if you want to echo the exact link, echo like this:

echo $json->responseData->results[0]->url; //echoes http://img2.wikia.nocookie.net/__cb20110331075248/peanuts/images/6/62/Snoopy.gif

or you can foreach() twice (or once...) then echo $res->url directly :) your choise!

7 Comments

I updated my answer with the correct format to output that URL. Please take a look now :) and tell me if you need anything more ;)
excuse me. when I echo $body, there is nothing to occur. I also try this code "echo $json->responseData->results[0]->url;" but happened error "Trying to get property of non-object"
in your case, $body should contain the json string. Try to var_dump() it and tell me what it outputs ;)
this could be because your $body does not contain that exact json string... it might be different... just var_dump() it and then tell me what you get ;)
You have forgotten the CURLOPT_REFERER look at my answer i have edited it
|

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.