我有PHP循环,它从Youtube频道的特定播放列表中提取视频信息,但当它遇到私人视频时会中断。它造成了一个致命的错误:
警告: main() function.main:第30行的F:\xampp\htdocs\youtube\index.php中不再存在节点 警告: main() function.main:第30行的F:\xampp\htdocs\youtube\index.php中不再存在节点 致命错误:在第35行的F:\xampp\htdocs\youtube\index.php中调用非对象的成员函数属性()
代码如下
<body>
<div id="mainPlayer" width="425" height="344">
</div>
</div>
<div id="mainPlaylist" class="grid_6 omega">
<h3 class="title">Legal Feed with Kate Wheeler</h3>
<ul id="playlists">
<?php
// Loop through each video in each playlist
// read feed into SimpleXML object
// *Moved into header to load main Video as well
$playlistFeedURL = 'http://gdata.youtube.com/feeds/api/playlists/6549D4CA7BB99B16?v=2';
$sxml = simplexml_load_file($playlistFeedURL);
// iterate over entries in feed
foreach ($sxml->entry as $entry) {
// get nodes in media: namespace for media information
$media = $entry->children('http://search.yahoo.com/mrss/');
// get video player URL
$attrs = $media->group->player->attributes();
$watch = $attrs['url'];
// get video thumbnail
$attrs = $media->group->thumbnail[0]->attributes();
$thumbnail = $attrs['url'];
// get <yt:duration> node for video length
$yt = $media->children('http://gdata.youtube.com/schemas/2007');
$attrs = $yt->duration->attributes();
$length = $attrs['seconds'];
//get <yt:videoId> node for Video ID
$yt = $media->children('http://gdata.youtube.com/schemas/2007');
$playlistVideoID = $yt->videoid;
// get <yt:stats> node for viewer statistics
$yt = $entry->children('http://gdata.youtube.com/schemas/2007');
$attrs = $yt->statistics->attributes();
$viewCount = $attrs['viewCount'];
// get <gd:rating> node for video ratings
$gd = $entry->children('http://schemas.google.com/g/2005');
if ($gd->rating) {
$attrs = $gd->rating->attributes();
$rating = $attrs['average'];
} else {
$rating = 0;
}
?>
<div class="item">
<span class="thumbnail left" rel="<?php echo $entry->id; ?>">
<a href="<?php echo $watch; ?>" rel="<?php echo $playlistVideoID; ?>" onclick="_run();"><img src="<?php echo $thumbnail;?>" /></a>
</span>
<a href="<?php echo $watch; ?>" rel="<?php echo $playlistVideoID; ?>" onclick="_run();"><?php echo $media->group->title; ?></a>
<span class="attr">Duration:</span> <?php printf('%0.2f', $length/60); ?> min.
<div class="clear"></div>
</div>
<?php
}
?>
</ul>
</body>请帮帮我。提前谢谢。
发布于 2012-11-14 19:06:37
你可能是用DOM把html从Youtube上拿出来的?这是值得称赞的,但是如果假设所有节点都在那里的话,那就太糟糕了。
$nodes = $dom->getElementsByTagName('video');
$nodes[0]->attribute(....); #<---but whatever if there's no video elements at all?
if ($nodes->length > 0) {
$nodes[0]->attribute(...); #<--only does this if there was at least one video element
}https://stackoverflow.com/questions/13385506
复制相似问题