【问题标题】:Get YouTube video title in PHP用 PHP 获取 YouTube 视频标题
【发布时间】:2012-03-28 16:47:46
【问题描述】:

在我的一个应用程序中,我正在保存一个 YouTube 视频的 ID...例如“A4fR3sDproE”。我必须在应用程序中显示它的标题。我得到了以下代码来获取它的标题,并且它工作正常。

现在的问题是如果发生任何错误(在删除视频的情况下)PHP 会显示错误。我只是添加了一个条件。但它仍然显示错误。

foreach($videos as $video) {

    $video_id = $video->videos;
    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";

    $i++;
}

return $myvideos;

如果发生错误,它会因以下情况而死:

严重性:警告

消息:file_get_contents(http://youtube.com/get_video_info?video_id=A4fR3sDprOE) [function.file-get-contents]:打开流失败:HTTP 请求失败! HTTP/1.0 402 需要付款

文件名:models/webs.php

行号:128

【问题讨论】:

    标签: php youtube-api youtube-javascript-api


    【解决方案1】:

    file_get_contents() 与远程 URL 一起使用是不安全的。在 YouTube API 2.0 中使用 cURL:

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'http://gdata.youtube.com/feeds/api/videos/' . $video_id);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    
    $response = curl_exec($ch);
    curl_close($ch);
    
    if ($response) {
        $xml   = new SimpleXMLElement($response);
        $title = (string) $xml->title;
    } else {
        // Error handling.
    }
    

    【讨论】:

      【解决方案2】:

      这是我的解决方案。很短。

      $id = "VIDEO ID";
      $videoTitle = file_get_contents("http://gdata.youtube.com/feeds/api/videos/${id}?v=2&fields=title");
      
      preg_match("/<title>(.+?)<\/title>/is", $videoTitle, $titleOfVideo);
      $videoTitle = $titleOfVideo[1];
      

      【讨论】:

      • 它背后的想法是什么?
      【解决方案3】:

      在 file_get_contents 之前使用error control operators 可能会起作用。

      类似:

      if($content = @file_get_contents("http://youtube.com/get_video_info?video_id=" . $video_id))
      

      它应该删除错误并使用它在您的 if 语句中返回 false。

      否则你可以只使用 try/catch 语句(见 Exceptions):

      try{
          // Code
      }
      catch (Exception $e){
          // Else code
      }
      

      【讨论】:

        【解决方案4】:

        我相信您的托管服务提供商出于安全目的禁用了 file_get_contents。你应该使用cURL

        <?php
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, "http://youtube.com/get_video_info?video_id=" . $video_id);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            $response = curl_exec($ch);
            curl_close($ch);
        
            /* Parse YouTube's response as you like below */
        ?>
        

        【讨论】:

          【解决方案5】:

          试试:

          // Use @ to suppress warnings
          $content = @file_get_contents("http://youtube.com/get_video_info?video_id=" . $video_id);
          if($content===FALSE) {
              .. Handle error here
          }
          else{
              ..The rest of your code
          }
          

          【讨论】:

            【解决方案6】:

            我想补充一点,此处看到的特定错误(HTTP 402 - 需要付款,否则是服务器通常未实现的 HTTP 状态代码)是 YouTube 在确定“过多”的流量来自您的 IP 地址(或 IP 地址范围——他们更喜欢经常阻止 OVH 的 IP 地址范围[1][2]

            >

            因此,如果您以编程方式(使用 PHP 或其他方式)访问 youtube.com(而不是 API),您最终可能会遇到此错误。 YouTube 通常从向来自“过多流量”IP 地址的请求提供验证码开始,但如果您没有完成它们(您的 PHP 脚本不会),他们将切换到这些无用的 402 响应,基本上没有追索权 - YouTube没有确切的客户支持服务台可以打电话,如果由于 IP 地址阻止您实际上无法访问他们的任何网站,那么您联系他们的机会就更少了。

            其他参考:
            http://productforums.google.com/forum/#!topic/youtube/tR4WkNBPnUo http://productforums.google.com/forum/?fromgroups=#!topic/youtube/179aboankVg

            【讨论】:

              【解决方案7】:

              我的解决办法是:

              $xmlInfoVideo = simplexml_load_file("http://gdata.youtube.com/feeds/api/videos/" . $videoId . "?v=2&fields=title");
              
              foreach($xmlInfoVideo->children() as $title) {
                  $videoTitle = strtoupper((string) $title); 
              }
              

              这会得到视频的标题。

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2013-03-23
                • 2015-08-09
                • 2017-10-14
                • 2016-01-11
                • 1970-01-01
                • 2015-10-23
                相关资源
                最近更新 更多