尝试使用simplexml从ean酒店列表中访问该名称

时间:2014-08-16 04:57:51

标签: javascript php simplexml

我正在尝试使用simplexml从api访问hotelname,显示酒店列表。

到目前为止,我已经使用过此代码。这似乎对我而言,但我仍然无法让它发挥作用。

<?php

  $xml_file = 'http://dev.api.ean.com/ean-services/rs/hotel/v3/list?minorRev=99&apiKey=ycdydfqdz4w5huxv4psfxs8h&cid=55505&locale=en_US&currencyCode=USD&supplierCacheTolerance=MED_ENHANCED&countryCode=US&city=Dallas&stateProvinceCode=TX&searchRadius=50&numberOfResults=20&arrivalDate=08/31/2014&departureDate=09/15/2014&minRate=100&maxRate=1000&room1=1&numberOfAdults=2';
  $xml = simplexml_load_file($xml_file);

  $response = $xml->HotelListResponse->HotelList->HotelSummary;
  foreach($response as $val){ 
    echo $val->name;
  }

?> 

1 个答案:

答案 0 :(得分:2)

这应该是您的新代码:

<?php

  $xml_file = 'http://dev.api.ean.com/ean-services/rs/hotel/v3/list?minorRev=99&apiKey=ycdydfqdz4w5huxv4psfxs8h&cid=55505&locale=en_US&currencyCode=USD&supplierCacheTolerance=MED_ENHANCED&countryCode=US&city=Dallas&stateProvinceCode=TX&searchRadius=50&numberOfResults=20&arrivalDate=08/31/2014&departureDate=09/15/2014&minRate=100&maxRate=1000&room1=1&numberOfAdults=2';
  $content = file_get_contents($xml_file);

  $xml = json_decode($content);
  //$xml = simplexml_load_file($xml_file);

  $response = $xml->HotelListResponse->HotelList->HotelSummary;
  foreach($response as $val){ 
    echo $val->name;
  }

?> 

文件请求以某种方式回答了JSON文件。只需这样解析它。