Domanda

Codice non di lavoro:
Avviso: cercando di ottenere proprietà di non oggetto

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

Errore fatale: non è possibile utilizzare l'oggetto di tipo stdclass come array in

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


Risultati del codice di lavoro:
Come posso imporre solo URL e trasformare l'elenco URL in un array?

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
...........................................................................

Codice di lavoro:

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

Ho provato più varianti di quanto sopra, ma non lo sto capendo. Probabilmente qualcosa di semplice, quindi qualsiasi commento sarebbe apprezzato.

È stato utile?

Soluzione

Sembra che questo dovrebbe funzionare

$urls = array_map(function($result) {
    return $result->url;
}, $rez->responseData->results);
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top