使用PHP从URL获取XML数据

时间:2014-03-11 16:28:59

标签: php html xml simplexml

在PHP中使用XML的新手。以下示例是作为类似问题的先前问题的解决方案而提供的,应该是正确的。我在加载页面时没有出现任何PHP错误,但是,我没有看到任何显示的内容(数组)。一直在看例子后的例子,并且不能为我的生活弄清楚这一点。我想知道PHP是否看到可能是URL可能需要一点点加载而且还没有等待足够长的时间?

$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);

任何反馈都会很棒。谢谢!

马特

1 个答案:

答案 0 :(得分:0)

查看$ url。

在&#39;&amp;&#39;之间有一个空格。并且&#39; apiKey&#39;,首先关闭它。

我得到了这个:

<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>

修改 有了这个PHP代码:

$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);

我得到了这个结果:

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" }

使我的代码与您的代码不同的是,我绝对确定查询网址中没有空格。