使用 curl 在 XML post 请求中缺少 XML 请求

getting XML request missing in XML post request using curl

我需要通过在 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);
    }

感谢@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);
    }