开发者

Help w/ Retrieving XML via cURL

I've run into trouble with the following php code:

<?php
$url = "http://api.ean.com/ean-services/rs/hotel/v3/list?    minorRev=1&cid=55505&apiKey=58x5kuujub8xbb5tzv3a2a开发者_开发百科8q&locale=en_US&currencyCode=USD&xml=    <HotelListRequest><destinationString>Seattle</destinationString>    <arrivalDate>08/01/2011</arrivalDate><departureDate>08/03/2011</departureDate><RoomGroup>    <Room><numberOfAdults>2</numberOfAdults></Room></RoomGroup>    <numberOfResults>1</numberOfResults></HotelListRequest>";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
$contents = curl_exec ($ch);
echo $contents;
curl_close($ch);
?>

The problem is that $contents contains markup that's not XML at all, so I can't parse it. It's confusing b/c entering the URL in my browser's address bar will display the XML document, but I can't seem to get a valid XML doc w/ this code.

Here is a snippet of the string that gets returned:

{"HotelListResponse":{"customerSessionId":"0ABAA83D-4428-4913-0382-28FBB1901EFC","numberOfRoomsRequested":1,"moreResultsAvailable":true,"cacheKey":"-32344284:1303828fbb1:-1ef9","cacheLocation":"10.186.168.61:7305","HotelList":{"@size":"1","HotelSummary":{"@order":"0"

Could someone explain to me where I'm going wrong?

Thx.


Instead of trying to get XML, which may not be provided, you could always work with what you have, which appears to be JSON.

$response = json_decode( $contents, true );

This will give you an associative array of your data, which can be much easier to work with.


Try to remove spaces: "/v3/list? minorRev=1" -> "/v3/list?minorRev=1"

  1. Make your URL correct, like

    $url = 'http://api.ean.com/ean-services/rs/hotel/v3/list?type=xml&minorRev=1&cid=55505&apiKey=58x5kuujub8xbb5tzv3a2a8q&locale=en_US&currencyCode=USD&xml=%3CHotelListRequest%3E%3CdestinationString%3ESeattle%3C/destinationString%3E%3CarrivalDate%3E08/01/2011%3C/arrivalDate%3E%3CdepartureDate%3E08/03/2011%3C/departureDate%3E%3CRoomGroup%3E%3CRoom%3E%3CnumberOfAdults%3E2%3C/numberOfAdults%3E%3C/Room%3E%3C/RoomGroup%3E%20%3CnumberOfResults%3E1%3C/numberOfResults%3E%3C/HotelListRequest%3E';

  2. Add option to accept xml only -- in browser we have such header -- in curl -- no:

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

  3. PROFIT!!!

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜