2013-05-05 159 views
0

我尝试使用API​​的YouTube V3的卷曲度(我尝试的PHP,没有结果)API的YouTube V3卷曲PHP

这样我就可以连接API我的帐户,我收集accesToken。

esay部分。 现在我想用她的,我走在DOC https://developers.google.com/youtube/v3/docs/channels/list

,我想我与此ADRESS频道信息:

GET https://www.googleapis.com/youtube/v3/channels?part=statistics&mine=true&key={YOUR_API_KEY} 

Authorization: Bearer ya29.AHES6ZRgKyWjPn_gwkMGDuI5N1Yzu4MmUPXTGybTRbox2zLatw 
X-JavaScript-User-Agent: Google APIs Explorer 

所以我用这个代码:

$option = array(
     'part' => 'statistics', 
     'mine' => 'true', 
     'key' => 'AIzaSyBmykey_xZhOR22u9txLyU6Yc' 
    ); 
$url = "https://www.googleapis.com/youtube/v3/channels" 
    $curl = curl_init($url); 

    curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY); 
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true); 
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'GET'); 
    curl_setopt($curl, CURLOPT_POST, 1); 
    curl_setopt($curl, CURLOPT_POSTFIELDS, $option); 
    $curlheader[0] = "Authorization: Bearer " . $accessToken; 
    curl_setopt($curl, CURLOPT_HTTPHEADER, $curlheader); 

    $json_response = curl_exec($curl); 

    curl_close($curl); 

    $responseObj = json_decode($json_response); 

但我对错误(无结果)也许在参数上的错误,如:

?part=statistics&mine=true&key={YOUR_API_KEY} with curl 

如果有人能帮助我。

+1

你好,我尝试做几乎相同的事情,但是我如何获得你在授权旁边的$ accessToken:承载者 – relipse 2016-04-04 19:13:34

回答

1

首先,你在这一行有一个语法错误:

$url = "https://www.googleapis.com/youtube/v3/channels" 

它应该是:

$url = "https://www.googleapis.com/youtube/v3/channels"; 

现在,如果固定没有帮助,你告诉卷曲做到既GET和POST。它可能只是为了更好地得到这样的:

$option = array(
     'part' => 'statistics', 
     'mine' => 'true', 
     'key' => 'AIzaSyBmykey_xZhOR22u9txLyU6Yc' 
    ); 
$url = "https://www.googleapis.com/youtube/v3/channels?".http_build_query($option, 'a', '&'); 
$curl = curl_init($url); 

curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY); 
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true); 
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 
$curlheader[0] = "Authorization: Bearer " . $accessToken; 
curl_setopt($curl, CURLOPT_HTTPHEADER, $curlheader); 

$json_response = curl_exec($curl); 

curl_close($curl); 

$responseObj = json_decode($json_response); 
0

我有同样的问题,它不会工作,直到我上传到我的服务器(而不是我的地方),然后它开始工作。从本地主机访问时,API密钥不起作用。

1

这里说的工作代码:

$option = array(
     'part' => 'statistics', 
     'mine' => 'true', 
     'key' => 'AIzaSyBmykey_xZhOR22u9txLyU6Yc' 
    ); 
    $url = "https://www.googleapis.com/youtube/v3/channels?".http_build_query($option, 'a', '&'); 
    $curl = curl_init($url); 

    curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY); 
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true); 
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($curl, CURLOPT_REFERER, "http://www.exemple.com"); 

    $json_response = curl_exec($curl); 

    curl_close($curl); 

    $responseObj = json_decode($json_response); 

神奇的是除去身份验证和报头,并使用引荐(如果你有restictions设置你的应用程序)。