当我直接在浏览器中加载curl
函数中使用的URL时,我得到了XML格式的正确数据。
但是当我通过curl
调用它时,有时我得到JSON格式的数据,有时根本没有数据。
$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¤cyCode=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可以正常工作,所以你可以自己测试。
你需要在你的HTTP请求中明确指定XML内容类型,通过设置Accept
头字段,所以REST服务器知道你想要什么,例如:
Accept: text/xml,application/xml;q=1
在你的例子中(q=1是默认值):
curl_setopt($ch,CURLOPT_HTTPHEADER,array('Accept: text/xml,application/xml'));