Parsing JSON Results with PHP - Yahoo Search API -
i able retrieve results yahoo api key, using instructions found on yahoo developers website. http://developer.yahoo.com/boss/search/boss_api_guide/codeexamples.html#
code:
if ($_post['query']) { $newline="<br />"; $query = urlencode("'{$_post['query']}'"); require("oauth.php"); $cc_key = "key goes here"; $cc_secret = "secret goes here"; $url = "http://yboss.yahooapis.com/ysearch/web"; $args = array(); $args["q"] = "$query"; $args["format"] = "json"; $consumer = new oauthconsumer($cc_key, $cc_secret); $request = oauthrequest::from_consumer_and_token($consumer, null,"get", $url, $args); $request->sign_request(new oauthsignaturemethod_hmac_sha1(), $consumer, null); $url = sprintf("%s?%s", $url, oauthutil::build_http_query($args)); $ch = curl_init(); $headers = array($request->to_header()); curl_setopt($ch, curlopt_httpheader, $headers); curl_setopt($ch, curlopt_url, $url); curl_setopt($ch, curlopt_returntransfer,1); $rsp = curl_exec($ch); $results = json_decode($rsp); print_r($results); }
using print_r($results) shown above, results, such following (extract of first 3 results shown searching "elephant"):
please note have changed urls "www" require @ least 10 reputation post more 2 links.
stdclass object ( [bossresponse] => stdclass object ( [responsecode] => 200 [web] => stdclass object ( [start] => 0 [count] => 50 [totalresults] => 36800000 [results] => array ( [0] => stdclass object ( [date] => [clickurl] => www [url] => www [dispurl] => en.wikipedia.org/wiki/elephant [title] => elephant - wikipedia, free encyclopedia [abstract] => elephant trunks have multiple functions, including breathing, olfaction, ... 1 elephant has been observed graze kneeling on front legs, ... ) [1] => stdclass object ( [date] => [clickurl] => www [url] => www [dispurl] => www.defenders.org/elephant/basic-facts [title] => elephant | basic facts elephants | defenders of wildlife [abstract] => elephant. basic facts elephants more on elephant: threats elephants » more on elephant: basic facts . threats. defenders doing help. can ... ) [2] => stdclass object ( [date] => [clickurl] => www [url] => www [dispurl] => kids.nationalgeographic.com/.../african-elephant [title] => african elephant facts , pictures -- national geographic kids [abstract] => kids' feature elephants, photographs, video, audio, fun facts, e-mail postcard, , links other animals. ) [3] => stdclass object ( [date] => [clickurl] => www [url] => www [dispurl] => elephant.elehost.com/about_elephants/about_elephants.htm [title] => elephants [abstract] => elephants on elephant information repository! page includes summary of elephant related facts inducted in world of elephants. )
i have attempted output results, in legible format, follows:
code attempt 1:
foreach ($results->{ 'results' } $item ) { echo "<a href=\"{$item->{ 'url' }}\"><font color ='blue'>{$item->{ 'title' }}</font></a>".": "."$newline"."$newline".$item->{ 'abstract' }."\n\n"; }
i tried following, without success:
code attempt 2:
echo $results['results']['url']; echo $results['results']['title']; echo $results['results']['abstract'];
any ideas on do?
thanks.
i've noticed copy-pasted code the documentation's code examples, never mind that.
you're accessing results
array wrong way:
foreach ($results->bossresponse->web->results $result) { //do stuff echo $result->title.'<br/>'; }
or, cptnk suggested:
$results = json_decode($rsp, true); //force assoc-array, allow array-access foreach($results['bossresponse']['web']['results'] $result) { //$result array here, same stuff echo $result['title'].'<br/>'; }
or, combine two
foreach($results->bossresponse->web->results $result) { $result = (array) $result;//casts stdclass array printf('<a href="%s">%s</a><br/>', $result['url'], $result['title']); }
Comments
Post a Comment