循环结果Simple XML php

时间:2015-12-21 20:58:59

标签: php xml simplexml

我不确定如何循环使用Simple XML响应。我刚开始探索XML,可以通过PHP MYSQL循环,但不能通过Simple XML循环。有什么帮助吗?

$XML = "&xml=<HotelListRequest><city>$citysearch</city><currencyCode>£</currencyCode><arrivalDate>01/17/2016</arrivalDate><departureDate>01/18/2016</departureDate><supplierType>E</supplierType><RoomGroup><Room><numberOfAdults>$noaddults</numberOfAdults><numberOfChildren>$nochildren</numberOfChildren></Room></RoomGroup><numberOfResults>5</numberOfResults><locale>en_US</locale><sort>PRICE</sort></HotelListRequest>";

// Sending Request to API

$header[] = "Accept: application/xml";
$ch = curl_init();
curl_setopt( $ch, CURLOPT_HTTPHEADER, $header );
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch,CURLOPT_POST,5);
curl_setopt($ch,CURLOPT_POSTFIELDS,$XML);
curl_setopt( $ch, CURLOPT_URL, $url.$XML );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
$response = curl_exec($ch);
$response = simplexml_load_string($response);

输出

SimpleXMLElement Object ( [customerSessionId] => xxxxxxxx-6DB54xxxx [numberOfRoomsRequested] => 1 [moreResultsAvailable] => true [cacheKey] => 41ab978e:151c66db542:-446a [cacheLocation] => 10.186.170.129:7300 [HotelList] => SimpleXMLElement Object ( [@attributes] => Array ( [size] => 5 [activePropertyCount] => 1253 ) [HotelSummary] => Array ( [0] => SimpleXMLElement Object ( [@attributes] => Array ( [order] => 0 ) [hotelId] => 402789 [name] => Venture Hostel [address1] => 86 Tanner's Hill [city] => London [postalCode] => SE8 4PN [countryCode] => GB [airportCode] => LHR [supplierType] => E [propertyCategory] => 5 [hotelRating] => 2.0 [confidenceRating] => 52 [amenityMask] => 133120 [tripAdvisorRating] => 3.0 [locationDescription] => In London (Lewisham) [shortDescription] => <p><b>Property Location</b> <br />With a stay at Venture Hostel in London (Lewisham), you'll be close to Greenwich Market and O2 Arena. This hostel is within close proximity of Cutty Sark and [highRate] => 8.36 [lowRate] => 8.36 [rateCurrencyCode] => GBP [latitude] => 51.47361 [longitude] => -0.02712 [proximityDistance] => 3.9014914 [proximityUnit] => MI [hotelInDestination] => true [thumbNailUrl] => /hotels/5000000/4920000/4916900/4916812/4916812_41_t.jpg [deepLink] => http://www.travelnow.com/templates/55505/hotels/402789/overview?lang=en&currency=GBP&standardCheckin=1/17/2016&standardCheckout=1/18/2016&roomsCount=1&rooms[0].adultsCount=2 [RoomRateDetailsList] => SimpleXMLElement Object ( [RoomRateDetails] => SimpleXMLElement Object ( [roomTypeCode] => 200585535 [rateCode] => 205900877 [maxRoomOccupancy] => 2 [quotedRoomOccupancy] => 1 [minGuestAge] => 0 [roomDescription] => Shared Dormitory, Mixed Dorm (8 Bed) [currentAllotment] => 12 [propertyAvailable] => true [propertyRestricted] => false [expediaPropertyId] => 4916812 [rateKey] => 9b7dbc12-65ba-4a72-b9c0-f991e610f9ee [RateInfo] => SimpleXMLElement Object ( [@attributes] => Array ( [priceBreakdown] => true [promo] => false [rateChange] => false ) [ChargeableRateInfo] => SimpleXMLElement Object ( [@attributes] => Array ( [averageBaseRate] => 8.36 [averageRate] => 8.36 [commissionableUsdTotal] => 12.45 [currencyCode] => GBP [maxNightlyRate] => 8.36 [nightlyRateTotal] => 8.36 [surchargeTotal] => 11.70 [total] => 20.06 ) [NightlyRatesPerRoom] => SimpleXMLElement Object ( [@attributes] => Array ( [size] => 1 ) [0] => SimpleXMLElement Object ( [@attributes] => Array ( [baseRate] => 8.36 [rate] => 8.36 [promo] => false ) ) ) [Surcharges] => SimpleXMLElement Object ( [@attributes] => Array ( [size] => 2 ) [Surcharge] => Array ( [0] => SimpleXMLElement Object ( [@attributes] => Array ( [type] => ExtraPersonFee [amount] => 8.36 ) ) [1] => SimpleXMLElement Object ( [@attributes] => Array ( [type] => TaxAndServiceFee [amount] => 3.34 ) ) ) ) ) ) [ValueAdds] => SimpleXMLElement Object ( [@attributes] => Array ( [size] => 2 ) [ValueAdd] => Array ( [0] => SimpleXMLElement Object ( [@attributes] => Array ( [id] => 2048 ) [description] => Free Wireless Internet ) [1] => SimpleXMLElement Object ( [@attributes] => Array ( [id] => 2 ) [description] => Continental Breakfast ) ) ) ) ) ) [1] => SimpleXMLElement Object ( [@attributes] => Array ( [order] => 1 ) [hotelId] => 325381 [name] => Hostel 639 [address1] => 639 Harrow Road [address2] => Kensal Green [city] => London [postalCode] => NW10 5NU [countryCode] => GB [airportCode] => LCY [supplierType] => E [propertyCategory] => 5 [hotelRating] => 2.0 [confidenceRating] => 52 [amenityMask] => 256 [tripAdvisorRating] => 2.0 [locationDescription] => In London (North London) [shortDescription] => <p><b>Property Location</b> <br />With a stay at Hostel 639 in London (North London), you'll be close to Portobello Road Market and Royal Albert Hall. This hostel is close to Imperial College London [highRate] => 9.77 [lowRate] => 8.47 [rateCurrencyCode] => GBP [latitude] => 51.53012 [longitude] => -0.22551 [proximityDistance] => 5.5449877 [proximityUnit] => MI [hotelInDestination] => true [thumbNailUrl] => /hotels/3000000/2840000/2831600/2831521/2831521_73_t.jpg [deepLink] => http://www.travelnow.com/templates/55505/hotels/325381/overview?lang=en&currency=GBP&standardCheckin=1/17/2016&standardCheckout=1/18/2016&roomsCount=1&rooms[0].adultsCount=2 [RoomRateDetailsList] => SimpleXMLElement Object ( [RoomRateDetails] => SimpleXMLElement Object ( [roomTypeCode] => 200052408 [rateCode] => 200307052 [maxRoomOccupancy] => 8 [quotedRoomOccupancy] => 0 [minGuestAge] => 18 [roomDescription] => Shared Dormitory, Mixed Dorm (8 Beds) [promoId] => 214239009 [promoDescription] => Save 13% [currentAllotment] => 75 [propertyAvailable] => true [propertyRestricted] => false [expediaPropertyId] => 2831521 [rateKey] => 57c8780b-2a50-45ff-8f0a-bb08622f071e [RateInfo] => SimpleXMLElement Object ( [@attributes] => Array ( [priceBreakdown] => true [promo] => true [rateChange] => false ) [ChargeableRateInfo] => SimpleXMLElement Object ( [@attributes] => Array ( [averageBaseRate] => 9.77 [averageRate] => 8.47 [commissionableUsdTotal] => 12.62 [currencyCode] => GBP [maxNightlyRate] => 8.47 [nightlyRateTotal] => 8.47 [surchargeTotal] => 11.86 [total] => 20.33 ) [NightlyRatesPerRoom] => SimpleXMLElement Object ( [@attributes] => Array ( [size] => 1 ) [0] => SimpleXMLElement Object ( [@attributes] => Array ( [baseRate] => 9.77 [rate] => 8.47 [promo] => true ) ) ) [Surcharges] => SimpleXMLElement Object ( [@attributes] => Array ( [size] => 2 ) [Surcharge] => Array ( [0] => SimpleXMLElement Object ( [@attributes] => Array ( [type] => ExtraPersonFee [amount] => 8.47 ) ) [1] => SimpleXMLElement Object ( [@attributes] => Array ( [type] => TaxAndServiceFee [amount] => 3.39 ) ) ) ) ) ) ) ) ) [2] => SimpleXMLElement Object ( [@attributes] => Array ( [order] => 2 ) [hotelId] => 423378 [name] => RestUp London - Hostel [address1] => 172 New Kent Road [city] => London [postalCode] => SE1 4YT [countryCode] => GB [airportCode] => LTN [supplierType] => E [propertyCategory] => 5 [hotelRating] => 2.0 [confidenceRating] => 52 [amenityMask] => 196864 [tripAdvisorRating] => 3.0 [locationDescription] => In London (South Bank) [shortDescription] => <p><b>Property Location</b> <br />With a stay at RestUp London - Hostel, you'll be centrally located in London, convenient to London South Bank University and London Bridge. This hostel is close to [highRate] => 11.22 [lowRate] => 8.49 [rateCurrencyCode] => GBP [latitude] => 51.49413 [longitude] => -0.0906 [proximityDistance] => 1.0447516 [proximityUnit] => MI [hotelInDestination] => true [thumbNailUrl] => /hotels/6000000/5570000/5568700/5568679/5568679_65_t.jpg [deepLink] => http://www.travelnow.com/templates/55505/hotels/423378/overview?lang=en&currency=GBP&standardCheckin=1/17/2016&standardCheckout=1/18/2016&roomsCount=1&rooms[0].adultsCount=2 [RoomRateDetailsList] => SimpleXMLElement Object ( [RoomRateDetails] => SimpleXMLElement Object ( [roomTypeCode] => 200352902 [rateCode] => 202156304 [maxRoomOccupancy] => 10 [quotedRoomOccupancy] => 1 [minGuestAge] => 18 [roomDescription] => Bed in 10-Bed Dormitory with Shared Bathroom [promoId] => 213611478 [promoDescription] => Save 24% [currentAllotment] => 21 [propertyAvailable] => true [propertyRestricted] => false [expediaPropertyId] => 5568679 [rateKey] => cf24b9fd-4e06-4086-b142-236832744d6b [RateInfo] => SimpleXMLElement Object ( [@attributes] => Array ( [priceBreakdown] => true [promo] => true [rateChange] => false ) [ChargeableRateInfo] => SimpleXMLElement Object ( [@attributes] => Array ( [averageBaseRate] => 11.22 [averageRate] => 8.49 [commissionableUsdTotal] => 12.65 [currencyCode] => GBP [maxNightlyRate] => 8.49 [nightlyRateTotal] => 8.49 [surchargeTotal] => 11.88 [total] => 20.37 ) [NightlyRatesPerRoom] => SimpleXMLElement Object ( [@attributes] => Array ( [size] => 1 ) [0] => SimpleXMLElement Object ( [@attributes] => Array ( [baseRate] => 11.22 [rate] => 8.49 [promo] => true ) ) ) [Surcharges] => SimpleXMLElement Object ( [@attributes] => Array ( [size] => 2 ) [Surcharge] => Array ( [0] => SimpleXMLElement Object ( [@attributes] => Array ( [type] => ExtraPersonFee [amount] => 8.49 ) ) [1] => SimpleXMLElement Object ( [@attributes] => Array ( [type] => TaxAndServiceFee [amount] => 3.39 ) ) ) ) ) ) [ValueAdds] => SimpleXMLElement Object ( [@attributes] => Array ( [size] => 1 ) [ValueAdd] => SimpleXMLElement Object ( [@attributes] => Array ( [id] => 2048 ) [description] => Free Wireless Internet ) ) ) ) ) [3] => SimpleXMLElement Object ( [@attributes] => Array ( [order] => 3 ) [hotelId] => 394808 [name] => The Londonears Hostel [address1] => 1 Barkston Gardens [city] => London [postalCode] => SW5 0ER [countryCode] => GB [airportCode] => LHR [supplierType] => E [propertyCategory] => 5 [hotelRating] => 2.0 [confidenceRating] => 52 [amenityMask] => 0 [tripAdvisorRating] => 2.0 [locationDescription] => Near London Natural History Museum [shortDescription] => <p><b>Property Location</b> <br />With a stay at The Londonears Hostel, you'll be centrally located in London, convenient to Kensington Roof Gardens and Imperial College London. This hostel is close [highRate] => 10.45 [lowRate] => 10.45 [rateCurrencyCode] => GBP [latitude] => 51.49285 [longitude] => -0.18991 [proximityDistance] => 3.2764168 [proximityUnit] => MI [hotelInDestination] => true [thumbNailUrl] => /hotels/5000000/4700000/4694100/4694093/4694093_70_t.jpg [deepLink] => http://www.travelnow.com/templates/55505/hotels/394808/overview?lang=en&currency=GBP&standardCheckin=1/17/2016&standardCheckout=1/18/2016&roomsCount=1&rooms[0].adultsCount=2 [RoomRateDetailsList] => SimpleXMLElement Object ( [RoomRateDetails] => SimpleXMLElement Object ( [roomTypeCode] => 200134869 [rateCode] => 200753387 [maxRoomOccupancy] => 10 [quotedRoomOccupancy] => 1 [minGuestAge] => 0 [roomDescription] => Shared Dormitory, 1 Single Bed (10 Bed) [currentAllotment] => 13 [propertyAvailable] => true [propertyRestricted] => false [expediaPropertyId] => 4694093 [rateKey] => fd7fe5b5-f791-4375-857b-f418c7e39896 [RateInfo] => SimpleXMLElement Object ( [@attributes] => Array ( [priceBreakdown] => true [promo] => false [rateChange] => false ) [ChargeableRateInfo] => SimpleXMLElement Object ( [@attributes] => Array ( [averageBaseRate] => 8.71 [averageRate] => 8.71 [commissionableUsdTotal] => 15.57 [currencyCode] => GBP [maxNightlyRate] => 8.71 [nightlyRateTotal] => 8.71 [surchargeTotal] => 12.19 [total] => 20.90 ) [NightlyRatesPerRoom] => SimpleXMLElement Object ( [@attributes] => Array ( [size] => 1 ) [0] => SimpleXMLElement Object ( [@attributes] => Array ( [baseRate] => 8.71 [rate] => 8.71 [promo] => false ) ) ) [Surcharges] => SimpleXMLElement Object ( [@attributes] => Array ( [size] => 2 ) [Surcharge] => Array ( [0] => SimpleXMLElement Object ( [@attributes] => Array ( [type] => ExtraPersonFee [amount] => 10.45 ) ) [1] => SimpleXMLElement Object ( [@attributes] => Array ( [type] => TaxAndServiceFee [amount] => 1.74 ) ) ) ) ) ) ) ) ) [4] => SimpleXMLElement Object ( [@attributes] => Array ( [order] => 4 ) [hotelId] => 336155 [name] => Palmers Lodge Swiss Cottage - Hostel [address1] => 40 College Crescent [address2] => Swiss Cottage [city] => London [postalCode] => NW3 5LB [countryCode] => GB [airportCode] => LHR [supplierType] => E [propertyCategory] => 5 [hotelRating] => 2.0 [confidenceRating] => 52 [amenityMask] => 264 [tripAdvisorRating] => 4.0 [locationDescription] => In London (Hampstead) [shortDescription] => <p><b>Property Location</b> <br />With a stay at Palmers Lodge Swiss Cottage - Hostel in London (Hampstead), you'll be minutes from Swiss Cottage Farmers' Market and Hampstead Theatre. This hostel [highRate] => 30.0 [lowRate] => 24.0 [rateCurrencyCode] => GBP [latitude] => 51.54552 [longitude] => -0.1756 [proximityDistance] => 4.6696467 [proximityUnit] => MI [hotelInDestination] => true [thumbNailUrl] => /hotels/4000000/3130000/3125600/3125521/3125521_83_t.jpg [deepLink] => http://www.travelnow.com/templates/55505/hotels/336155/overview?lang=en&currency=GBP&standardCheckin=1/17/2016&standardCheckout=1/18/2016&roomsCount=1&rooms[0].adultsCount=2 [RoomRateDetailsList] => SimpleXMLElement Object ( [RoomRateDetails] => SimpleXMLElement Object ( [roomTypeCode] => 200145115 [rateCode] => 200813400 [maxRoomOccupancy] => 2 [quotedRoomOccupancy] => 2 [minGuestAge] => 0 [roomDescription] => Mixed Dorm (9 Double Bed) [promoId] => 213544183 [promoDescription] => Save 20% [currentAllotment] => 6 [propertyAvailable] => true [propertyRestricted] => false [expediaPropertyId] => 3125521 [rateKey] => 1ce0444e-0fc9-47c8-87b7-8641b184b173 [RateInfo] => SimpleXMLElement Object ( [@attributes] => Array ( [priceBreakdown] => true [promo] => true [rateChange] => false ) [ChargeableRateInfo] => SimpleXMLElement Object ( [@attributes] => Array ( [averageBaseRate] => 26.00 [averageRate] => 20.00 [commissionableUsdTotal] => 35.75 [currencyCode] => GBP [maxNightlyRate] => 20.00 [nightlyRateTotal] => 20.00 [surchargeTotal] => 4.00 [total] => 24.00 ) [NightlyRatesPerRoom] => SimpleXMLElement Object ( [@attributes] => Array ( [size] => 1 ) [0] => SimpleXMLElement Object ( [@attributes] => Array ( [baseRate] => 26.00 [rate] => 20.00 [promo] => true ) ) ) [Surcharges] => SimpleXMLElement Object ( [@attributes] => Array ( [size] => 1 ) [Surcharge] => SimpleXMLElement Object ( [@attributes] => Array ( [type] => TaxAndServiceFee [amount] => 4.00 ) ) ) ) ) [ValueAdds] => SimpleXMLElement Object ( [@attributes] => Array ( [size] => 1 ) [ValueAdd] => SimpleXMLElement Object ( [@attributes] => Array ( [id] => 2048 ) [description] => Free Wireless Internet ) ) ) ) ) ) ) ) 

稍后我只使用输出xml数据的简单ECHO语句,但是,它只显示一个酒店而不显示其他可用酒店。

Thnx的帮助。

1 个答案:

答案 0 :(得分:0)

除了我的评论:你可以像在数组中一样遍历SimpleXML对象:

$XML = "&xml=<HotelListRequest><city>$citysearch</city><currencyCode>£</currencyCode><arrivalDate>01/17/2016</arrivalDate><departureDate>01/18/2016</departureDate><supplierType>E</supplierType><RoomGroup><Room><numberOfAdults>$noaddults</numberOfAdults><numberOfChildren>$nochildren</numberOfChildren></Room></RoomGroup><numberOfResults>5</numberOfResults><locale>en_US</locale><sort>PRICE</sort></HotelListRequest>";

// Sending Request to API

$header[] = "Accept: application/xml";
$ch = curl_init();
curl_setopt( $ch, CURLOPT_HTTPHEADER, $header );
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch,CURLOPT_POST,5);
curl_setopt($ch,CURLOPT_POSTFIELDS,$XML);
curl_setopt( $ch, CURLOPT_URL, $url.$XML );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
$response = curl_exec($ch);
$response = simplexml_load_string($response);

// to get the  customerSessionId xxxxxxxx-6DB54xxxx
echo "sessionID: " . $response->customerSessionId;

// to get the HotelList
print_r($response->HotelList);

// loop over the HotelList:
foreach ($response->HotelList as $hotel) {
    // do sth. useful with the hotel
    // e.g. build a pool or whatever ;-)
}