douzangdang2225 2013-04-30 10:50
浏览 41
已采纳

如何从URL获取动态XML数据

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

  • 写回答

1条回答 默认 最新

  • dsjbest2014 2013-04-30 10:58
    关注

    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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?