我正在构建产品提要,但在提取 xml 代码深处的 xml 值时遇到了一些问题。
XML 代码是:(对于这个问题,我清理了不相关的值)
<ns2:HotelListResponse xmlns:ns2="http://v3.hotel.wsapi.ean.com/">
<customerSessionId>0ABAAA78-D672-ED91-3F32-332C20D917D9</customerSessionId>
<numberOfRoomsRequested>1</numberOfRoomsRequested>
<moreResultsAvailable>true</moreResultsAvailable>
<cacheKey>-39d672ed:13f3332c20d:1b00</cacheKey>
<cacheLocation>10.186.170.120:7300</cacheLocation>
<HotelList activePropertyCount="1621" size="1">
<HotelSummary order="0">
<RoomRateDetailsList>
<RoomRateDetails>
<RateInfo rateChange="true" promo="true" priceBreakdown="true">
<ChargeableRateInfo total="656.27" surchargeTotal="42.94" nightlyRateTotal="613.32996" maxNightlyRate="178.42" currencyCode="USD" commissionableUsdTotal="613.32996" averageRate="153.33249" averageBaseRate="170.37251">
</RateInfo>
</RoomRateDetails>
</RoomRateDetailsList>
</HotelSummary>
</HotelList>
</ns2:HotelListResponse>
我使用的php代码:
<?php
$post_string ='type=xml';
$post_string .= '&cid=55505';
$post_string .= '&minorRev=24';
$post_string .= '&apiKey=wwfzhd95v9vtzh4jc8yjcug9';
$post_string .= '&locale=nl_NL';
$post_string .= '¤cyCode=EUR';
$post_string .= '&customerIpAddress=10.184.2.9';
$post_string .= '&customerUserAgent=Mozilla/5.0+(Windows+NT+6.1)+AppleWebKit /535.11+(KHTML,+like+Gecko)+Chrome/17.0.963.79+Safari /535.11';
$post_string .= '&customerSessionId=';
$post_string .= '&xml=';
$post_string .= '<HotelListRequest>';
$post_string .= '<arrivalDate>$checkindate</arrivalDate>';
$post_string .= '<departureDate>$checkoutdate</departureDate>';
$post_string .= '<RoomGroup>';
$post_string .= '<Room>';
$post_string .= '<numberOfAdults>2</numberOfAdults>';
$post_string .= '</Room>';
$post_string .= '<Room>';
$post_string .= '<numberOfAdults>$adultcount</numberOfAdults>';
$post_string .= '<numberOfChildren></numberOfChildren>';
$post_string .= '</Room>';
$post_string .= '</RoomGroup>';
$post_string .= '<latitude>';
$post_string .= $lat;
$post_string .= '</latitude>';
$post_string .= '<longitude>';
$post_string .= $long;
$post_string .= '</longitude>';
$post_string .= '<searchRadius>30</searchRadius>';
$post_string .= '<searchRadiusUnit>km</searchRadiusUnit>';
$post_string .= '<minStarRating>';
$post_string .= $ratingmin;
$post_string .= '</minStarRating>';
$post_string .= '<maxStarRating>';
$post_string .= $ratingmax;
$post_string .= '</maxStarRating>';
$post_string .= '<minRate>';
$post_string .= $minprice;
$post_string .= '</minRate>';
$post_string .= '<maxRate>';
$post_string .= $maxprice;
$post_string .= '</maxRate>';
$post_string .= '<tripAdvisorRatingUrl>';
$post_string .= $mintripadvisorarray;
$post_string .= '</tripAdvisorRatingUrl>';
$post_string .= '<countryCode>FR</countryCode>';
$post_string .= '<supplierCacheTolerance>MED_ENHANCED</supplierCacheTolerance>';
$post_string .= '<sort>';
$post_string .= $sort;
$post_string .= '</sort>';
$post_string .= '<numberOfResults>10</numberOfResults>';
$post_string .= '</HotelListRequest> ';
$path = "http://api.ean.com/ean-services/rs/hotel/v3/list"; //Relative path to the file with $_POST parsing
$ch = curl_init($path);
$fp = fopen('data.xml','w');
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string); //Send the data to the file
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/xml'));
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_FILE, $fp);
$val = curl_exec($ch);
curl_close($ch);//Close curl session
fclose($fp); //Close file overwrite
//Use Curl to get XML here.
$data = simplexml_load_file('data.xml');
//Uncomment this to view the parsed XML on screen.
/*
echo '<pre>';
print_r($data->HotelList);
echo '</pre>';
*/
echo "<ul id=\"data\">\n";
$resultnumber = 0;
foreach($data->HotelList->HotelSummary as $info):
$resultnumber++;
$title=$info->name;
$add=$info->address1;
$city=$info->city;
$locationdescription=$info->locationDescription;
$locationdescription = lcfirst($locationdescription);
$count=$data->HotelList['activePropertyCount'];
$thumbnail1=$info->thumbNailUrl;
$thumbnail2= "http://images.travelnow.com";
$thumbnail = $thumbnail2.$thumbnail1;
$image = substr_replace($thumbnail, 'l', -5, -4);
$image1 = substr_replace($thumbnail, 'n', -5, -4);
$image2 = substr_replace($thumbnail, 'g', -5, -4);
$image3 = substr_replace($thumbnail, 'd', -5, -4);
$image4 = substr_replace($thumbnail, 'y', -5, -4);
$image5 = substr_replace($thumbnail, 'z', -5, -4);
$chargerate= $data->RoomRateDetailsList->RoomRateDetails->RateInfo->ChargeableRateInfo['total'];
$highrate=$info->highRate;
$prijs= round($highrate,0);
$deeplink=$info->deepLink;
$hotelrating=$info->hotelRating;
$hotelratinground = round($hotelrating,0);
$tripadvisorratingurl=$info->tripAdvisorRatingUrl;
$distance =$info->proximityDistance;
$distanceround= round($distance,0);
$chargeableRateInfo = $rateInfo->ChargeableRateInfo['total'];
echo
"<li>
<time> <i>Hotel ",$resultnumber," van ",$count,"</i> </time>
<div class=\"title\"><b> ",$title," <img src='../pics/",$hotelratinground,"-star-rating.png' width='100' height='20'></b></div>
<div class=\"artist\">Adres: ",$add,"</div>
<div class=\"locatie\">Locatie: ",$city,", ",$locationdescription,"</div>
<div class=\"image\"><img src=$image /></div>
<div class=\"prijs\">Prijs: €",$chargerate,"</div>
<div class=\"deeplink\"><a href=",$deeplink,">Bekijk aanbieding</a></div>
<div class=\"hotelrating\"> ",$hotelrating," sterren</div>
<div class=\"tripadvisorratingurl\"><img src=$tripadvisorratingurl/> ",$tripadvisorratingurl," </div>
<div class=\"distance \"> Afstand: ",$distanceround," km </div><br>
</li>\n";
endforeach;
echo "</ul>";
?>
所有代码都工作正常并且按预期工作。执行以下行:
$chargerate= $data->RoomRateDetailsList->RoomRateDetails->RateInfo->ChargeableRateInfo['total'];
有人可以向我解释我做错了什么吗?