如何在PHP中回显cURL结果? [英] How to echo the cURL result in PHP?

查看:83
本文介绍了如何在PHP中回显cURL结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里是一个网址→ https://ajax.googleapis.com/ajax/services/search/images?v=1.0&q=snoopy&rsz=1

这是我的PHP代码

<?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);
?>

我想从结果中回显所有图像网址

(对于例如: http://img2.wikia.nocookie.net /__cb20110331075248/peanuts/images/6/62/Snoopy.gif

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!

推荐答案

也许像这样:

<?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;
?>

这篇关于如何在PHP中回显cURL结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆