I am using PHP to try to see if a streamer is live on the page load. I am using an APP key to do this.
Here is the code:
error_reporting(E_ALL);
$clientID = "client"; // from the dashboard
$secret = "secret"; // from the dashboard
$twitchName = "Keegers02";
// See if the streamer is live... I hope :)
$opts = array(
'https' => array(
'method' => "GET",
'header' => "Authorization: Bearer $secret\r\n" .
"Client-Id: $clientID"
),
'http' => array(
'method' => "GET",
'header' => "Authorization: Bearer $secret\r\n" .
"Client-Id: $clientID"
)
);
$context = stream_context_create($opts);
$getStreamersRaw = file_get_contents("https://api.twitch.tv/helix/streams?user_login=$twitchName", false, $context);
print_r($getStreamersRaw);
When running this locally I get 401 unauthorized (yes on the dashboard I have added http://localhost to the list of Redirect URIs). Remotely I get absolutely nothing, so I am more trusting the local process right now.
I have tried multiple versions of the code above (including, as you can see breaking apart http and https), but the results are the same.
I ran the command given here: Twitch API Concepts | Twitch Developers
for Windows and got the access token. I replaced the secret with that token, and I am still getting Unauthorized.
So I assumed you did the secret from the dashboard.
Whats the body message of the response.
The HTTP Code is half the information
You might want to convert your code from file_get_contents to curl like my linked example, most hosts block first_get_contents from network operations.
No a hosting provider will not the api provdered being called
thats concerning you should get a descriptive json blob.
but having never used file_get_contents to make http requests I dont know what happens on http error
Best advice is to convert your code to curl it’ll be easier to work with