0

Hello all

I am attempting to use PHP programming to execute a scripts to this link... http://ws.geonames.org/postalCodeSearch?postalcode=VARIABLE_ZIP&country=US. The VARIABLE_ZIP is the actual zip code entered into the form that will submit the information in the link above. The output of that link creates an XML page that i do not want displayed anywhere on my website.

What I want to do is capture the XML data Latitude and Longitude values as variables within php and store them into a database.

1) I have a form
2) The user inputs their zip code into the form
3) upon submitting the form: I want code to capture the XML data generated by the link with the zip code variable added: http://ws.geonames.org/postalCodeSearch?postalcode=90210&country=US (I don't want this page to display anywhere, I just want to capture the data)
4) I want to take those variables back to PHP and store them in my database (I know how to do this)

*Step 3 is an unknown process for me.

Can someone help me understand how to do this?*

3
Contributors
7
Replies
10
Views
5 Years
Discussion Span
Last Post by diafol
0

SOmething as easy as this:

assume you do this on your site:

www.example.com/postal.php

you have a form with inputs names as postalcode and country. You then pick up the form variables thus:

<?php
$pc = $_POST['postalcode'];
$c = $_POST['country'];

$xml = file_get_contents("http://ws.geonames.org/postalCodeSearch?postalcode={$pc}&country={$c}");
file_put_contents("geopost_{$pc}_{$c}.xml",$xml);

?>

I know some hosts don't like users using file_get_contents on remote files, so they disable it. You'll have to check your phpinfo() to see if that is the case. If so, you may need to use cURL to extract the info.

BTW - I've not included any data checks/validation etc. I've just put this into a file (put), but you can do what you want with it once it's in the $xml variable.

Edited by diafol

0

That helps me greatly. I wasn't sure how to call xml into php.
What happens when I run the xml is the link i provided above generates an XML page... I need to capture the Lat and Long values that the page generates... and bring them back into php as variables.

Can you look at link above with real zipcode in it?
I need to know how to capture lat and long as values for my databaes.

0

That helps me greatly. I wasn't sure how to call xml into php.
What happens when I run the xml is the link i provided above generates an XML page...
I need to capture the Lat and Long values that the page generates... and bring them back into php as variables.

When the link of XML is clicked on it generates this page data:

    <geonames>
        <totalResultsCount>1</totalResultsCount>
            <code>
                <postalcode>90210</postalcode>
                <name>Beverly Hills</name>
                <countryCode>US</countryCode>
                <lat>34.09011</lat>
                <lng>-118.40648</lng>
                <adminCode1>CA</adminCode1>            
                <adminName1>California</adminName1>
                <adminCode2>037</adminCode2>
                <adminName2>Los Angeles</adminName2>
                <adminCode3/><adminName3/>
             </code>
    </geonames>

I need to know how to capture lat and long as values for my databaes but back in PHP.

0

You have a choice, either do a php substr or a preg_match. OR even use simple_load_xml().
As I've already got all the data into a $var, I'd just use preg_match():

1

Hi,

You should go by Ardav's suggestion.. (I mean diafol :) ). That should get the job done, but if want to do some simple xml parsing, then you can try codes below. Not the best one, but can also get the job done ( I hope :))..

Here we go... simple DOM object..

## don't forget to provide the location of the actual xml file.
$xml = "someXml.xml";
$doc = new DOMDocument();
  $doc->load( $xml);
  ## lets read the code block
  $records = $doc->getElementsByTagName( "code" );
  foreach( $records as $record )
  {
  ## parse the zip code
  $zip = $record->getElementsByTagName( "postalcode" );
  $zip = $zip->item(0)->nodeValue;

  ## parse the city
  $city = $record->getElementsByTagName( "name" );
  $city = $city->item(0)->nodeValue;

  ## parse the latitude
  $lat = $record->getElementsByTagName( "lat" );
  $lat = $lat->item(0)->nodeValue;

  ## parse longtitude.
  $longtitude = $record->getElementsByTagname( "lng" );
  $longtitude = $longtitude->item(0)->nodeValue;

  echo "<p>Zip Code: ".$zip."<br/>City: ".$city."<br/>Latitude: ".$lat."<br/>Lontitude:".$longtitude."</p>";
  }

that's pretty much about it...

Edited by veedeoo: more info added

Votes + Comments
good example
0

Thank you both for answering... this is all new and confusing to me.
My mother just passed away and my brain isn't at 100% yet and it wasn't that good with php to begin with.

I have used preg_match for data input validation.

Can you show me quickly how to grab the Name(city), Lat, Lng out of the $xml variable?

I am sorry to bother you and i greatly appreciate the assistance.

Also you mentioned...

If so, you may need to use cURL to extract the info:

Can you show me how to code it in cURL in case i do run into that problem?
I will be switching hosting domain companies soon and may run into that problem!

0

The DomDOcument would be the way to go (veedeoo). You have a number of options for parsing xml data - DD is one of the easiest.
IF you really want preg_match:

$str = "<lat>-10.315</lat>\n<long>-14.375</long>\n<city>Cardiff</city>";

$tags = array('lat','long','city');
foreach($tags as $tag){
    preg_match($patterns = "/<$tag>(.+)<\/$tag>/",$str,$matches);
    $$tag = $matches[0];
}
echo $lat . "<br />";
echo $long . "<br />";
echo $city . "<br />";

Dunno if it'll work for you, it's late and I'm going to bed.

Can you show me how to code it in cURL in case i do run into that problem?

No, I'm not going to do that unless you really need it. There are plenty of online tutorials you can peruse to find that info.

Edited by diafol

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.