我需要从.asp
网址中获取XML数据,但无法弄清楚它出错的地方。尝试:
<?php
$url = "http://bookings.emperordivers.com/webScheduleSpecificXML_all.asp";
$feed = file_get_contents($url);
$xml = simplexml_load_string($feed);
// Display the first post title
echo $xml->Schedules->Schedule[0]->Boat;
我希望这会有效,但怀疑.asp网址会以某种方式阻止,所以请尝试:
function get_data($url) {
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$returned_content = get_data('http://bookings.emperordivers.com/webScheduleSpecificXML_all.asp');
$xml = simplexml_load_string($returned_content);
// Display the first post title
echo $xml->Schedules->Schedule[0]->Boat;
在这两种情况下都是漂亮的白色屏幕。是否有一个简单的技巧可以让它发挥作用?
答案 0 :(得分:0)
您无法正确访问它:
echo $xml->Schedule[0]->Boat;
格式为:
SimpleXMLElement Object
(
[Schedule] => Array
(
[0] => SimpleXMLElement Object
(
[Start] => 2017-09-24
[Duration] => 7
[Boat] => MV Emperor Orion
[Itinerary] => Best of Maldives
[Dep-Arr] => Male
[Spaces] => 4
[Rates] => SimpleXMLElement Object
(
[Rate] => Array
(
[0] => 1149
[1] => 1,057.08
[2] => 1,367.31
)
)
)
...