【问题标题】:Get YouTube video title with video ID in PHP在 PHP 中获取带有视频 ID 的 YouTube 视频标题
【发布时间】:2016-01-11 16:25:03
【问题描述】:

我必须使用 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_ALLdisplay_errors设置为1。

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

如何在 PHP 中获取带有视频 ID 的视频标题?还有一个 易于理解的 YouTube API 教程。

注意:这不是重复的问题

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

【问题讨论】:

标签: php video youtube-api


【解决方案1】:

使用 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,
));

【讨论】:

  • @Slowmove 我让它以另一种方式工作,但您的回答对我达成该解决方案有很大帮助。感谢您的努力。 :) 愿上帝保佑你!!
【解决方案2】:

这对我有用:

$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;

所以我认为您的问题与您的代码以外的其他问题有关。

【讨论】:

    【解决方案3】:

    我可以使用 file_get_contents(),但这次我使用了不同的 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');
    

    【讨论】:

      【解决方案4】:
      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));
      

      【讨论】:

      • 返回 SSL 是必需的
      【解决方案5】:

      我在没有 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 或不安全的 localhost 中,在 curl_exec 之前再添加一行:

      curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
      

      【讨论】:

        【解决方案6】:
        $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;
        

        【讨论】:

        • $video_info = file_get_contents('youtube.com/get_video_info?video_id=' .end($video_formulate)); parse_str($video_info, $video_info_array); $title = json_decode($video_info_array['player_response']); print_r($title->videoDetails->title);退出;
        【解决方案7】:

        这是我创建的一行代码:

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

        【讨论】:

        • 奖励 - 如果你想要 Youtube 左侧 1 行标题代码(使用这个): echo explode(' - YouTube',explode('&lt;/title&gt;',explode('&lt;title&gt;',file_get_contents("https://www.youtube.com/watch?v=VideoIDHERE"))[1])[0])[0];
        • 解释一下。想法是什么?它是如何工作的?它有什么作用?为什么它使用echo
        • 没问题,但是如何去掉标题中的“-YouTube”呢?它出现在每个标题上。
        【解决方案8】:

        PHP sn-p

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

        【讨论】:

        • 解释一下。想法是什么?它是如何工作的?它有什么作用?为什么它使用echo
        猜你喜欢
        • 2012-05-22
        • 2017-07-01
        • 1970-01-01
        • 1970-01-01
        • 2012-04-15
        • 2012-03-28
        • 2016-10-31
        • 2015-07-26
        • 2011-03-24
        相关资源
        最近更新 更多