如何在PHP中回显cURL结果?
这是一个网址→ 这是我的php代码如何在PHP中回显cURL结果?,php,json,curl,Php,Json,Curl,这是一个网址→ 这是我的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
<?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);
?>
我想从结果中回显所有图像URL(例如:) 但我不知道如何回应它们。
希望有人能帮助我,谢谢 为了得到准确的结果,只需使用:
echo $body; //echoes json string
如果要回显确切的链接,请如下回显:
echo $json->responseData->results[0]->url; //echoes http://img2.wikia.nocookie.net/__cb20110331075248/peanuts/images/6/62/Snoopy.gif
或者你可以foreach()
两次(或一次…),然后echo$res->url
直接:)你的选择 可能是这样的:
<?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;
?>
如果链接是响应,那么只需回显$body
:-?我用正确的格式更新了我的答案以输出该URL。现在请看一下:)如果您还需要什么,请告诉我;)打扰一下当我回显$body时,不会发生任何事情。我也尝试了这段代码“echo$json->responseData->results[0]->url”;但在您的例子中,“尝试获取非对象的属性”发生了错误,$body
应该包含json字符串。尝试var\u dump()
it并告诉我它的输出;)这可能是因为$body不包含确切的json字符串。。。这可能是不同的。。。只要把它倒出来,然后告诉我你得到了什么;)你已经忘记了卷发器请看我的答案我已经编辑了它致命错误:无法在线使用stdClass类型的对象作为数组…
这是我使用你的代码后得到的;)为什么是-1?我不知道api给了你什么。如果你真的要访问OP提供的URL,你会这么做的;)仅供将来参考:{“responseData”:{“results”:[{“GsearchResultClass”:“GimageSearch”,“width”:“608”,“height”:“766”
…好多了:)很抱歉这么痛苦,但如果PHP做得好,我真的很喜欢:)