0

What I'm trying to do is pull specific data from Google Geocoding results in XML form. Eventually what I'm aiming for is a space where the user can enter their zip/postal code and the website will automatically pull their city/state/country/and coords, but right now I'm just testing to see if I can get it to work.

Here's the code I have so far:

$postcode = 11212; // zip code that will be looked up. Eventually I'll have a user-input the zip code, but for testing purposes this will suffice
$request_url = "http://maps.googleapis.com/maps/api/geocode/xml?address=".$postcode."&sensor=false"; 
$xml = simplexml_load_file($request_url) or die("url not loading");
$status = $xml->status;// GET the request status as google's api can return several responses
if ($status=="OK") {
    foreach($xml->result->address_component as $address_component){
        if(in_array("country", $address_component->type)){
            $country = $address_component->long_name;
            continue;
        } elseif(in_array('locality', $address_component->type)){
            $city = $address_component->long_name;
        }
    }
}
echo "$country";
echo "$city";

For those who haven't seen the results of the Geocode XML, here's what it looks like:

https://maps.googleapis.com/maps/api/geocode/xml?address=11212&sensor=false

I've been stuck here for ages and I feel like I'm going insane trying to figure out what I'm doing wrong. I know it's probably something simple, or I'm just making a silly mistake haha, but I figured it was time to ask for help.

So what am I doing wrong here? Thanks for the help, everyone :)

5
  • The secret to figure this out is .... to look at warnings and errors! After that you should be able to solve the issue. Here's one hint: you are not using in_array() properly, take a look at warning to find out why :) Commented Apr 5, 2014 at 20:07
  • Ok, here's more hint, you can easliy typecast an object to array, it's upto you to find out how ;) Commented Apr 5, 2014 at 20:10
  • @lihsus thanks for the reply :) I think that's one of the biggest frustrations I've been experiencing - there are no errors or warnings that I can see! When I upload the code to my website it all loads fine, but I just don't get the results that I'm expecting :/ Commented Apr 5, 2014 at 20:20
  • possible duplicate of simplexml_load_file error in PHP 5.3 Commented Apr 6, 2014 at 0:49
  • @user2631583, ahh okay probably you need to edit your php.ini then. Anyway the error I see is that you are passing $address_component->type as an argument to in_array(), you need to typecast it to array using (array) $address_component->type. Then it should work as expected. But I strongly recommend to configure your php.ini to show errors and warnings. Goodluck! Commented Apr 6, 2014 at 5:21

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.