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

use*_*991 24 php xml curl

我正在研究arzoo的Flight API.服务器必须在简单的POST请求中接收发布的数据.要实现这一点,我正在使用PHP cURL.在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>
Run Code Online (Sandbox Code Playgroud)

我已经在变量$ xml中使用了上面的代码.我的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);
    }
Run Code Online (Sandbox Code Playgroud)

我没有得到任何回应.我已经与API提供商谈过相同的内容,但他们在日志中发现了空请求.我错过了我的一些东西.您的回复将不胜感激.谢谢.

use*_*991 44

在用Arzoo International飞行API苦苦挣扎之后,我终于找到了解决方案,而且代码对我来说非常棒.以下是完整的工作代码:

//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>';
Run Code Online (Sandbox Code Playgroud)

现在我对上面的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>');
Run Code Online (Sandbox Code Playgroud)

这就是代码对我来说绝对没问题.我非常感谢@hakre和@Lucas的精彩支持.


bpi*_*ile 9

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