401 Error by this Code?


#1
<?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?


#2

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


#3

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!


#4

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


#5

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” }


#6
<?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));

?>

#7

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


#8

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:

You need to convert the usernames to userID’s


#9

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

?>

#10

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? you put a call into the channels API not the streams API

#11

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

#12

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";

?>

#13

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

Pardon?


#14

oh okay, any other way?


#15

I already answered here


#16

thanks!


closed #17

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