我尝试将youtube脚本迁移到Youtube API版本2 有些视频没有3GP或MP4移动流,而且我有
致命错误:在第29行的C:\ wamp \ www \ view.php中调用非对象的成员函数attributes()
第29行
/* Get 3GP STREAM URL */
$attrs = $media->group->content[1]->attributes(); /* THIS IS LINE 29 */
$obj->tgpp = $attrs['url'];
我尝试使用empty和isset函数创建一个条件语句来检查3GP / MP4流链接是否可用
if (!empty($media->group->content[1]->attributes())) {
$attrs = $media->group->content[1]->attributes();
$obj->tgpp = $attrs['url'];
} else {
echo "";
}
if (isset($media->group->content[1]->attributes())) {
$attrs = $media->group->content[1]->attributes();
$obj->tgpp = $attrs['url'];
} else {
echo "";
}
都抛出错误
致命错误:无法在第29行的C:\ wamp \ www \ view.php中的写入上下文中使用方法返回值
使用此代码段
if ($media->group->content[1]->attributes() == "") { echo ""; }
else {
$attrs = $media->group->content[1]->attributes();
$obj->tgpp = $attrs['url'];
}
我回来时错误
致命错误:在第29行的C:\ wamp \ www \ view.php中调用非对象的成员函数attributes()
继承剧本
<?php
header("Content-type: text/html; charset=UTF-8");
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
/* Function to parse a video <entry> */
function parseVideoEntry($entry) {
$obj= new stdClass;
/* Get author name and feed URL */
$obj->author = $entry->author->name;
$obj->authorURL = $entry->author->uri;
/* Geet video publish date */
$obj->publish = $entry->published;
/* Get nodes in media: namespace for media information */
$media = $entry->children('http://search.yahoo.com/mrss/');
$obj->title = $media->group->title;
$obj->description = $media->group->description;
/* Get video category */
$attrs = $media->group->category->attributes();
$obj->category = $attrs['label'];
/* Get FLV STREAM URL */
$attrs = $media->group->content[0]->attributes();
$obj->flv = $attrs['url'];
/* Get 3GP STREAM URL */
$attrs = $media->group->content[1]->attributes();
$obj->tgpp = $attrs['url'];
/* Get MP4 STREAM URL */
$attrs = $media->group->content[2]->attributes();
$obj->mp4 = $attrs['url'];
/* Get video thumbnail */
$attrs = $media->group->thumbnail[0]->attributes();
$obj->thumbnailURL = $attrs['url'];
/* Get <yt:duration> node for video length */
$yt = $media->children('http://gdata.youtube.com/schemas/2007');
$attrs = $yt->duration->attributes();
$obj->length = $attrs['seconds'];
/* Get <yt:stats> node for viewer statistics */
$yt = $entry->children('http://gdata.youtube.com/schemas/2007');
if ($yt->statistics)
{
$attrs = $yt->statistics->attributes();
$obj->viewCount = $attrs['viewCount'];
}
else
{
$obj->viewCount = 0;
}
//LIKES get <yt:rating> node for number of likes statistics
//$yt = $entry->children('http://gdata.youtube.com/schemas/2007');
if ($yt->rating)
{
$attrs = $yt->rating->attributes();
$obj->numLikes = $attrs['numLikes'];
}
else
{
$obj->numLikes = 0;
}
//DISLIKES get <yt:rating> node for number of dislikes statistics
//$yt = $entry->children('http://gdata.youtube.com/schemas/2007');
if ($yt->rating)
{
$attrs = $yt->rating->attributes();
$obj->numDislikes = $attrs['numDislikes'];
} else
{
$obj->numDislikes = 0;
}
return $obj;
} // close funcion parseVideoEntry
/* Get video ID from $_GET */
!isset($_GET['id']) ? die ('ERROR: Missing video ID') : $vid = $_GET['id'];
/* Set video data feed URL */
$feedURL = 'http://gdata.youtube.com/feeds/api/videos/'.$vid.'?v=2';
/* Read feed into SimpleXML object */
$entry = simplexml_load_file($feedURL);
/* Parse video entry */
$video = parseVideoEntry($entry);
?>
<!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.0//EN" "http://www.wapforum.org/DTD/xhtml-mobile10.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title><?=$video->title?></title>
<link href="./style.css" rel="stylesheet" type="text/css" />
</head>
<ul>
<li>Stream:</li>
<li><a href="<?=$video->tgpp?>">3GP</a></li>
<li><a href="<?=$video->mp4?>">MP4</a></li>
<li><a href="<?=$video->flv?>">Youtube Player</a></li>
</ul>
答案 0 :(得分:0)
if(isset($media) and isset($media->group) and isset($media->group->content[1])) {
$attrs = $media->group->content[1]->attributes();
$obj->tgpp = $attrs['url'];
}
else {
...