Jump to content

How to use a foreach statement to loop through json search engine results?


the5thace

Recommended Posts

Sample query: http://www.faroo.com/api?q=elephants&start=1&length=10&l=en&src=web&f=json&jsoncallback=mycallback

 

I do not quite understand the parameters I need to use for a foreach statement to work on this query. 

 

Previously I have managed the foreach to loop through Blekko results sets with

 
 
foreach ($js->RESULT as $item)
         {  if(@$item->snippet || @$item->url_title)
         {$Blekko[$i] = "<a href=\"{$item->url}\"> 
         {$item->url_title}</a>blekko     
          <p>$item->snippet}</p>"; 

 

 but I'm not sure how it actually works. Bing query : https://api.datamarket.azure.com/Bing/Search/Web?$format=json&Query=%27bing%27

Whatever variable you're json_decode()ing to,

$obj = json_decode(/* whatever */);
$obj->results is the array of results
$obj->results[0] is the first one in that array
$obj->results[0]->title is the first title

I understand that but how do you know which variable offers the array of results? 

My current foreach does not work for the faroo query. Giving me an error on the foreach line.

foreach ($js->results as $item)
        {
            $Faroo[$i] = "<a href=\"{$item->url}\"> 
            {$item->title}</a>faroo  
            <p>$item->kwic}</p>";    
            $i++;

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.