Modify Channel Information API in PHP

I’m attempting to automate updating stream title and game because my streamer partner is struggling with remembering to do those. This is my first stab at doing it in PHP - here’s my code (minus extraneous var_dump bits):

$URD = curl_init();
$URDarray = array(   
    'game_id'=>'32982',
    'title'=>'There be chaos up in here',
    'broadcaster_language'=>'en'
);  

$URDinput = json_encode($URDarray);    
curl_setopt($URD, CURLOPT_URL, "https://api.twitch.tv/helix/channels?broadcaster_id=501071947");
curl_setopt($URD, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($URD, CURLOPT_HTTPHEADER, array(  
    'Content-type: application/json',
    'Authorization: Bearer '.$authtoken,   
    'Client-ID: '.$clientid,         
)); 
curl_setopt($URD, CURLOPT_POST,true);
curl_setopt($URD, CURLOPT_CUSTOMREQUEST, 'PATCH');
curl_setopt($UCR, CURLOPT_POSTFIELDS,$URDinput);
$URRdata = json_decode(curl_exec($URD),true);
curl_close($URD);      

The response I’m getting is:
array(3) {
[“error”]=>
string(11) “Bad Request”
[“status”]=>
int(400)
[“message”]=>
string(61) “request must contain at least 1 channel property for updating”
}

Can some kind person please help me figure out what I’ve done wrong - the auth is working, the channel scope is good, so I’m sure there’s just some glaring errors in the way I’ve attempted to do the CURL patch stuff - the documentation is really really unhelpful for novice programmers!

curl_setopt($URD, CURLOPT_POST,true);
curl_setopt($URD, CURLOPT_CUSTOMREQUEST, 'PATCH');

You delcare both POST and PATCH, so not sure which one succeeded. PATCH should of won. But it may not have done.

Also a typo here

curl_setopt($UCR, CURLOPT_POSTFIELDS,$URDinput);

should be

curl_setopt($URD, CURLOPT_POSTFIELDS,$URDinput);

URD not UCR

1 Like

Well the typo certainly doesn’t help :rofl: :no_mouth: - thank you Barry - once again, for being so helpful! Unfortunately he’s gone offline so I can’t test it now so I will give it another try tomorrow!

Channel doesn’t need to be online to change the title/game!

Interesting - it’s pulled the channel information but it’s now giving me a NULL response to the query (after fixing the typo) - literally just ‘NULL’

If I take out curl_setopt($URD, CURLOPT_CUSTOMREQUEST, ‘PATCH’); I get a 404 not found

This Endpoint only accept’s PATCH’s

The response is a 204 No Content, so NULL is correct.
You should use curl_getinfo (PHP: curl_getinfo - Manual) to extract the HTTP response code.

2 Likes

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