使用Expedia API和curl获取XML数据时出现问题 [英] Problem fetching XML data using Expedia API and curl

查看:141
本文介绍了使用Expedia API和curl获取XML数据时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在浏览器中直接加载 curl 函数中使用的URL时,我会以XML格式获取正确的数据。

When I load the URL used below in curl function directly in a browser, I get correct data in XML format.

但是当我通过 curl 调用它时,有时我获得JSON格式的数据,有时甚至没有dat。

But when I call it through curl, sometimes I get data in JSON format or sometimes no dat at all.

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"http://api.ean.com/ean-services/rs/hotel/v3/list?minorRev=4&cid=55505&apiKey=5q4gzx43g6ukcrq798z2hz75&customerSessionId=&locale=en_US&currencyCode=USD&xml=<HotelListRequest><city>new%20delhi</city><RoomGroup><Room><numberOfAdults>2</numberOfAdults><numberOfResults></numberOfResults></Room></RoomGroup></HotelListRequest>");
curl_setopt($ch, CURLOPT_FAILONERROR,1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);

curl_setopt($ch, CURLOPT_TIMEOUT, 15);
$retValue = curl_exec($ch);                     
curl_close($ch);

echo $retValue;

上述API正在运作,因此您可以自行测试。

The above API is working so you can test it yourself.

推荐答案

您需要在HTTP请求中明确指定XML内容类型,方法是设置 接受 头字段,因此REST服务器知道你想要什么,例如:

You need to specify XML content type explicitly in your HTTP request by setting Accept header field, so REST server knows what do you want e.g.:

Accept: text/xml,application/xml;q=1

在您的情况下(q = 1是默认值):

In your case (q=1 is default value):

curl_setopt($ch,CURLOPT_HTTPHEADER,array('Accept: text/xml,application/xml'));

这篇关于使用Expedia API和curl获取XML数据时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆