简体   繁体   中英

How to fetch dynamical XML data from a URL

I am working on PHP and XML. I am sending some values like origin,Destination,Journeydate,Returndate to a particular URL. As a result according to my Values some XML data is generating in that URL. Now i want to fetch that generated result(XML Data) and print on my page.

I have tried the below code for printing the data of a URL

$url="your url";

$xmlinfo = simplexml_load_file($url);

print_r($xmlinfo);

It is not working.

I think it will work for the URL's which will contain Static XML data.

But here i want to send some values to a url. There the result will generate and then i want to get that generated result.

Here is the code how i am sending the values to URL

    <HTML>
        <HEAD>
        <script language="Javascript">
        function submit_search()
        {       
            window.document.forms[0].action="URL";      
            window.document.forms[0].submit();
        }
        </script>
        </HEAD>
        <BODY>
        <form method="post">
            XML Request:    
            <input type="text" name="xmlRequest" id="fromcity" 
value="<AvailRequest>
     <Trip>ONE</Trip>
     <Origin>BOM</Origin>  
     <Destination>JFK</Destination>  
     <DepartDate>2013-05-01</DepartDate>  
     <ReturnDate>2013-05-05</ReturnDate>  
     <AdultPax>1</AdultPax>  
     <ChildPax>0</ChildPax>  
     <InfantPax>0</InfantPax>  
     <Currency>INR</Currency>  
     <PreferredClass>E</PreferredClass>  
     <Eticket>true</Eticket> 
    </AvailRequest>" />

            <input type="button" name="SUBMIT" value="submit" onClick="submit_search();"/>

        </form>
        </BODY>
        </HTML>

Thanks in Advance, Shoba

Try

$url = 'URL GOES HERE';

//You need to build the front-end to grab and build the xml
//eg: $input_xml = "<AvailRequest><Trip>" . $_POST['trip'] . "</Trip> ......";
$input_xml = "<AvailRequest>
    <Trip>ONE</Trip>
    <Origin>BOM</Origin>  
    <Destination>JFK</Destination>  
    <DepartDate>2013-05-01</DepartDate>  
    <ReturnDate>2013-05-05</ReturnDate>  
    <AdultPax>1</AdultPax>  
    <ChildPax>0</ChildPax>  
    <InfantPax>0</InfantPax>  
    <Currency>INR</Currency>  
    <PreferredClass>E</PreferredClass>  
    <Eticket>true</Eticket> 
</AvailRequest>";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POSTFIELDS,
            "xmlRequest=" . $input_xml);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$data = curl_exec($ch);
curl_close($ch);

//convert the XML result into array
$array_data = json_decode(json_encode(simplexml_load_string($data)), true);

print_r('<pre>');
print_r($array_data);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM