試試這個,對你有幫助。
<?php
/*CURL PHP library to get file contents*/
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://idragonlk.com/channels.xml");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
curl_close($ch);
/*End*/
/*Takes a well-formed XML string and returns it as an object.*/
$XMLOutPut = simplexml_load_string($output);
//$channel_list = getChannel_list($XMLOutPut);
//$programme_list = getProgramme_list($XMLOutPut);
echo "<pre>";print_r(getChannel_list($XMLOutPut));
echo "<pre>";print_r(getProgramme_list($XMLOutPut));
exit;
/*Function to fetch channel and programme*/
//channel
function getChannel_list($mainObj){$Result_List = array();
for($i=0;$i<count($mainObj->channel);$i++){
$Result_List[] = objectToArray($mainObj->channel[$i]);
}
return $Result_List;
}
//programme
function getProgramme_list($mainObj){$Result_List = array();
for($i=0;$i<count($mainObj->programme);$i++){
$Result_List[] = objectToArray($mainObj->programme[$i]);
}
return $Result_List;
}
//Convert Object to array.
function objectToArray($obj) {
if (is_object($obj)) {$obj = get_object_vars($obj);}
if (is_array($obj)) {return array_map(__FUNCTION__, $obj);}
else {return $obj;}
}
?>
輸出
Array
(
[0] => Array
(
[@attributes] => Array
(
[id] => 4seven UK
)
[display-name] => 4seven UK
[icon] => Array
(
[@attributes] => Array
(
[src] => 4seven UK.png
)
)
[url] => http://www.tvguide.co.uk
)
[1] => Array
(
[@attributes] => Array
(
[id] => 5STAR UK
)
[display-name] => 5STAR UK
[icon] => Array
......
......
回答了這個問題儘可能短。嘗試使用下面的代碼 –