如何从URL获取动态XML数据

时间:2013-04-30 10:50:39

标签: php xml

我正在研究PHP和XML。我将一些值origin,Destination,Journeydate,Returndate发送到特定的URL。因此,根据我的值,一些XML数据正在该URL中生成。现在我想获取生成的结果(XML数据)并在我的页面上打印。

我已尝试使用以下代码打印网址数据

$url="your url";

$xmlinfo = simplexml_load_file($url);

print_r($xmlinfo);

它不起作用。

我认为它适用于包含静态XML数据的URL。

但是我想在网址上发送一些值。结果将生成,然后我想获得生成的结果。

以下是我将值发送到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>

先谢谢, 了Shoba

1 个答案:

答案 0 :(得分:0)

尝试

$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);