在 PHP 中获取带有视频 ID 的 YouTube 视频标题
Get YouTube video title with video ID in PHP
我必须使用 PHP 中的 YouTube 视频 ID 获取 YouTube 视频标题。
目前我是这样做的:
$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,我需要一些帮助。
如何获取视频ID在PHP中的视频标题?还有一个
easy-to-follow YouTube 教程 API。
注意:这不是重复的问题。
Stack Overflow 上关于 YouTube API 的其他答案是 2013 年之前的,它们不再有效。
使用 YouTube API v3 和列表方法,将 ID 作为参数发送,您将获得有关该特定视频的信息,https://developers.google.com/youtube/v3/docs/videos/list#id.
带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;
所以我认为您的问题与您的代码以外的其他问题有关。
我用 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');
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 年)这个解决方案对我有用。您需要来自 Google Cloud Platform 的自己的 YouTube API 密钥(或全局 API 密钥)。替换示例中的“yourAPIkey”后:
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;
这是我创建的一行代码:
echo explode('</title>', explode('<title>', file_get_contents("https://www.youtube.com/watch?v=VideoIDHERE"))[1])[0];
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);
}
我必须使用 PHP 中的 YouTube 视频 ID 获取 YouTube 视频标题。
目前我是这样做的:
$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,我需要一些帮助。
如何获取视频ID在PHP中的视频标题?还有一个 easy-to-follow YouTube 教程 API。
注意:这不是重复的问题。
Stack Overflow 上关于 YouTube API 的其他答案是 2013 年之前的,它们不再有效。
使用 YouTube API v3 和列表方法,将 ID 作为参数发送,您将获得有关该特定视频的信息,https://developers.google.com/youtube/v3/docs/videos/list#id.
带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;
所以我认为您的问题与您的代码以外的其他问题有关。
我用 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');
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 年)这个解决方案对我有用。您需要来自 Google Cloud Platform 的自己的 YouTube API 密钥(或全局 API 密钥)。替换示例中的“yourAPIkey”后:
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;
这是我创建的一行代码:
echo explode('</title>', explode('<title>', file_get_contents("https://www.youtube.com/watch?v=VideoIDHERE"))[1])[0];
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);
}