Analyzing JSON Results with PHP - Yahoo Search API

I can get the results from yahoo using my API key using the instructions found on the 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) as shown above, I get results such as the following (extracting the first three results shown when searching for " elephant "):

PLEASE PAY ATTENTION I CHANGE THE "WWW" URLS AS I APPEAL THE LAST 10 REPUTS TO AFTER MORE THAN 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, the free encyclopedia [abstract] => Elephant fonts have multiple functions, including breathing, smelling, ... One elephant was observed to graze on its knees on its front legs, ...) [1] => stdClass Object ([date] => [clickurl] => WWW [url] => WWW [dispurl] => www.defenders.org/elephant/basic-facts [name] => Elephant | basic Facts about elephants | Wildlife Protectors [abstract] => Elephant. Basic fa kty about elephants Read more about elephants: threats to elephants "Read more about Elephant: basic facts. Threats. What advocates do to Help. What you can ...) [2] => stdClass Object ([date] => [clickurl] => WWW [url] => WWW [dispurl] => kids.nationalgeographic.com/.../african-elephant [name] => Facts about African elephants and drawings - National Geographic Kids [abstract] => Children's feature of elephants, photographs , video, audio, fun facts, postcard and links to other animals. ) [3] => stdClass Object ([date] => [clickurl] => WWW [url] => WWW [dispurl] => elephant.elehost.com/About_Elephants/about_elephants.htm [name] => About Elephants [ abstract] => All about elephants on the Elephant Information Repository! This page contains a summary of elephant-related facts so you can be introduced to the elephant world.)

I tried to output the results in a clear format as follows:

Code try 1:

 foreach ($results->{ 'results' } as $item ) { echo "<a href=\"{$item->{ 'url' }}\"><font color ='blue'>{$item->{ 'title' }}</font></a>".": "."$newline"."$newline".$item->{ 'abstract' }."\n\n"; } 

I also tried the following, without success:

Code try 2:

 echo $results['results']['url']; echo $results['results']['title']; echo $results['results']['abstract']; 

Any ideas on what to do?

Thanks.

+6
source share
1 answer

I noticed that you just copied the code from the sample documentation code , but ignore it.

You get the wrong path to the results array:

 foreach ($results->bossresponse->web->results as $result) { //do stuff echo $result->title.'<br/>'; } 

Or, as cptnk suggested:

 $results = json_decode($rsp, true); //force to assoc-array, which will allow array-access foreach($results['bossresponse']['web']['results'] as $result) { //$result is array here, but do the same stuff echo $result['title'].'<br/>'; } 

Or, combine the two

 foreach($results->bossresponse->web->results as $result) { $result = (array) $result;//casts stdClass to array printf('<a href="%s">%s</a><br/>', $result['url'], $result['title']); } 
+1
source

Source: https://habr.com/ru/post/948457/


All Articles