...

View Full Version : Handle HTTP Request and XML Response



Arnaud
03-18-2010, 04:40 PM
Hi Folks,

I would like to send an HTTP request to another host, and handle the XML data that it returns.

I am using the following website that provides geo-localisation data in XML format.

For example:

http://ws.geonames.org/findNearbyPlaceName?lat=48.8566667&lng=2.3509871&radius=5&maxRows=3

This returns:



<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<geonames>
<geoname>
<name>Paris 04 Hôtel-de-Ville</name>
<lat>48.8601</lat>
<lng>2.3507</lng>
<geonameId>3013131</geonameId>
<countryCode>FR</countryCode>
<countryName>France</countryName>
<fcl>P</fcl>

<fcode>PPLX</fcode>
<distance>0.3823</distance>
</geoname>
<geoname>
<name>Paris</name>
<lat>48.85341</lat>
<lng>2.3488</lng>
<geonameId>2988507</geonameId>
<countryCode>FR</countryCode>
<countryName>France</countryName>

<fcl>P</fcl>
<fcode>PPLC</fcode>
<distance>0.3959</distance>
</geoname>
<geoname>
<name>Les Halles</name>
<lat>48.8620115326636</lat>
<lng>2.34694361686707</lng>
<geonameId>7117438</geonameId>
<countryCode>FR</countryCode>

<countryName>France</countryName>
<fcl>P</fcl>
<fcode>PPLX</fcode>
<distance>0.6638</distance>
</geoname>
</geonames>


How would I send the request using a PHP script and display or save that XML data on my web host?

Thanks in advance for any help on that!

angst
03-18-2010, 04:49 PM
send the request:



function SendRequest($Request){
// get file
$ch = curl_init("http://SomeSite.com?xml=" . $Request );
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); // forces response into return string, not echo
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, Array("User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.15) Gecko/20080623 Firefox/2.0.0.15") );
return curl_exec($ch);
curl_close($ch);
}


handle response;


function HandleReponse($Response){
// read response
if($Response <> ""){
$xml = new SimpleXMLElement($Response);

// the rest will depent on the returned xml
}
}

Arnaud
03-18-2010, 04:50 PM
What a quick answer! Thanks a lot, I will try that asap!

Best,
Arnaud

Arnaud
03-19-2010, 11:57 AM
Okay, I don't understand the second part. Please let me know if I understood correctly.

In the first function, $Request should contain the arguments I will pass to the URL.

Now I don't understand how to work with the HandleResponse function.

// the rest will depent on the returned xml

what does that mean? could you give a full example based on the link I posted in my first post?

Thanks in advance. I have no experience at all with that.
Arnaud

Arnaud
03-19-2010, 04:06 PM
Anyone?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum