简体   繁体   English

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

[英]Send XML data to webservice using php curl

I'm working on Flight API of arzoo.我正在研究 arzoo 的 Flight API。 The server must receive the posted data in simple POST Request.服务器必须在简单的 POST 请求中接收发布的数据。 To achieve this i'm using PHP cURL.为了实现这一点,我正在使用 PHP cURL。 In the API Document it is clearly mention that the data should be sent in the following format:在 API 文档中明确提到数据应按以下格式发送:

<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.我把上面的代码放在一个变量 $xml 中。 My PHP cURL code is as follows:我的 PHP cURL 代码如下:

$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.我已经和 API Provider 谈过同样的事情,但他们在他们的日志中发现了空请求。 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.在与 Arzoo 国际航班 API 苦苦挣扎之后,我终于找到了解决方案,并且代码对我来说简直太棒了。 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:现在我对上面的 curl_setopt 声明做了一些改动,如下所示:

    $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.我非常感谢@hakre 和@Lucas 的大力支持。

Previous anwser works fine.以前的 anwser 工作正常。 I would just add that you dont need to specify CURLOPT_POSTFIELDS as "xmlRequest=" . $input_xml我只想补充一点,您不需要将 CURLOPT_POSTFIELDS 指定为"xmlRequest=" . $input_xml "xmlRequest=" . $input_xml to read your $_POST. "xmlRequest=" . $input_xml读取您的 $_POST。 You can use file_get_contents('php://input') to get the raw post data as plain XML.您可以使用file_get_contents('php://input')将原始帖子数据作为纯 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');

If you are using shared hosting, then there are chances that outbound port might be disabled by your hosting provider.如果您使用的是共享主机,那么您的主机提供商可能会禁用出站端口。 So please contact your hosting provider and they will open the outbound port for you所以请联系您的托管服务提供商,他们将为您打开出站端口

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM