获取 YouTube 视频的标题

发布于 2024-11-30 20:10:51 字数 1316 浏览 0 评论 0原文

我正在使用以下代码来获取一些 YouTube 视频的标题,该视频以前运行良好,但自最近几天以来我收到了一些错误/警告,

$entry = simplexml_load_file('http://gdata.youtube.com/feeds/api/videos/' . $key);
return ($entry) ? ucwords(strtolower($entry->children('http://search.yahoo.com/mrss/')->group->title)) : false;

警告:simplexml_load_file() [function.simplexml-load-file]:http ://gdata.youtube.com/feeds/api/videos/jIP4xI9C8us:1:解析器错误:文档为空/home/public_html/panel/index.php 第 214 行

警告:simplexml_load_file() [function.simplexml-load-file]:位于 /home/public_html/panel/index.php 第 214 行

警告:simplexml_load_file() [function.simplexml-load-file]: ^ 在 /home/public_html/panel/index.php 第 214 行

警告:simplexml_load_file() [function.simplexml-load-file]:http ://gdata.youtube.com/feeds/api/videos/jIP4xI9C8us:1:解析器错误:需要开始标记,“<”在 /home/public_html/panel/index.php 第 214 行中找不到

警告:simplexml_load_file() [function.simplexml-load-file]:位于 /home/public_html/panel/index.php 第 214 行

警告:simplexml_load_file() [function.simplexml-load-file]: ^ 在 /home/public_html/panel/index.php 第 214 行

I am using following code to get title of some YouTube video, which was working fine previously but I am getting some error/warnings since last few days,

$entry = simplexml_load_file('http://gdata.youtube.com/feeds/api/videos/' . $key);
return ($entry) ? ucwords(strtolower($entry->children('http://search.yahoo.com/mrss/')->group->title)) : false;

Warning: simplexml_load_file() [function.simplexml-load-file]: http://gdata.youtube.com/feeds/api/videos/jIP4xI9C8us:1: parser error : Document is empty in /home/public_html/panel/index.php on line 214

Warning: simplexml_load_file() [function.simplexml-load-file]: in /home/public_html/panel/index.php on line 214

Warning: simplexml_load_file() [function.simplexml-load-file]: ^ in /home/public_html/panel/index.php on line 214

Warning: simplexml_load_file() [function.simplexml-load-file]: http://gdata.youtube.com/feeds/api/videos/jIP4xI9C8us:1: parser error : Start tag expected, '<' not found in /home/public_html/panel/index.php on line 214

Warning: simplexml_load_file() [function.simplexml-load-file]: in /home/public_html/panel/index.php on line 214

Warning: simplexml_load_file() [function.simplexml-load-file]: ^ in /home/public_html/panel/index.php on line 214

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

请你别敷衍 2024-12-07 20:10:51

这是我的方法。

使用 Youtube Data API v3.0

$searchResponse = $youtube->videos->listVideos('id','snippet,statistics',
        array('id' => 'CSV_VIDEO_IDS_HERE',
              'fields' => 'items(id,snippet(title,publishedAt,channelId,channelTitle,thumbnails(default),description),statistics)'
    ));

foreach($searchResponse["items"] as $item)
{
    echo "video titles: ".$item['snippet']['title'];
}

google-api-php-client 库

here's my way.

use Youtube Data API v3.0

$searchResponse = $youtube->videos->listVideos('id','snippet,statistics',
        array('id' => 'CSV_VIDEO_IDS_HERE',
              'fields' => 'items(id,snippet(title,publishedAt,channelId,channelTitle,thumbnails(default),description),statistics)'
    ));

foreach($searchResponse["items"] as $item)
{
    echo "video titles: ".$item['snippet']['title'];
}

google-api-php-client library

回梦 2024-12-07 20:10:51

您可以尝试用以下函数替换对 simplexml_load_file 的调用,看看是否存在相同的问题?听起来您的主机可能更改了一些 PHP 安全设置。

function load_youtube_xml($url) {
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_REFERER, 'http://www.YourWebSiteName.com/');
    $response = curl_exec($curl);
    curl_close($curl);
    return $response;
}
$xml = load_youtube_xml(YOUTUBE_URL);

PS 当我从您的 URL 中删除 :1 时,它会在浏览器中加载该文件。

Can you try replacing your call to simplexml_load_file with the following function and see if it has the same issue? Sounds like your host may have changed some PHP security settings.

function load_youtube_xml($url) {
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_REFERER, 'http://www.YourWebSiteName.com/');
    $response = curl_exec($curl);
    curl_close($curl);
    return $response;
}
$xml = load_youtube_xml(YOUTUBE_URL);

P.S. When I remove :1 from your URL, it loads the file up when going there in a browser.

樱花细雨 2024-12-07 20:10:51

我的方法:

函数

function getVideoInfo($videoID){
    if($videoID == ""){
        return false;   
    }
    /* Get the xml file from YouTube Data API */
    $books = simplexml_load_file('http://gdata.youtube.com/feeds/api/videos/'.$videoID);
    $user = $books->author[0]->name;
    $desc = $books->content[0]; 
    $title = $books->title[0];
    $link = 'http://www.youtube.com/watch?v='.$videoID;

    return array($user, $title, $desc, $link); 
}

main

$info = getVideoInfo($YourVideoID);
    echo $info[0];  //username
    echo '<br />';
    echo $info[1];  //title
    echo '<br />';
    echo $info[2];  //description
    echo '<br />';
    echo $info[3];  //link

My way:

function

function getVideoInfo($videoID){
    if($videoID == ""){
        return false;   
    }
    /* Get the xml file from YouTube Data API */
    $books = simplexml_load_file('http://gdata.youtube.com/feeds/api/videos/'.$videoID);
    $user = $books->author[0]->name;
    $desc = $books->content[0]; 
    $title = $books->title[0];
    $link = 'http://www.youtube.com/watch?v='.$videoID;

    return array($user, $title, $desc, $link); 
}

main

$info = getVideoInfo($YourVideoID);
    echo $info[0];  //username
    echo '<br />';
    echo $info[1];  //title
    echo '<br />';
    echo $info[2];  //description
    echo '<br />';
    echo $info[3];  //link
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文