[PHP] Cannot get cURL to send Client-ID

Hi! I’m trying to “repair” my API classes for my bot since they now require the Client-ID to be sent. I’ve registered my application and everything, and with “Header-Editor”(Chrome Extension) I can get it to work but I just cannot figur out how to make a cURL call in PHP that sends the Client-ID with it…

Thanks in advance,
SPETSDev

If you can, post what you have right now in terms of code.

The Client ID blog has this as an example which is PHP w/ a cURL call:

<?php
 $channelsApi = 'https://api.twitch.tv/kraken/channels/';
 $channelName = 'twitch';
 $clientId = 'axjhfp777tflhy0yjb5sftsil';
 $ch = curl_init();
 
 curl_setopt_array($ch, array(
    CURLOPT_HTTPHEADER => array(
       'Client-ID: ' . $clientId
    ),
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_URL => $channelsApi . $channelName
 ));
 
 $response = curl_exec($ch);
 curl_close($ch);
?>

Hey, thanks for your reply, and sorry for my late response! I’ve attempted to use your code, but still cannot get it to work… This is what I tried:

<?php $channelsApi = 'https://api.twitch.tv/kraken/channels/'; $channelName = 'somechannel'; $clientId = 'myClientID'; $ch = curl_init(); curl_setopt_array($ch, array( CURLOPT_HTTPHEADER => array( 'Client-ID: ' . $clientId ), CURLOPT_RETURNTRANSFER => true, CURLOPT_URL => $channelsApi . $channelName )); $response = curl_exec($ch); curl_close($ch); $followAPI = json_decode(@file_get_contents(channelsApi . $channelName), true); echo $followAPI['status']; ?>

If I understand what you’ve written (and I am no cURL/PHP expert, you’ve been warned :innocent: ), you are making the API call on the line $response = curl_exec($ch); but you then attempt to make the same call again but this time, you are feeding a URL through variables without the clientID - that is on this line $followAPI = json_decode(@file_get_contents(channelsApi . $channelName), true);

You should be able to alter that line to use the response from cURL: $followAPI = json_decode($response, true); - if that still doesn’t work, post what error you receive.

Worst case scenario, we should be able to do without the cURL and use: $followAPI = json_decode(@file_get_contents($channelsApi . $channelName . "?clientID=" . $clientId), true);

Also, as a note: @file_get_contents is blocked on some hosting servers for security reasons, but I can’t remember the alternative to it :banging head on desk:

@SPETSDev What isn’t working? The code you posted works just fine. You simply need to decode and then output what you already have. For example:

<?php
 $channelsApi = 'https://api.twitch.tv/kraken/channels/';
 $channelName = 'somechannel';
 $clientId = 'myClientID';
 $ch = curl_init();
 curl_setopt_array($ch, array(
    CURLOPT_HTTPHEADER => array(
       'Client-ID: ' . $clientId
    ),
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_URL => $channelsApi . $channelName
 ));
 $response = curl_exec($ch);
 curl_close($ch);
 
 $json = json_decode($response, true);
 echo $json['display_name'];
?>

I’ve tried this, however, it just does not show any data… I’m using the exact code you quoted there, put my channel & clientId in, but it displays nothing but a blank page…

@SPETSDev Have you tried to do something like:

echo curl_error($ch);

Make sure you do this before curl_close. It will output the error if there was a problem with the request or an empty string if there was no error. I’m guessing it is an invalid Client-ID.

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