使用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.

dshqd84261
dshqd84261 psst:或许告诉你哪些更改是必要的(在你已经添加的完整代码旁边)与你的问题相比较。;)
大约 7 年之前 回复
dpftppc9674
dpftppc9674 当然会立即这样做。
大约 7 年之前 回复
duansha8764
duansha8764 为了您的回复,我可以通过您的解决方案和另一个反复试验来解决这个问题。我非常感谢你在这个问题上花了很多时间。在hakre:感谢您的合作并照顾所述问题。上述评论中共享的链接与我面临的问题完全相似。我通过简单地添加curl_setopt($ch,CURLOPT_POSTFIELDS,“xmlRequest=”。$input_xml)得到了服务器的响应。和最后一个json代码来解码响应。谢谢你们俩。我非常感谢您的亲切工作和支持。
大约 7 年之前 回复
dongxia1390
dongxia1390 重复的建议是善意的。如果您想获得技术问题的答案,请参考您在那里使用的arzoo的API文档,否则我们无法说明需要如何形成请求才能工作。
大约 7 年之前 回复
douken0530
douken0530 可能重复如何从URL获取动态XML数据
大约 7 年之前 回复
ds3464
ds3464 如果($response===false),您可以使用严格比较来测试您的响应。卷曲错误0表示没有错误,因此您仍然有一个空的响应,使用==计算为false。我用curl命令行做了同样的请求,然后用空体获得200OK。
大约 7 年之前 回复
dpd66100
dpd66100 嗨卢卡斯,谢谢你的时间和回复。我之前也这样做了,这次也是:$response=curl_exec($ch);if($response==false){echo'Curlerror:'。curl_error($CH);echo'Curlerrorno:'。curl_errno($CH);}else{print_r($response);curl_close($CH);}::这段代码输出我:Curlerror:Curlerrorno:0。我不知道为什么它会进入if循环。
大约 7 年之前 回复
dongyu4554
dongyu4554 尝试在检查错误之前执行请求,例如:if(curl_exec($ch)===false)echo'Curlerror:'。curl_error($CH);
大约 7 年之前 回复

3个回答

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.

duanmeng9336
duanmeng9336 我是新手玩xml,如何动态使用$ input_xml。 我的意思是价值来自一个表格并使用$ POST传递它?
一年多之前 回复
doujiaozhan4397
doujiaozhan4397 对我来说,上面的代码不起作用。 返回XML无效。 但是,这个答案确实有助于stackoverflow.com/a/15679384/2990720
接近 3 年之前 回复



以前的anwser工作正常。 我只想补充一点,你不需要将CURLOPT_POSTFIELDS指定为“xmlRequest =”。 $ input_xml </ code>来读取$ _POST。 您可以使用 file_get_contents('php:// input')</ code>将原始发布数据作为纯XML格式。</ p>
</ div>

展开原文

原文

Previous anwser works fine. I would just add that you dont need to specify CURLOPT_POSTFIELDS as "xmlRequest=" . $input_xml to read your $_POST. You can use file_get_contents('php://input') to get the raw post data as plain XML.

Check this one. It will work.

function fetch($i1,$i2,$i3,$i4)
{
$input_data = '<I> 
                <i1>'.$i1.'</i1> 
                <i2>'.$i2.'</i2> 
                <i3>'.$i2.'</i3> 
                <i4>'.$i3.'</i4> 
              </I>';
$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_PORT => "8080",
  CURLOPT_URL => "http://192.168.1.100:8080/avaliablity",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => $input_data,
  CURLOPT_HTTPHEADER => array(
    "Cache-Control: no-cache",
    "Content-Type: application/xml"
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}
}

fetch('i1','i2','i3','i4');
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐