Code from API streams in my website

Hello all, I dont know where I should type:

curl -H ‘Accept: application/vnd.twitchtv.v5+json’
-H ‘Client-ID: uo6dggojyb8d6soh92zknwmi5ej1q2’
-X GET ‘https://api.twitch.tv/kraken/streams/?game=Overwatch

What kind of language is this?
Someone can help me?

That’s a cURL command. What are you trying to do?

1 Like

Thank you matt_thomas for answer me, I’m trying to get a list of the 25 live streams of Overwatch with the most current viewers.

Like this: http://isso.pro/twitchtest/overwatch.php ?

1 Like

Yes, exactly

You will want to start with an API query like:

https://api.twitch.tv/kraken/streams/?game=Overwatch&limit=25&client_id=xxxxxxxxxxxxxxxxxxxxxxxx

This will have the API search for games matching Overwatch, and limit the results to the first 25. The order is by viewer count. Therefore, limiting to 25 you will get the top 25 most viewed streams (matching your search)in descending order.

doing a foreach loop through the 25 results we can store values from each stream. I grab the display_name (user’s set case of name) the name (lowercase version of the name) and a preview panel to be used (i used medium for the example)

I have removed my client_id and made the code viewable: http://isso.pro/twitchtest/overwatch.php.txt

1 Like

Thank you matt_thomas for the support, but I’m not familiarize with cURL command, what am I supposed to do? I created my own Client-ID and I put in:

$clientID = array(
“Client-ID: kgvf8epqezfb5u3wwmlc58n681v5jj”
);

$dataArray = json_decode(@file_get_contents_curl(‘https://api.twitch.tv/kraken/streams/?game=Overwatch&limit=25&client_id=kgvf8epqezfb5u3wwmlc58n681v5jj’), true);

but didn’t work

function get_url_contents($url){
    $crl = curl_init();
    $timeout = 5;
    curl_setopt ($crl, CURLOPT_URL,$url);
    curl_setopt ($crl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt ($crl, CURLOPT_CONNECTTIMEOUT, $timeout);
    $ret = curl_exec($crl);
    curl_close($crl);
    return $ret;
}

Implement that helper function.

Use it as matt_thomas suggested:
$streams = json_decode(get_url_contents("https://api.twitch.tv/kraken/streams/?game=Overwatch&limit=25&client_id=xxxxxxxxxxxxxxxxxxxxxxxx"));

Then just view what it spits out:
print_r($streams);

1 Like

I have modified the code on http://isso.pro/twitchtest/overwatch.php to use your client_id and it’s working.

http://isso.pro/twitchtest/overwatch.php.txt is a symlink to http://isso.pro/twitchtest/overwatch.php - it shows you the php code being used to generate the page.

I will remove your client_id within 24 hours.

1 Like

I’m sorry matt_thomas, I dont know what I’m doing wrong, I put the code from http://isso.pro/twitchtest/overwatch.php.txt in my website and didn’t work :confused:

Thank you Sayuta for the support

http://isso.pro/twitchtest/overwatch.php.txt it’s not working for me, can someone help me?

What errors do you get?

I get else “Error in results from api.twitch.tv, probably cannot connect to server.”

I found my issue, I was working in localhost so I had to type:
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
probably you have the same issue, try it, thank you very much @matt_thomas for the support, you help me a lot <3

1 Like

This issue stems from not having the specific CA in a CA bundle available for the cURL library (Or OpenSSL, though OpenSSL usually has this). You can grab any CA Bundle and apply it with:

// Replace the path here with the actual path for your CS bundle
$CABundlePemPath = realpath(__DIR__ . '/../cabundle.pem')

curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_CAINFO, $CABundlePemPath);

The CA Bundle I use (Extracted from the FireFox browser source) is located here.

Now note that, while the solution you have here does work, it also means your server will not verify that api.twitch.tv is actually api.twitch.tv, meaning anyone can generate a cert and sit between you and the actual API and fake being the Twitch API to harvest tokens. I would recommend always verifying the peer as a safety measure for your OAuth tokens.

1 Like

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