dqhnp44220
dqhnp44220
2014-03-11 16:28
浏览 71

使用PHP从URL获取XML数据

New to working with XML in PHP. The following example, which was given as a solution to a prior question of a similar issue, is supposed to be working correctly. I'm not having any PHP errors come up upon loading the page, however, I am not seeing anything being displayed (the array). Been looking at example after example and can't for the life of me figure this out. I'm wondering if PHP is seeing that maybe the URL could be taking a little bit to load and is not waiting long enough?

$context  = stream_context_create(array('http' => array('header' => 'Accept: application/xml')));
$url = 'http://api.ean.com/ean-services/rs/hotel/v3/list?cid=55505&minorRev=12&    apiKey=2hkhej72gxyas3ky6hhjtsga&locale=en_US&currencyCode=USD&customerIpAddress=10.184.2.9&customerSessionId=&xml=<HotelListRequest><arrivalDate>01/22/2012</arrivalDate><departureDate>01/24/2012</departureDate><RoomGroup><Room><numberOfAdults>1</numberOfAdults><numberOfChildren>1</numberOfChildren><childAges>4</childAges></Room></RoomGroup><city>Amsterdam</city><countryCode>NL</countryCode><supplierCacheTolerance>MED</supplierCacheTolerance></HotelListRequest> ';

$xml = file_get_contents($url, false, $context);
$xml = simplexml_load_string($xml);
print_r($xml);

Any feedback would be great. Thanks!

Matt

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongtan8979
    dongtan8979 2014-03-11 17:54
    已采纳

    Check out $url.

    You have a space inbetween the '&' and 'apiKey', remove that, first off.

    I'm getting this:

    <ns2:HotelListResponse xmlns:ns2="http://v3.hotel.wsapi.ean.com/">
      <EanWsError>
        <itineraryId>-1</itineraryId>
        <handling>RECOVERABLE</handling>
        <category>DATA_VALIDATION</category>
        <exceptionConditionId>-1</exceptionConditionId>
        <presentationMessage>
            Data in this request could not be validated: Specified arrival date is prior to today's date.
        </presentationMessage>
        <verboseMessage>
            Data in this request could not be validated: Specified arrival date is prior to today's date.
        </verboseMessage>
        <ServerInfo instance="71" timestamp="1394560288" serverTime="12:51:28.803-0500"/>
      </EanWsError>
      <customerSessionId>0ABAAA47-1A0E-8A91-44B2-24409C793B7A</customerSessionId>
    </ns2:HotelListResponse>
    

    Edit: With this php code:

    $context  = stream_context_create(array('http' => array('header' => 'Accept: application/xml')));
    $url = 'http://api.ean.com/ean-services/rs/hotel/v3/list?cid=55505&minorRev=12&';
    $url.= 'apiKey=2hkhej72gxyas3ky6hhjtsga&locale=en_US&currencyCode=USD';
    $url.= '&customerIpAddress=10.184.2.9&customerSessionId=&xml=<HotelListRequest>';  
    $url.= '<arrivalDate>01/22/2012</arrivalDate><departureDate>01/24/2012</departureDate><RoomGroup>';
    $url.= '<Room><numberOfAdults>1</numberOfAdults><numberOfChildren>1</numberOfChildren>';
    $url.= '<childAges>4</childAges></Room></RoomGroup><city>Amsterdam</city><countryCode>NL</countryCode>'; 
    $url.= '<supplierCacheTolerance>MED</supplierCacheTolerance></HotelListRequest>';
    
    $xml = file_get_contents($url, false, $context);
    $xml = simplexml_load_string($xml);
    var_dump($xml);
    

    I am getting this result:

    object(SimpleXMLElement)#1 (2) { ["EanWsError"]=> object(SimpleXMLElement)#2 (7) { ["itineraryId"]=> string(2)     "-1" ["handling"]=> string(11) "RECOVERABLE" ["category"]=> string(15) "DATA_VALIDATION" ["exceptionConditionId"]=> string(2) "-1" ["presentationMessage"]=> string(93) "Data in this request could not be validated: Specified arrival date is prior to today's date." ["verboseMessage"]=> string(93) "Data in this request could not be validated: Specified arrival date is prior to today's date." ["ServerInfo"]=> object(SimpleXMLElement)#3 (1) { ["@attributes"]=> array(3) { ["instance"]=> string(3) "113" ["timestamp"]=> string(10) "1394564545" ["serverTime"]=> string(17) "14:02:25.060-0500" } } } ["customerSessionId"]=> string(36) "0ABAAA71-C7B2-7914-4B22-8599F39026A8" }
    

    What makes my code different from yours is that I made absolutely sure that there were no spaces in the query url.

    点赞 评论