我只是使用
simplexml_load_file
解析xml文件
当我用它来解析文件时,它工作得很好,并返回所有文件内容
<?php
//$homepage = file_get_contents('');
$x = simplexml_load_file('http://www.metacafe.com/api/videos?vq=sexy&max-results=50');
echo "<pre>";
print_r($x);
echo "</pre>";
?>
出局就好了
SimpleXMLElement Object
(
[@attributes] => Array
(
[version] => 2.0
[source] => Metacafe
)
[title] => Metacafe
[channel] => SimpleXMLElement Object
(
[title] => SimpleXMLElement Object
(
)
[link] => http://www.metacafe.com/tags/sexy/
[image] => SimpleXMLElement Object
(
[url] => http://s.mcstatic.com/Images/MCLogo4RSS.jpg
[link] => http://www.metacafe.com
[title] => Metacafe
[height] => 65
[width] => 229
)
[description] => SimpleXMLElement Object
(
)
[item] => Array
(
[0] => SimpleXMLElement Object
(
[id] => 9310776
[author] => ORLIK MUSCLE VIDEO
[title] => Muscle Worship. MEET: 18 Y.o. Vlad
[link] => http://www.metacafe.com/watch/9310776/muscle_worship_meet_18_y_o_vlad/
[rank] => 4.33
[category] => Sports
[description] => SimpleXMLElement Object
(
)
[guid] => http://www.metacafe.com/watch/9310776/muscle_worship_meet_18_y_o_vlad/
[pubDate] => 01-Nov-12 +0000
)
[1] => SimpleXMLElement Object
(
[id] => yt-RU2yJxjxMtw
[author] => Metacafe Affiliate U
[title] => 10 Minute Intense Sexy Bikini ABS WORKOUT!! TRY IT!!
[link] => http://www.metacafe.com/watch/yt-RU2yJxjxMtw/10_minute_intense_sexy_bikini_abs_workout_try_it/
[rank] => 4.33
[category] => How To
[description] => SimpleXMLElement Object
(
)
[guid] => http://www.metacafe.com/watch/yt-RU2yJxjxMtw/10_minute_intense_sexy_bikini_abs_workout_try_it/
[pubDate] => 30-Oct-12 +0000
)
[2] => SimpleXMLElement Object
(
[id] => 9296344
[author] => Sonia Wari
[title] => Arab Real Hot Girl Desi Video
[link] => http://www.metacafe.com/watch/9296344/arab_real_hot_girl_desi_video/
[rank] => 4.33
[category] => Entertainment
[description] => SimpleXMLElement Object
(
)
[guid] => http://www.metacafe.com/watch/9296344/arab_real_hot_girl_desi_video/
[pubDate] => 28-Oct-12 +0000
)
[3] => SimpleXMLElement Object
(
[id] => 9299266
[author] => lehren.tv
[title] => Sherlyn Chopra In 3D Kamasutra
[link] => http://www.metacafe.com/watch/9299266/sherlyn_chopra_in_3d_kamasutra/
[rank] => 4.22
[category] => Entertainment
[description] => SimpleXMLElement Object
(
)
[guid] => http://www.metacafe.com/watch/9299266/sherlyn_chopra_in_3d_kamasutra/
[pubDate] => 29-Oct-12 +0000
)
[4] => SimpleXMLElement Object
(
[id] => cb-r3B4NEf5A8sw
[author] => CBS
[title] => Can the New Dart Give Dodge a Sexy Italian Accent? CNET On Cars, Episode 5
[link] => http://www.metacafe.com/watch/cb-r3B4NEf5A8sw/can_the_new_dart_give_dodge_a_sexy_italian_accent_cnet_on_cars_episode_5/
[rank] => 4.00
[category] => Entertainment
[description] => SimpleXMLElement Object
(
)
[guid] => http://www.metacafe.com/watch/cb-r3B4NEf5A8sw/can_the_new_dart_give_dodge_a_sexy_italian_accent_cnet_on_cars_episode_5/
[pubDate] => 30-Oct-12 +0000
)
[5] => SimpleXMLElement Object
(
[id] => cb-jZx26n5Iweg1
[author] => CBS
[title] => The Young and the Restless - 10/30/2012 Sneak Peek - Season 40
[link] => http://www.metacafe.com/watch/cb-jZx26n5Iweg1/the_young_and_the_restless_10_30_2012_sneak_peek_season_40/
[rank] => 4.00
[category] => Entertainment
[description] => SimpleXMLElement Object
(
)
[guid] => http://www.metacafe.com/watch/cb-jZx26n5Iweg1/the_young_and_the_restless_10_30_2012_sneak_peek_season_40/
[pubDate] => 30-Oct-12 +0000
)
)
)
)
当我试图仅打印项目数组时,问题才刚刚开始
<?php
//$homepage = file_get_contents('');
$x = simplexml_load_file('http://www.metacafe.com/api/videos?vq=sexy&max-results=50');
echo "<pre>";
print_r($x->channel->item);
echo "</pre>";
?>
这个只获得item数组的第一个对象而不是整个数组
SimpleXMLElement Object
(
[id] => 9310776
[author] => ORLIK MUSCLE VIDEO
[title] => Muscle Worship. MEET: 18 Y.o. Vlad
[link] => http://www.metacafe.com/watch/9310776/muscle_worship_meet_18_y_o_vlad/
[rank] => 4.33
[category] => Sports
[description] => SimpleXMLElement Object
(
)
[guid] => http://www.metacafe.com/watch/9310776/muscle_worship_meet_18_y_o_vlad/
[pubDate] => 01-Nov-12 +0000
)
我需要做的是检索项目数组的整个对象
答案 0 :(得分:3)
<?php
//$homepage = file_get_contents('');
$x = simplexml_load_file('http://www.metacafe.com/api/videos?vq=sexy&max-results=50');
echo "<pre>";
foreach($x->channel->item as $key=>$value){
print_r($value);
}
echo "</pre>";
?>
只需在foreach循环中制作它吗?