非工作代码:
注意:尝试获取非对象的财产

$i = 1;
var_dump($rez->responseData->results[$i]->url);

致命错误:不能将类型stdclass的对象用作数组

$rez = (array) $rez;
var_dump($rez['responseData']['results']['1']['url']);


工作代码的结果:
如何仅扩展URL并将URL列表变成数组?

stdClass Object
(
[responseData] => stdClass Object
    (
        [results] => Array
            (
                [0] => stdClass Object
                    (
                        [GsearchResultClass] => GwebSearch
                        [unescapedUrl] => http://www.rottentomatoes.com/top/
                        [url] => http://www.rottentomatoes.com/top/
                        [visibleUrl] => www.rottentomatoes.com
...........................................................................

工作代码:

function google_search_api($args, $referer = 'http://localhost/test/', $endpoint = 'web'){
$url = "http://ajax.googleapis.com/ajax/services/search/".$endpoint;

if ( !array_key_exists('v', $args) )
    $args['v'] = '1.0';

$url .= '?'.http_build_query($args, '', '&');

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// note that the referer *must* be set
curl_setopt($ch, CURLOPT_REFERER, $referer);
$body = curl_exec($ch);
curl_close($ch);
//decode and return the response
return json_decode($body);
}

$rez = google_search_api(array(
    'q' => 'movie list', 'start' => '5',
 ));

echo '<pre>';
print_r($rez);
echo '</pre>';
?>

我尝试了上述多种变体,但只是没有弄清楚。可能很简单,因此任何评论都将不胜感激。

有帮助吗?

解决方案

好像这应该有效

$urls = array_map(function($result) {
    return $result->url;
}, $rez->responseData->results);
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top