duancheng3042 2013-09-09 11:39
浏览 104
已采纳

使用php curl将XML数据发送到webservice

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.

  • 写回答

3条回答 默认 最新

  • dongwen1909 2013-09-09 21:22
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵