Search code examples
phpcurlyahoo-api

Extract yahoo api data - php?


{ "query" : { "count" : 2,
  "created" : "2014-03-08T10:16:51Z",
  "lang" : "en-US",
  "results" : { "place" : [ { "admin1" : { "code" : "IN-DL",
                "content" : "Delhi",
                "type" : "Territory",
                "woeid" : "20070458"
              },
            "admin2" : { "code" : "",
                "content" : "New Delhi",
                "type" : "District",
                "woeid" : "28743736"
              },
            "admin3" : null,
            "areaRank" : "4",
            "boundingBox" : { "northEast" : { "latitude" : "28.645321",
                    "longitude" : "77.240784"
                  },
                "southWest" : { "latitude" : "28.569349",
                    "longitude" : "77.166237"
                  }
              },
            "centroid" : { "latitude" : "28.607330",
                "longitude" : "77.203506"
              },
            "country" : { "code" : "IN",
                "content" : "India",
                "type" : "Country",
                "woeid" : "23424848"
              },
            "lang" : "en-US",
            "locality1" : { "content" : "Delhi",
                "type" : "Town",
                "woeid" : "2295019"
              },
            "locality2" : null,
            "name" : "New Delhi",
            "placeTypeName" : { "code" : "9",
                "content" : "District"
              },
            "popRank" : "0",
            "postal" : null,
            "timezone" : { "content" : "Asia/Kolkata",
                "type" : "Time Zone",
                "woeid" : "28350818"
              },
            "uri" : "http://where.yahooapis.com/v1/place/28743736",
            "woeid" : "28743736"
          },
          { "admin1" : { "code" : "US-IL",
                "content" : "Illinois",
                "type" : "State",
                "woeid" : "2347572"
              },
            "admin2" : { "code" : "",
                "content" : "Jersey",
                "type" : "County",
                "woeid" : "12588119"
              },
            "admin3" : null,
            "areaRank" : "2",
            "boundingBox" : { "northEast" : { "latitude" : "39.043468",
                    "longitude" : "-90.245506"
                  },
                "southWest" : { "latitude" : "39.025280",
                    "longitude" : "-90.268921"
                  }
              },
            "centroid" : { "latitude" : "39.034382",
                "longitude" : "-90.257217"
              },
            "country" : { "code" : "US",
                "content" : "United States",
                "type" : "Country",
                "woeid" : "23424977"
              },
            "lang" : "en-US",
            "locality1" : { "content" : "New Delhi",
                "type" : "Town",
                "woeid" : "23510871"
              },
            "locality2" : null,
            "name" : "New Delhi",
            "placeTypeName" : { "code" : "7",
                "content" : "Town"
              },
            "popRank" : "1",
            "postal" : { "content" : "62052",
                "type" : "Zip Code",
                "woeid" : "12784947"
              },
            "timezone" : { "content" : "America/Chicago",
                "type" : "Time Zone",
                "woeid" : "56043661"
              },
            "uri" : "http://where.yahooapis.com/v1/place/23510871",
            "woeid" : "23510871"
          }
        ] }
} }

How do i get the centroid, locality , country etc via php?

 $json = curl_exec($ch); //RESULT  of YQL QUERY




  for($x=0;$x<1;$x++)
  {

         $lat = $json->results->place[$x]->centroid->latitude;
         $lng = $json->results->place[$x]->centroid->longitude; 


  }
  echo $lat;
  echo $lng;

Please help

When trying this code:

     $data = curl_exec($ch);
$json = json_decode($data, true);
// print_r($json);
if(is_array($json))
{
  foreach($json as $query){
    foreach($query['results']['place'] as $places){

        if( is_array($places['centroid']) ){ 
            echo "Centroid:\n";
            echo "Latitude: " . $places['centroid']['latitude'] . "\n";
            echo "longitude: " . $places['centroid']['longitude'] . "\n\n";
        }
        if( is_array($places['country']) ){ 
            echo "Country:\n";
            echo "Content: " . $places['country']['content'] . "\n";
            echo "Woeid: " . $places['country']['woeid'] . "\n\n";
        }
    }
  }
}

It doesn't go inside the if(is_array($json)) { loop.


Solution

  • consider this example http://codepad.org/KbcRcAZd

    Try like this:

      $json = json_decode($data, true);
      //print_r($json);
    
      foreach($json as $query){
        foreach($query['results']['place'] as $places){
            echo "Latitude: " . $places['centroid']['latitude'] . "\n";
            echo "longitude: " . $places['centroid']['longitude'] . "\n\n";
        }
      }