[英]getting XML request missing in XML post request using curl
I need to get some data from api using post request method by sending data in xml.我需要通过在 xml 中发送数据使用 post 请求方法从 api 获取一些数据。 here what i have done till now, but i am getting error.这是我到目前为止所做的,但我遇到了错误。
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);
}
but i am getting error in response something like this但我在响应类似这样的事情时遇到错误
<?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>
As per @CBroe suggestion below, i tried following but getting same error.根据下面的@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);
}
Thanks to @Cbroe, i finally made it work.感谢@Cbroe,我终于让它工作了。 here is the working code.这是工作代码。
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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.