如何在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;
?>