Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    Jan 2008
    Location
    Geneva, Switzerland
    Posts
    413
    Thanks
    12
    Thanked 29 Times in 29 Posts

    Handle HTTP Request and XML Response

    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/findNearbyPla...us=5&maxRows=3

    This returns:

    PHP Code:
    <?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!
    Chuck Norris counted to infinity.
    Twice.

  • #2
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    send the request:

    PHP Code:
        function SendRequest($Request){
            
    // get file
            
    $ch curl_init("http://SomeSite.com?xml=" $Request );
            
    curl_setopt($chCURLOPT_RETURNTRANSFER,1); // forces response into return string, not echo
            
    curl_setopt($chCURLOPT_HEADER0);
            
    curl_setopt($chCURLOPT_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;
    PHP Code:
        function HandleReponse($Response){
            
    // read response
            
    if($Response <> ""){
                
    $xml = new SimpleXMLElement($Response);
                
                
    // the rest will depent on the returned xml
            
    }
        } 

  • #3
    Regular Coder
    Join Date
    Jan 2008
    Location
    Geneva, Switzerland
    Posts
    413
    Thanks
    12
    Thanked 29 Times in 29 Posts
    What a quick answer! Thanks a lot, I will try that asap!

    Best,
    Arnaud
    Chuck Norris counted to infinity.
    Twice.

  • #4
    Regular Coder
    Join Date
    Jan 2008
    Location
    Geneva, Switzerland
    Posts
    413
    Thanks
    12
    Thanked 29 Times in 29 Posts
    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
    Chuck Norris counted to infinity.
    Twice.

  • #5
    Regular Coder
    Join Date
    Jan 2008
    Location
    Geneva, Switzerland
    Posts
    413
    Thanks
    12
    Thanked 29 Times in 29 Posts
    Anyone?
    Chuck Norris counted to infinity.
    Twice.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •