I need to get all near by postalcodes with geonames api. I am using the curl for api response.Right now, sometimes I have getting the response from api and sometimes getting the Curl error: name lookup timed outArray.
function getResponse()
{
$xml = get_data('http://api.geonames.org/findNearbyPostalCodes?postalcode=73301&maxRows=100&username=demo');
$all_zip_codes = array_keys($xml);
print_r($all_zip_codes);}
/*Function for getting response from api */
function get_data($url) {
if(!function_exists('curl_init')) {
die ("Curl PHP package not installed\n");
}
$ch=curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($ch);
if(!curl_errno($ch))
{
$info = curl_getinfo($ch);
echo '<br/> Took ' . $info['total_time'] . ' seconds to send a request to ' . $info['url'];
}
else
{
echo 'Curl error: ' . curl_error($ch);
}
$refineArray = array();
$parseArray = json_decode(json_encode((array)simplexml_load_string($response)),1);
foreach($parseArray['code'] as $value) {
$refineArray[$value['postalcode']] = $value;
}
return($refineArray);
}