Jump to content

Parsing XML data from a website using variables


dullnev

Recommended Posts

So I wrote this code for a friends website maybe 2 years ago, and it was working perfectly until about March of this year. trying to trace through it and I'm almost certain it has to do with retrieving the data from the URL. the script uses Google geocode API to get city information from a zip code which is usually pulled from a database of users, for now I have a hardcoded zip code to make it simpler.

 

here is what the URL should be retrieving - http://maps.googleapis.com/maps/api/geocode/json?address=32905&sensor=false

 

here's the code(the sleep is because it may need to do multiple queries at once depending on what data is being retrieved)

<?php
$errorMsg = "";
$zip = "32905";

//geolocation-------------------------------

function getCty($zip){
usleep(300000);
$url = "http://maps.googleapis.com/maps/api/geocode/json?address=".urlencode($zip)."&sensor=false";
$result_string = file_get_contents($url);
$result = json_decode($result_string, true);
$result1[]=$result['results'][0];
$result2[]= $result1[0]['formatted_address'];
return $result2[0];
}

$u_cty = getCty($zip);
$locArray = explode($zip, $u_cty);
$u_city = $locArray[0];

echo $u_city;

?>



Normally I would just google and find a working code snippet but this was already working for so long with no issues so I'm stumped.

 

Any insight is greatly appreciated.

Link to comment
Share on other sites

it does not retrieve any results, or at least it does not let me see them, I have tried echo'ing each variable after file_get_contents($url); 

 

My friend does not know PHP so I know he hasn't modified anything and I have left the website alone since I created it since we did a pretty thorough test of all the features. When I look through the database any users created since March of this year have been given blank data in the rows for city 

 

I'm not a professional developer I do it for fun for myself and friends so maybe I'm missing something or just not troubleshooting correctly

Link to comment
Share on other sites

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.