php - YouTube (API) 流链接

标签 php youtube youtube-api

我正在尝试将 youtube 脚本迁移到 Youtube API 版本 2
有些视频没有 3GP 或 MP4 移动流,而我有

fatal error :在第 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 "";
    }

都抛出错误

fatal error :无法在第 29 行的 C:\wamp\www\view.php 的写入上下文中使用方法返回值

用这个片段
        if ($media->group->content[1]->attributes() == "") { echo ""; }
    else {
    $attrs = $media->group->content[1]->attributes();
    $obj->tgpp = $attrs['url'];
    }

我回来时出错了

fatal error :在第 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>

最佳答案

if(isset($media) and isset($media->group) and isset($media->group->content[1])) {
    $attrs = $media->group->content[1]->attributes();
    $obj->tgpp = $attrs['url'];
}
else {
...

关于php - YouTube (API) 流链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17539125/

相关文章:

php - MySQL select * with distinct id

javascript - 如何在自定义元素或按钮小部件上触发事件?

php - 将 Symfony2 应用程序部署到 AWS Elastic Beanstalk - 部署后缓存清除

youtube - 如何通过在YouTube v3搜索API中传递 channel ID来获取特定 channel 的供稿?

php - Mysql - 数据库中有很多数据

javascript - 删除嵌入式 YouTube 视频上的注释

youtube - YouTube iframe API-无法触发onReady和onStateChanged事件

java - 如何使用 google API 从 Youtube 请求 Creative Common 内容

html - YouTube 缩略图破坏了 maxres 使用

php - 通过访问 token 提取 Youtube 帐户信息?