获得YouTube视频的标题

时间:2011-08-21 22:02:16

标签: php youtube-api

我正在使用以下代码获取某些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:解析器错误:第214行/home/public_html/panel/index.php中的文档为空

     

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

     

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

     

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

     

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

     

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

3 个答案:

答案 0 :(得分:2)

这是我的方式。

使用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

答案 1 :(得分:0)

您是否可以尝试使用以下功能将来电替换为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);

P.S。当我从您的网址中删除:1时,会在浏览器中访问该文件时加载该文件。

答案 2 :(得分:0)

我的方式:

功能

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); 
}

$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