本文介绍了使用 php curl 将 XML 数据发送到 webservice的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究 arzoo 的 Flight API.服务器必须在简单的 POST 请求中接收发布的数据.为了实现这一点,我正在使用 PHP cURL.在 API 文档中明确提到数据应按以下格式发送:

<Trip>ONE</Trip><Origin>BOM</Origin><目的地>纽约</目的地><出发日期>2013-09-15</出发日期><退货日期>2013-09-16</退货日期><AdultPax>1</AdultPax><ChildPax>0</ChildPax><InfantPax>0</InfantPax><货币>印度卢比</货币><Preferredclass>E</Preferredclass><Eticket>true</Eticket><Clientid>77752369</Clientid><Clientpassword>*AB424E52FB5ASD23YN63A099A7B747A9BAF61F8E</Clientpassword><Clienttype>ArzooINTLWS1.0</Clienttype><PreferredAirline></PreferredAirline></AvailRequest>

我把上面的代码放在一个变量 $xml 中.我的PHP cURL代码如下:

$URL = "http://59.162.33.102:9301/Avalability";//设置卷曲参数.$ch = curl_init();curl_setopt($ch, CURLOPT_URL,$URL);curl_setopt($ch, CURLOPT_VERBOSE, 1);curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);curl_setopt($ch, CURLOPT_POST, 1);curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/xml'));curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);如果 (curl_errno($ch)){//移动到显示页面以显示卷曲错误回声 curl_errno($ch) ;回声 curl_error($ch);}别的{//从服务器获取响应$response = curl_exec($ch);打印_r($响应);curl_close($ch);}

我没有得到任何回应.我已经和 API Provider 谈过同样的事情,但他们在他们的日志中发现了空请求.我是不是错过了什么?您的回复将不胜感激.谢谢.

解决方案

在使用 Arzoo International 航班 API 苦苦挣扎之后,我终于找到了解决方案,并且代码对我来说简直太棒了.以下是完整的工作代码:

//将您的 XML 请求存储在一个变量中$input_xml = '<Trip>ONE</Trip><Origin>BOM</Origin><目的地>肯尼迪</目的地><出发日期>2013-09-15</出发日期><退货日期>2013-09-16</退货日期><AdultPax>1</AdultPax><ChildPax>0</ChildPax><InfantPax>0</InfantPax><货币>印度卢比</货币><PreferredClass>E</PreferredClass><Eticket>true</Eticket><Clientid>777ClientID</Clientid><Clientpassword>*您的API密码</Clientpassword><Clienttype>ArzooINTLWS1.0</Clienttype><PreferredAirline></PreferredAirline></AvailRequest>';

现在我对上面的 curl_setopt 声明做了一些改动,如下所示:

 $url = "http://59.162.33.102:9301/Avalability";//设置卷曲参数.$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, 300);$data = curl_exec($ch);curl_close($ch);//将XML结果转换为数组$array_data = json_decode(json_encode(simplexml_load_string($data)), true);print_r('

');打印_r($array_data);print_r('</pre>');

代码对我来说绝对没问题.我真的很感激@hakre &@Lucas 感谢他们的大力支持.

I'm working on Flight API of arzoo. The server must receive the posted data in simple POST Request. To achieve this i'm using PHP cURL. In the API Document it is clearly mention that the data should be sent in the following format:

<AvailRequest>
        <Trip>ONE</Trip>
        <Origin>BOM</Origin>
        <Destination>NYC</Destination>
        <DepartDate>2013-09-15</DepartDate>
        <ReturnDate>2013-09-16</ReturnDate>
        <AdultPax>1</AdultPax>
        <ChildPax>0</ChildPax>
        <InfantPax>0</InfantPax>
        <Currency>INR</Currency>
        <Preferredclass>E</Preferredclass>
        <Eticket>true</Eticket>
        <Clientid>77752369</Clientid>
        <Clientpassword>*AB424E52FB5ASD23YN63A099A7B747A9BAF61F8E</Clientpassword>
        <Clienttype>ArzooINTLWS1.0</Clienttype>
        <PreferredAirline></PreferredAirline>
</AvailRequest>

I've taken the above code in a variable $xml. My PHP cURL code is as follows:

$URL = "http://59.162.33.102:9301/Avalability";

    //setting the curl parameters.
    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL,$URL);
    curl_setopt($ch, CURLOPT_VERBOSE, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/xml'));
    curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);

        if (curl_errno($ch))
    {
        // moving to display page to display curl errors
          echo curl_errno($ch) ;
          echo curl_error($ch);
    }
    else
    {
        //getting response from server
        $response = curl_exec($ch);
         print_r($response);
         curl_close($ch);
    }

I'm not getting anything in response. I've spoken about the same with the API Provider but they found empty request in their log. Am i missing something from my end. Your reply will be appreciated. Thank You.

解决方案

After Struggling a bit with Arzoo International flight API, I've finally found the solution and the code simply works absolutely great with me. Here are the complete working code:

//Store your XML Request in a variable
    $input_xml = '<AvailRequest>
            <Trip>ONE</Trip>
            <Origin>BOM</Origin>
            <Destination>JFK</Destination>
            <DepartDate>2013-09-15</DepartDate>
            <ReturnDate>2013-09-16</ReturnDate>
            <AdultPax>1</AdultPax>
            <ChildPax>0</ChildPax>
            <InfantPax>0</InfantPax>
            <Currency>INR</Currency>
            <PreferredClass>E</PreferredClass>
            <Eticket>true</Eticket>
            <Clientid>777ClientID</Clientid>
            <Clientpassword>*Your API Password</Clientpassword>
            <Clienttype>ArzooINTLWS1.0</Clienttype>
            <PreferredAirline></PreferredAirline>
    </AvailRequest>';

Now I've made a little changes in the above curl_setopt declaration as follows:

    $url = "http://59.162.33.102:9301/Avalability";

        //setting the curl parameters.
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
// Following line is compulsary to add as it is:
        curl_setopt($ch, CURLOPT_POSTFIELDS,
                    "xmlRequest=" . $input_xml);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 300);
        $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);
        print_r('</pre>');

That's it the code works absolutely fine for me. I really appreciate @hakre & @Lucas For their wonderful support.

这篇关于使用 php curl 将 XML 数据发送到 webservice的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-26 01:41