dqhnp44220 2014-03-11 16:28
浏览 75
已采纳

使用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 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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里