I did some work with the Yahoo! API recently to process CSV files of addresses to find longitude and latitude. The final code takes a CSV file, processes it, and then returns a CSV file with longitude and latitude fields added.
The code below includes the “pollYahoo” function, that takes an address and returns a string from Yahoo! – It’s part of a class so, references “$this” in places where other sections may have set values.
function pollYahoo($address){
// grab in PHP serialized format
$url = "http://where.yahooapis.com/geocode?q=" . $address . "&appid=" . $this->app_id . "&flags=CP";
$ch = curl_init();
$timeout = 5;
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
// fire request at yahoo
$latlongstring = $this->pollYahoo(urlencode($request));
$return = unserialize($latlongstring);
$longlat = $return['ResultSet']['Result'][0];
$newdata .= implode(',', $data) . ',' . $longlat['longitude'] . ',' . $longlat['latitude'] . "\n";
Image Credit: Flickmor
Comment or tweet @douglasradburn