Adding Client ID in header in PHP?

The documentation of the Twitch API recommends adding a Client ID on every request you make. Now I’m unsure how to do this in PHP.

Some of my code looks like follows:

function is_twitch_user_online($user_id) {
    
    if ( get_user_meta($user_id, 'twitch', true) ) {
        $twitch_name = get_user_meta($user_id, 'twitch', true);
            
        $jsonurl = "https://api.twitch.tv/kraken/channels/$twitch_name";
        
        if ( file_get_contents($jsonurl) ) {
            $json = json_decode(file_get_contents($jsonurl), true);

            //....
        }        
    }
    
}

Am I supposed to add a client ID to requests like this? How do I do that in PHP? Thank you :slight_smile:

You should use cURL.

Thank you for the reply. Could you perhaps post a practical example of how this would work in combination with my current code?

$ch = curl_init('https://api.twitch.tv/kraken/channels/' . $twitch_name);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Client-ID: ' . $YOURID,
));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$r = curl_exec($ch);
$i = curl_getinfo($ch);
curl_close($ch);

if ($i['http_code'] == 200) {
    $json = json_decode($r);
} else {
    echo 'Failed';
}
1 Like

Thank you very much!

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