Get streams by ID in curl is so long

Hi, I did a little app using javascript API. I did a first call requesting a twitch team and I loop this teams to get the streams with a “https://api.twitch.tv/kraken/streams/” + resp.users[index].display_name".

It’s fast and good.

I did the same in CURL with PHP. Trying to get the stream by the name gave me an error :" Bad Request [status] => 400 [message] => The parameter “id” was malformed: the value must match the regular expression /^[0-9]+$". So I did it with the stream _ID. As you can see the curl calls are almost the same as the javascript one. But, the result is far to be the same. In javascript the answer is almost instantly, with the curl it tooks around 1 minute to get the answer.

Here is my code :

 /* file_get_contents replaced with curl function */
function file_get_contents_curl($url) {
    $curlHeader = array(
        "Client-ID: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", /* SET CLIENT ID HERE */
        "Accept: application/vnd.twitchtv.v5+json"
    );

    $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, $curlHeader);

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

    return $data;
}

/* start doing stuff */
$dataArray = json_decode(@file_get_contents_curl('https://api.twitch.tv/kraken/teams/chatsbarbus'), true); 


if ($dataArray['users'] != null) { 
    foreach($dataArray['users'] as $mydata){
        //print_r($mydata);
        if($mydata['_id'] != null){ 
            //$streamer = $mydata['display_name'];  
            // $username = $mydata['channel']['name'];
            // $preview = $mydata['preview']['medium'];
            // ID2Name($mydata['_id'])
            $L2ID = json_decode(@file_get_contents_curl('https://api.twitch.tv/kraken/streams/' . $mydata['_id'] . '?api_version=5'), true);
            print_r($L2ID);
            //print_r($streamer);
            
			
        }
		echo "<a href=https://twitch.tv/" . $username . ">" . $streamer . "</a><br /> <img src=" . $preview . "> <br />";
    }
}
else {
        echo "Error in results from api.twitch.tv, probably cannot connect to server."; /* API probably did not respond at all */
    }
?>

So anybody can tell me what’s wrong, or maybe it’s normal ??

V5 API is by ID not by Login.

https://api.twitch.tv/kraken/streams/&lt;channel ID>

You need the channelID/userID for the user whom you are looking up.

As to the speed issues sounds like a problem with your Host/Server rather than anything else. I don’t see any latencty/long times here.

Your mileage will vary with teams end point, as it’s unofficial/unsupported/undocumented.

You are comparing the speed of client side with the speed of your server, which are two totally different systems to compare. (different types of firewall and methods of performing the lookup)


  1. 0-9 ↩︎

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