Stream Update API not working

Hello everyone.
I have a website that uses Twitch API to update the streams that are online, viewer count, etc. So from what i understand now we need to add a Client ID to the code. I’ve already tried some solutions suggested on the forum but with no success.
This is my code without the Client ID

<?php
include("_mysql.php");
$mysqli = new mysqli($host, $user, $pwd, $db);

$streams = $mysqli->query("SELECT * FROM ".PREFIX."streams");
while ($ds = $streams->fetch_assoc()) {
    $streamID = $ds['streamID'];
    $channel = $ds['channel'];
    $type = $ds['type'];

    switch($type) {

        case 1: // Twitch Stream API
            $twitch_api = 'https://api.twitch.tv/kraken/streams/'.$channel;
            $twitch_json = json_decode(@file_get_contents($twitch_api));
            if ($twitch_json && $twitch_json->stream) {
                $live = 1;
                $viewers = $twitch_json->stream->viewers;
                $thumb = $twitch_json->stream->preview->large;
            } else {
                $live = 0;
            }
            break;
		case 2: // Hitbox Stream API
            $hitbox_api = 'https://api.hitbox.tv/media/live/'.$channel;
			$hitbox_json = json_decode(@file_get_contents($hitbox_api));
			if ($hitbox_json->livestream[0]->media_is_live == 1) {
				$live = 1;
				$viewers = $hitbox_json->livestream[0]->media_views;
				$thumb = $hitbox_json->livestream[0]->media_thumbnail;
			} else {
				$live = 0;
			}
            break;
			}

    $query = "UPDATE ".PREFIX."streams SET status='".$live."', viewers='".$viewers."', thumb='".$thumb."' WHERE streamID='".$streamID."'";
    $result = $mysqli->query($query);
}
$mysqli->close();
?>

And this is what i’m trying to do

<?php include("_mysql.php"); $mysqli = new mysqli($host, $user, $pwd, $db); $clientID = array( 'Client-ID: ...' );

function file_get_contents_curl($url) {
$ch = curl_init();

curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, $clientID);

$data = curl_exec($ch);
curl_close($ch);

return $data;
}

$streams = $mysqli->query("SELECT * FROM ".PREFIX.“streams”);
while ($ds = $streams->fetch_assoc()) {
$streamID = $ds[‘streamID’];
$channel = $ds[‘channel’];
$type = $ds[‘type’];

switch($type) {
    case 1: // Twitch Stream API
        $twitch_api = 'https://api.twitch.tv/kraken/streams/'.$channel;
        $twitch_json = json_decode(@file_get_contents_curl($twitch_api));
        if ($twitch_json && $twitch_json->stream) {
            $live = 1;
            $viewers = $twitch_json->stream->viewers;
            $thumb = $twitch_json->stream->preview->large;
        } else {
            $live = 0;
        }
        break;
  case 2: // Hitbox Stream API
        $hitbox_api = 'https://api.hitbox.tv/media/live/'.$channel;
  	$hitbox_json = json_decode(@file_get_contents($hitbox_api));
  	if ($hitbox_json->livestream[0]->media_is_live == 1) {
  		$live = 1;
  		$viewers = $hitbox_json->livestream[0]->media_views;
  		$thumb = $hitbox_json->livestream[0]->media_thumbnail;
  	} else {
  		$live = 0;
  	}
        break;
  	}
$query = "UPDATE ".PREFIX."streams SET status='".$live."', viewers='".$viewers."', thumb='".$thumb."' WHERE streamID='".$streamID."'";
$result = $mysqli->query($query);

}
$mysqli->close();
?>

With this code i randomly get some twitch streams on my website but they disappear after some minutes.
My website URL is www.phantoms-esports.com
Can someone give me some help?
Best regards

I would be very worried if you’re writing code like this all over your site. If so, your entire website is a walking SQL injection waiting to happen.

With that aside, you don’t seem to understand scope in PHP. You have a function that gets a url with curl. $clientID is not within the scope of that function.

While you could make it possible by using global $clientID; within the function, it’s not a recommended practice. You can read more about scoping in PHP at http://php.net/manual/en/language.variables.scope.php

It was working fine before the Client ID thing (both Twitch and Hitbox). Now only Hitbox works. Unfortunately coding is not my strong point so i’m really struggling to get this working because the main attraction of my website is the stream :\

After some research i think i’ve got it.
I directly inserted the Client ID to the URL and it works atm

twitch_api = 'https://api.twitch.tv/kraken/streams/'.$channel.'?client_id=';

Don’t know if this is the best solution :\

This code looks identical to the code in this post. Is this some sort of WordPress plugin that hasn’t been updated by the author?

It’s part of a Webspell template i suppose

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