获得YouTube视频标题与视频ID在PHP


Get YouTube video title with video ID in PHP

我必须在PHP中获得YouTube视频标题和YouTube视频ID。

目前我是这样做的:

    $html = "http://www.youtube.com/watch?v=" . $video_id;
    $doc = new DOMDocument();
    $doc->loadHTMLFile($html);
    $doc->preserveWhiteSpace = false;
    $title_div = $doc->getElementById('eow-title');
    $title = $title_div->nodeValue;

但是这会抛出一个错误:

Warning: DOMDocument::loadHTMLFile(http://www.youtube.com/watch?v=hMpCsfvi_3c): failed to open stream: HTTP request failed! HTTP/1.0 429 Too Many Requests in /home/vhosts/xxxx/xxxx/get_youtube_title.php on line 6
Warning: DOMDocument::loadHTMLFile(): I/O warning : failed to load external entity "http://www.youtube.com/watch?v=hMpCsfvi_3c" in /home/vhosts/xxxx/xxxx/get_youtube_title.php on line 6

以上方法适用于GoDaddy托管,但不适用于101domain.com托管。

我尝试过的另一种方法是:

if($content = file_get_contents("http://youtube.com/get_video_info?video_id=" . $video_id)) {
    parse_str($content, $ytarr);
    $myvideos[$i]['video_title'] = $ytarr['title'];
}
else
    $myvideos[$i]['video_title'] = "No title";

抛出错误:

Warning: DOMDocument::loadHTMLFile(http://youtube.com/get_video_info?video_id=hMpCsfvi_3c): failed to open stream: HTTP request failed! HTTP/1.0 402 Payment Required in /home/vhosts/xxxx/get_youtube_title.php on line 6
Warning: DOMDocument::loadHTMLFile(): I/O warning : failed to load external entity "http://youtube.com/get_video_info?video_id=hMpCsfvi_3c" in /home/vhosts/xxxx/get_youtube_title.php on line 6

我尝试的最后一个方法是:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://youtube.com/get_video_info? video_id=" . $video_id);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);

这不会抛出任何错误,但也不能工作。错误报告为E_ALL,且display_errors设置为1

有人告诉我使用YouTube API更安全。由于我是YouTube API的新手,我需要一些帮助。

如何在PHP中获得视频ID的视频标题?也是一个YouTube API教程:

注意:这不是重复的问题

Stack Overflow上关于YouTube API的其他答案是2013年之前的,它们不再工作

我得到了它的工作与file_get_contents(),但我使用了一个不同的URL这次

我使用的URL是:

https://www.googleapis.com/youtube/v3/videos?id=' . $video_id . '&key=' . $YoutubeAPIKey . '&part=snippet
例如:

function get_youtube_title($video_id){
    $html = 'https://www.googleapis.com/youtube/v3/videos?id=' . $video_id . '&key=alskdfhwueoriwaksjdfnzxcvxzfserwesfasdfs&part=snippet';
    $response = file_get_contents($html);
    $decoded = json_decode($response, true);
    foreach ($decoded['items'] as $items) {
         $title = $items['snippet']['title'];
         return $title;
    }
}
echo $title = get_youtube_title('PQqudiUdGuo');

下面是我创建的一行代码:

echo explode('</title>', explode('<title>', file_get_contents("https://www.youtube.com/watch?v=VideoIDHERE"))[1])[0];

使用YouTube API v3和list方法,发送ID作为参数,您将获得有关该特定视频的信息,https://developers.google.com/youtube/v3/docs/videos/list#id

得到https://www.googleapis.com/youtube/v3/videos

带有id参数

string

id参数指定要检索的资源的YouTube视频id的逗号分隔列表。在视频资源中,id属性指定视频的id。

在使用PHP SDK的情况下,示例类似于:

# Call the videos.list method to retrieve location details for each video.
$videosResponse = $youtube->videos->listVideos('snippet, recordingDetails', array(
'id' => $videoIds,
));

这对我有用:

$video_id = ...;
$url = "http://www.youtube.com/watch?v=" . $video_id;
$page = file_get_contents($url);
$doc = new DOMDocument();
$doc->loadHTML($page);
$title_div = $doc->getElementById('eow-title');
$title = $title_div->nodeValue;
var_dump($title);
exit;

这也是有效的(这基本上是相同的你的第一个方法):

$url = "http://www.youtube.com/watch?v=" . $video_id;
$doc = new DOMDocument();
$doc->preserveWhiteSpace = FALSE;
$doc->loadHTMLFile($url);
$title_div = $doc->getElementById('eow-title');
$title = $title_div->nodeValue;
var_dump($title);
exit;

所以我认为你的问题与你的代码以外的东西有关。

function get_youtube($url){
    $youtube = "http://www.youtube.com/oembed?url=" . $url . "&format=json";
    $curl = curl_init($youtube);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    $return = curl_exec($curl);
    curl_close($curl);
    return json_decode($return, true);
}
$url = // YouTube video URL
// Display Data
print_r(get_youtube($url));

我尝试了以前没有PHP SDK的解决方案,它们不适合我。如果我尝试file_get_contents,我从API得到一个错误。

我检查了YouTube API v3,现在(2019)这个解决方案适用于我。你需要自己的YouTube API密钥(或全局API密钥)从谷歌云平台。更换"你的apikey"后在示例中:

function get_youtube_title($video_id){
  $url = 'https://www.googleapis.com/youtube/v3/videos?part=snippet&id=' . $video_id . '&key=yourAPIkey';
  $curl = curl_init($url);
  curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  $data = curl_exec($curl);
  curl_close($curl);
  $decoded = json_decode($data, true);
  foreach ($decoded['items'] as $items) {
     $title= $items['snippet']['title'];
     return $title;
  }
}
echo $title = get_youtube_title('fhntQrZsjdw');

如果你从HTTPS发送请求,这将工作。在HTTP或不安全的本地主机上,在curl_exec:

之前多放一行
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
$video_info = file_get_contents('https://www.youtube.com/get_video_info?video_id=' . $video_id);
parse_str($video_info, $video_info_array);
$title = json_decode($video_info_array['player_response'])->videoDetails->title;

FOR Youtube:使用此代码,无需APIkey

即可获得视频标题。

$id_video='1OlEupMZQyo';// <---id_video example
echo $title = get_title_youtube($id_video);
function get_title_youtube($id_video){
$code_html = html_entity_decode( file_get_contents("https://www.youtube.com/watch?v=".$id_video) );
$search_string_title = '"title":{"runs":[{"text":"';
$found_string = strpos_recursive($code_html, $search_string_title);
if($found_string) {
    foreach($found_string as $string_position ) {
        $string_title = substr($code_html, $string_position+26, 100); 
        $title = explode('"', $string_title);
        $title=$title[0];
        break;
    }   
} 
return $title;
}

PHP代码段

function getTubeTitel($id)
{
    $tmp = file_get_contents("http://youtube.com/watch?v=" . $id);
    $tmp2 = substr($tmp, (strpos($tmp, "<title>") + 7), 220);
    $tmp = substr($tmp2, 0, strpos($tmp2, "</title>") - 9);
    echo htmlspecialchars_decode($tmp);
}