php - 检索YouTube channel 名称,头像和订阅者

标签 php youtube youtube-api

我有连接到YouTube的代码,然后填写表格,然后提交到数据库。我正在努力寻找YouTube channel 的名称,头像和订阅者。我已经可以获取 channel ID,并可以查看最近30天的内容:只需要有关头像,姓名和订阅者的帮助。以下是我最近30天获取ID和查看次数的代码。

function get_access_token($authentication_code,$client_id,$client_secret,$redirect_uris){
  $url = "https://accounts.google.com/o/oauth2/token";
  $post="code={$authentication_code}&client_id={$client_id}&client_secret={$client_secret}&redirect_uri={$redirect_uris}&grant_type=authorization_code";
     $ch = curl_init();
     curl_setopt($ch, CURLOPT_URL, $url);
     curl_setopt($ch, CURLOPT_POST, true);
     curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
   curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);  
     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);  
     curl_setopt($ch, CURLOPT_COOKIEJAR,'cookie.txt');  
     curl_setopt($ch, CURLOPT_COOKIEFILE,'cookie.txt');  
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);  
     curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.3) Gecko/20070309 Firefox/2.0.0.3");  
   $st=curl_exec($ch);  
   return $result=json_decode($st,TRUE);   
   }

   function get_curl($url){
      $ch = curl_init();
      $headers = array("Content-type: application/json");
     curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
       curl_setopt($ch, CURLOPT_URL, $url);
     curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);  
       curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);  
       curl_setopt($ch, CURLOPT_COOKIEJAR,'cookie.txt');  
       curl_setopt($ch, CURLOPT_COOKIEFILE,'cookie.txt');  
       curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);  
       curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.3) Gecko/20070309 Firefox/2.0.0.3");   
     $st=curl_exec($ch);  
    return $result=json_decode($st,TRUE);
   }
   function get_id($url){
      $ch = curl_init();
      $headers = array("Content-type: application/json");
     curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
       curl_setopt($ch, CURLOPT_URL, $url);
     curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);  
       curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);  
       curl_setopt($ch, CURLOPT_COOKIEJAR,'cookie.txt');  
       curl_setopt($ch, CURLOPT_COOKIEFILE,'cookie.txt');  
       curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);  
       curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.3) Gecko/20070309 Firefox/2.0.0.3");   
     $st=curl_exec($ch);  
    return $st;
   }



if(isset($_GET['code'])){
  $authentication_code=$_GET['code'];
}

if ($access_token=='' && $authentication_code!=''){
  /******Get The authorization Code/Access Token******/
  $result=get_access_token($authentication_code,$client_id,$client_secret,$redirect_uris);
  /***Take access token, also there is the expiration duration*****/
  $access_token=$result['access_token'];

  $refresh_token=$result['refresh_token'];
}

if(!$access_token){
  $auth_url="https://accounts.google.com/o/oauth2/auth";
  $message = "<form id=form-login action='{$auth_url}' method=get>
  <input type=hidden name=client_id value=$client_id>
  <input type=hidden name=redirect_uri value=$redirect_uris>
  <input type=hidden name=scope value=\"https://www.googleapis.com/auth/yt-analytics.readonly https://www.googleapis.com/auth/youtube.readonly\">
  <input type=hidden name=response_type value=code>
  <input type=hidden name=access_type value=offline>
  <div style=\"float: right;\" data-sr=\"enter bottom\" ><button class=\"button\" type=submit>Apply Now</button></div></form>";
} else {

  $end_date=date('Y-m-d');
  $start_date=date('Y-m-d',strtotime("-1 months"));

   $gcid="https://www.googleapis.com/youtube/v3/channels?part=id&mine=true&access_token={$access_token}";
   $channelfind=get_id($gcid);

   $s="https://www.googleapis.com/youtube/analytics/v1/reports?ids=channel%3D%3DMINE&start-date={$start_date}&end-date={$end_date}&metrics=views,comments,likes&dimensions=day&sort=day&access_token={$access_token}";
   $view_data=get_curl($s);



   $total_view=0;
   foreach($view_data['rows'] as $v){
    $total_view+=$v[1];
   }  //MONTHLY VIEWS NUMBER

   $channelid = explode('id": "', $channelfind);
   $channelid = explode('"
  }
 ]
}', $channelid[1]);
   $channelid = $channelid[0]; //CHANNELID

   $_SESSION['Channelid'] = $channelid;
   $_SESSION['access_token'] = $access_token;
   $_SESSION['Views'] = $total_view;
   header("location: index.php");

  }


  echo $message;

   ?>

有什么建议么?

最佳答案

Subscriber count是 channel 统计信息的一部分。

如果要获取订阅者数量,只需在请求中指定statistics并获取subscriberCount即可。

关于php - 检索YouTube channel 名称,头像和订阅者,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31733280/

相关文章:

php - 调用数组上的成员函数失败()

php - Laravel 队列通过 Redis : local. 错误 : read error on connection to 127. 0.0.1:6379

javascript - YouTube API - onPlayerStateChange

php - 使用 youtube api v3 获取当前上传视频的 ID

youtube - 是否可以在Web Audio的MediaElementAudioSourceNode中获取YouTube音频流?

php - Yii2:Yii::$app->request->baseUrl 在 Linux 中不工作

php - 在 laravel 中路由未定义错误,即使路由已定义

jquery - Youtube 视频无法在 Flexslider 中播放

android - 如何从默认应用程序播放YouTube视频

python - 为 Youtube 自动生成的字幕自动打开脚本