开发者

Problem fetching XML data using Expedia API and curl

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

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-ser开发者_Go百科vices/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;

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


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

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

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜