【问题标题】:getting XML request missing in XML post request using curl使用 curl 获取 XML 发布请求中缺少的 XML 请求
【发布时间】:2021-09-14 03:48:03
【问题描述】:

我需要通过在 xml 中发送数据,使用 post 请求方法从 api 获取一些数据。 这是我到目前为止所做的,但我得到了错误。

function htlbycountry_get(){
    
        $url = "http://xmldemo.travellanda.com/xmlv1/";
        $ch = curl_init($url);
        $timeout = 5;
        $d = "<Request>
             <Head>
             <Username>cecd5656c336e815f69a587c69aa34cc</Username>
             <Password>3skbiOKjtaeb</Password>
             <RequestType>GetHotels</RequestType>
             </Head>
             <Body>
             <CountryCode>GB</CountryCode>
             </Body>
             </Request>";
        curl_setopt($ch, CURLOPT_POSTFIELDS, $d);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('accept: application/xml',
                                                   'Content-Type: application/xml'
                                                   ));
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
        $rawdata = curl_exec($ch);
        print_r($rawdata);
    
    }

但我在响应类似这样的内容时遇到错误

<?xml version="1.0" encoding="UTF-8"?>
<Response>
  <Head>
    <ServerTime>2021-07-02T07:48:41</ServerTime>
    <ServerType>Test</ServerType>
    <ExecutionTime>0</ExecutionTime>
  </Head>
  <Body>
    <Error>
      <ErrorId>101</ErrorId>
      <ErrorText>XML request is missing. Use POST method to send the 'xml' parameter.</ErrorText>
    </Error>
  </Body>
</Response>

根据下面的@CBroe 建议,我尝试关注但得到相同的错误。

    function htlbycountry_get(){

        $url = "http://xmldemo.travellanda.com/xmlv1/";
        $timeout = 5;
        $reqdata = "<Request><Head><Username>cecd5656c336e815f69a587c69aa34cc</Username><Password>3skbiOKjtaeb</Password><RequestType>GetHotels</RequestType></Head><Body><CountryCode>GB</CountryCode></Body></Request>";
        $data = ['xml' => $reqdata];
        $headers = array(
            "Content-type: application/x-www-form-urlencoded",
        );
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL,$url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        curl_setopt($ch, CURLOPT_VERBOSE, 1);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

        $rawdata = curl_exec($ch);
        print_r($rawdata);
    }

【问题讨论】:

  • 错误消息似乎是说您不应该将 XML 作为请求正文发送,而是发出一个发送名为 xml 的参数的普通 POST 请求(因此也不是 Content-Type: application/xml),并使用您获得的 XML 数据作为值。
  • 老实说,我想了解您的回复,但我无法让它发挥作用。你能在这里多指导一点吗?我不明白这里的普通 POST 是什么意思。
  • “正常”是指application/x-www-form-urlencoded 请求,在请求正文中包含键=值对。
  • 您需要将获得的 XML 代码作为名为 xml 的参数的 发送。
  • CURLOPT_POSTFIELDS 默认情况下会在传递数组时导致 multipart/form-data 请求 - 不确定这是否与您尝试设置的标头冲突。尝试传递 http_build_query($data) 的结果。

标签: php xml api curl xmlhttprequest


【解决方案1】:

感谢@Cbroe,我终于成功了。这是工作代码。

function htlbycountry_get(){
        $url = "http://xmldemo.travellanda.com/xmlv1/";
        $timeout = 20;
        $countrycode = $this->input->get('countrycode');
        $reqdata = "<Request><Head><Username>cecd5656c336e815f69a587c69aa34cc</Username><Password>3skbiOKjtaeb</Password><RequestType>GetHotels</RequestType></Head><Body><CountryCode>$countrycode</CountryCode></Body></Request>";
        $data = array('xml' => $reqdata);
        $headers = array(
            "Content-type: application/x-www-form-urlencoded",
        );
        $ch = curl_init();
        $payload = http_build_query($data);
        curl_setopt($ch, CURLOPT_URL,$url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

        $rawdata = curl_exec($ch);
        $xml = simplexml_load_string($rawdata);
        $json = json_encode($xml);
        print_r($json);
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-08-04
    • 2013-12-09
    • 2014-05-19
    • 2012-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多