401 Error by this Code?

<?php

 $channelsApi = 'https://api.twitch.tv/kraken/channels/';
 $channelName = 'tracepaladinxxl';
 $clientId = 'xxx';
 $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);

if($response == true) {
echo "Stream is Online";
}

else {
echo "Stream is Offline";
}

echo"</br>";

var_dump(json_decode($response, true));

?>

I get 401 Error, can someone help me?

Make sure you are using v5 kraken, see the migration guide .https://dev.twitch.tv/docs/v5/guides/migration

So my biggest problem is that I can not work without sample PHP codes, do you have maybe an link? So the Docs at Twitch are very complicated for me! I do not know how to include those stuff into php!

So is that an solution: “?api_version=5” adding this into code, but where?

Now I get this Error after adding the API Version!

array(3) { [“error”]=> string(11) “Bad Request” [“status”]=> int(400) [“message”]=> string(60) “Channel identifier “tracepaladinxxl” is in an invalid format” }

<?php

 $channelsApi = 'https://api.twitch.tv/kraken/channels/';
 $channelName = 'tracepaladinxxl';
 $clientId = 'xxx';
 $ch = curl_init();
 $apiVers = '?api_version=5';
 
 curl_setopt_array($ch, array(
    CURLOPT_HTTPHEADER => array(
       'Client-ID: ' . $clientId
    ),
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_URL => $channelsApi . $channelName . $apiVers
 ));
 
 $response = curl_exec($ch);

if($response == true) {
echo "Stream is Online";
}

else {
echo "Stream is Offline";
}

echo"</br>";

var_dump(json_decode($response, true));

?>

So someone has an sample PHP code for the Accept Header?

You already know how, since you have a clientID header…

curl_setopt_array($ch, array(
CURLOPT_HTTPHEADER => array(
   'Client-ID: ' . $clientId,
    'Accept': 'application/vnd.twitchtv.v5+json'
),
CURLOPT_RETURNTRANSFER => true,
CURLOPT_URL => $channelsApi . $channelName . $apiVers
 ));

Also as documented:

https://dev.twitch.tv/docs/v5/guides/migration#username-versus-user-id

You need to convert the usernames to userID’s

So this code does replay data:
<?php

 $channelsApi = 'https://api.twitch.tv/kraken/users?login=';
 $channelName = 'traceapaldinxxl';
 $clientId = 'xxx';
 $ch = curl_init();
 **$apiVers = '&api_version=5';**
 
 curl_setopt_array($ch, array(
    CURLOPT_HTTPHEADER => array(
       'Client-ID: ' . $clientId
    ),
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_URL => $channelsApi . $channelName . **$apiVers**
 ));
 
 $response = curl_exec($ch);

if($response == true) {
echo "Stream is Online";
}

else {
echo "Stream is Offline";
}

echo"</br>";

var_dump(json_decode($response, true));

?>

No.

  1. thats the users API you are calling not the streams API
  2. Remove **$apiVers = '&api_version=5';** completely
  3. Use the same headers I already provided.
  1. I checked traceapaldinxxl doesn’t exist/is not a valid username
  2. In 401 Error by this Code? - #6 by tracepaladinxxl you put a call into the channels API not the streams API

Go use helix instead:

This snippet should suffice

<?php

 $ch = curl_init('https://api.twitch.tv/helix/streams?user_login=tracepaladinxxl');
 
 curl_setopt_array($ch, array(
    CURLOPT_HTTPHEADER => array(
       'Client-ID: ' . $clientId
    ),
    CURLOPT_RETURNTRANSFER => true
 ));
 
 $response = curl_exec($ch);
 $response = json_decode($repsonse);

  if ($response && $response->data && count($response->data) == 1) {
    echo "Stream is Online";
  } else {
    echo "Stream is Offline";
  }

  echo"</br>";

  var_dump($response, true);

thanks but this worked yet:

<?php

 $channelsApi = 'https://api.twitch.tv/helix/streams?user_login=';
 $channelName = 'tracepaladinxxl';
 $clientId = 'xxx';
 $ch = curl_init();
 $apiVers = '&api_version=5';
 
 curl_setopt_array($ch, array(
    CURLOPT_HTTPHEADER => array(
       'Client-ID: ' . $clientId
    ),
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_URL => $channelsApi . $channelName . $apiVers
 ));
 
 $response = curl_exec($ch);
$bX = strlen($response);
if($bX > "27") {
echo "Stream is Online";
}

else {
echo "Stream is Offline";
}

echo"</br>";


var_dump(json_decode($response, true));
echo"</br>";echo"</br>";echo"</br>";

echo "$response";
echo"</br>";echo"</br>";echo"</br>";

echo"</br>";echo"</br>";
echo "$bX";

?>

Thats that worse way to do this sort of check. Doing a length check is “wrong”

Pardon?

oh okay, any other way?

I already answered here

thanks!

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