How to make calls that require to be Authenticated in PHP

How I make a call to a non authenticated API request would be something like this in PHP.
$URLResult = file_get_contents(“https://api.twitch.tv/kraken?oauth_token=”.$token);
$decodedURL = json_decode($URLResult, true);
return $decodedURL[‘token’][‘user_name’];

Even though I read how to send the access token:
curl https://api.twitch.tv/kraken?oauth_token=[access token]

Im not sure how I would connect them to make a call for something like:
curl -H ‘Accept: application/vnd.twitchtv.v3+json’ -H ‘Authorization: OAuth <access_token>’
-X GET https://api.twitch.tv/kraken/channel

I don’t work directly with cURL but I believe those two methods are independent of each other - You can make an API call either via PHP, PHP w/ cURL Library, or cURL (command line).

The PHP code you listed should work as long as the oauth has proper scope for what you are trying to pull.

An example of PHP w/ cURL Library is in the Client ID Blog :

<?php
 $channelsApi = 'https://api.twitch.tv/kraken/channels/';
 $channelName = 'twitch';
 $clientId = 'axjhfp777tflhy0yjb5sftsil';
 $ch = curl_init();
 
 curl_setopt_array($ch, array(
    CURLOPT_HTTPHEADER => array(
       'Client-ID: ' . $clientId
    ),
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_URL => $channelsApi . $channelName
 ));
 
 $response = curl_exec($ch);
 curl_close($ch);
?>

The example you posted,

curl -H 'Accept: application/vnd.twitchtv.v3+json' -H 'Authorization: OAuth ' \
-X GET https://api.twitch.tv/kraken/channel

is an example of command-line cURL.

So, if you are looking to include cURL in your PHP then you best bet is to focus on the including the cURL library and going with those examples.

What information are you trying to pull or what are you trying to do in the end? Some direction might also help someone else if they have better help to give than I. If you are just looking to make authenticated calls, the code included should suffice.

On a side note, was there anything wrong with your initial PHP statement to pull desired info or is there a technical reason you are looking to include cURL?

Your code works for me to pull a users email using OAUTH with user_read scope (modified slightly as I don’t have your code that engages it – ClientID and OAUTH are interchangeable up until you are trying to access information that a user needs to give the OK):

<?php
$token = "CLIENTIDHERE";
$URLResult = file_get_contents("https://api.twitch.tv/kraken/user?oauth_token=".$token);
$decodedURL = json_decode($URLResult, true);
echo $decodedURL['email'];

?>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.