Php – API YouTube V3 curl php

curloptionsphpyoutube-api

I try to use API YouTube V3 with curl (i try php, no result)

So i can connect my account with the api and i collect the accesToken.

The esay part.
Now i want to use her, i go on the doc https://developers.google.com/youtube/v3/docs/channels/list

And i want information on my channel with this 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

So i use this code :

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

But i have on erro (no result) Maybe the error on the parameter like :

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

If someone can help me.

Best Solution

Here the code that works:

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

The magic is remove auth and header and use referer (if you set your app with restictions).