Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/google-sheets/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 回顾YouTube频道名称、头像和订户_Php_Youtube_Youtube Api - Fatal编程技术网

Php 回顾YouTube频道名称、头像和订户

Php 回顾YouTube频道名称、头像和订户,php,youtube,youtube-api,Php,Youtube,Youtube Api,我有一个代码连接到YouTube,然后填写表格,然后提交到数据库。我在努力检索YouTube频道名称、头像和订户。我已经可以检索频道ID和过去30天的浏览量:只需要有关头像、姓名和订户的帮助。下面是我过去30天获取ID和视图的代码 function get_access_token($authentication_code,$client_id,$client_secret,$redirect_uris){ $url = "https://accounts.google.com/o/oaut

我有一个代码连接到YouTube,然后填写表格,然后提交到数据库。我在努力检索YouTube频道名称、头像和订户。我已经可以检索频道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;

   ?>
函数获取访问令牌($authentication\u code、$client\u id、$client\u secret、$redirect\u uris){
$url=”https://accounts.google.com/o/oauth2/token";
$post=“code={$authentication\u code}&client\u id={$client\u id}&client\u secret={$client\u secret}&redirect\u uri={$redirect\u uris}&grant\u type=authorization\u 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);
返回$result=json_decode($st,TRUE);
}
函数get\u curl($url){
$ch=curl_init();
$headers=array(“内容类型: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);
返回$result=json_decode($st,TRUE);
}
函数get_id($url){
$ch=curl_init();
$headers=array(“内容类型: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);
返回$st;
}
如果(isset($_GET['code'])){
$authentication\u code=$\u GET['code'];
}
if($access\u token=''&$authentication\u code!=''){
/******获取授权代码/访问令牌******/
$result=get_access_token($authentication_code、$client_id、$client_secret、$redirect_uris);
/***获取访问令牌,还有到期时间*****/
$access_token=$result['access_token'];
$refresh_token=$result['refresh_token'];
}
if(!$access\u令牌){
$auth_url=”https://accounts.google.com/o/oauth2/auth";
$message=”
“立即申请”;
}否则{
$end_date=日期('Y-m-d');
$start_date=日期('Y-m-d',标准时间('1个月”);
$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-日期={$start\u date}&end date={$end\u date}&metrics=views、comments、likes&dimensions=day&sort=day&access\u token={$access\u token}”;
$view\u data=get\u curl($s);
$total_view=0;
foreach($v形式的视图_数据['rows']{
$total_view+=$v[1];
}//每月浏览次数
$channelid=explode('id:',$channelfind);
$channelid=分解(''
}
]
}“,$channelid[1]);
$channelid=$channelid[0];//channelid
$\会话['Channelid']=$Channelid;
$\u会话['access\u token']=$access\u token;
$\u会话['Views']=$total\u视图;
标题(“location:index.php”);
}
回声$信息;
?>
有什么建议吗?

是频道统计信息的一部分

如果要获取订阅者数量,只需在请求中指定
统计信息
,并获取
订阅者计数

是频道统计信息的一部分


如果你想获得订阅者的数量,你只需要在请求中指定
统计信息
,然后获得
订阅者数量

你是想获得你自己频道的订阅者列表还是其他人的订阅者列表?我不是真的想要一个列表,我只想要数量,它将是针对正在使用youtube频道的用户应用程序您是否正在尝试获取您自己频道或其他人频道的订户列表?我不是真的想要一个列表,我只想要数量,它将由使用该应用程序的youtube频道的用户使用。您是否可以编写一些与我类似的代码,将其设置为$\u会话[susbcribers]您可以编写一些类似于我上面的代码,将其设置为$\u SESSION[susbcribers]