【发布时间】:2014-02-06 14:39:54
【问题描述】:
我想从我所做的 API 调用中提取一些数据。使用 PHP CURL。
这是我的代码:
<?PHP
//use latest minorRev 14
$url ='http://api.ean.com/ean-services/rs/hotel/v3/list?minorRev=99';
$url .= '&apiKey=' . $apiKey;
$url .= '&cid=55505';
$url .= '&locale=en_US&city=Dallas&stateProvinceCode=TX&countryCode=US&numberOfResults=3';
$url .= '&searchRadius=50';
//using the cache returns results much faster
$url .= '&supplierCacheTolerance=MED_ENHANCED';
//dates and occupancy
$url .='&arrivalDate=09/04/2014&departureDate=09/05/2014&room1=2';
$header[] = "Accept: application/json";
$header[] = "Accept-Encoding: gzip";
$ch = curl_init();
curl_setopt( $ch, CURLOPT_HTTPHEADER, $header );
curl_setopt($ch,CURLOPT_ENCODING , "gzip");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
$response = json_decode(curl_exec($ch));
$response = curl_exec($ch);
?>
<html>
<body>
<table>
<Tr>
<TD>
<?PHP
$hotels = simplexml_load_file('http://api.ean.com/ean-services/rs/hotel/v3/list?minorRev=99&apiKey=7z6tduachrht362dpnsch34v&cid=55505&locale=en_US&city=Dallas&stateProvinceCode=TX&countryCode=US&numberOfResults=3&searchRadius=50&supplierCacheTolerance=MED_ENHANCED&arrivalDate=09/04/2014&departureDate=09/05/2014&room1=2');
echo $hotels;
?>
</TD>
</Tr>
</table>
为什么使用 simplexml_load_file 函数没有返回任何内容?
我也以此为指导 - http://blog.teamtreehouse.com/how-to-parse-xml-with-php5
【问题讨论】:
-
也许你不能只做 echo $hotels。尝试使用
die(var_dump($hotels)),您将看到 $hotels 是什么类型的对象以及它包含什么。我不完全了解该函数,但似乎您可能会从中获取一个数组或一个对象,您必须对其进行迭代才能加载其内容。 -
simplexml_load_file()调用与其他一切有什么关系? -
我已从此代码中删除了您的 API 密钥。如果那是一个实时密钥,您可能想要更改它。