du90093662774150
du90093662774150
2014-08-16 04:57
浏览 37

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

I am trying to use simplexml to access the hotelname from an api, showing a hotel list.

I have used this code so far. It seems right to me, but I still can't get it to work.

<?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条回答 默认 最新

  • duanfei8399
    duanfei8399 2014-08-16 08:54
    已采纳

    This should be your new code:

    <?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;
      }
    
    ?> 
    

    Somehow the file request answers with a JSON file. Just parse it this way.

    点赞 评论

相关推荐