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.

Edited by dullnev
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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.