PHP update stream tags

I’m trying to automate updating stream tags for my partner (because he never remembers to do them).

This is what I’ve got so far:

    $UST = curl_init();
    $USTinput = json_encode($USTarray);    
    var_dump(json_encode($USTarray));  // var dump 1
    curl_setopt($UST, CURLOPT_URL, "https://api.twitch.tv/helix/streams/tags?broadcaster_id=501071947");
    curl_setopt($UST, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($UST, CURLOPT_HTTPHEADER, array(  
        'Content-type: application/json',
        'Authorization: Bearer '.$authtoken,   
        'Client-ID: '.$clientid,         
    )); 
    curl_setopt($UST, CURLOPT_POST,true);
    curl_setopt($UST, CURLOPT_CUSTOMREQUEST, 'PUT');
    curl_setopt($UST, CURLOPT_POSTFIELDS,$USTinput);
    $USTdata = json_decode(curl_exec($UST),true);
    curl_close($UST);    

    var_dump($USTdata);    // var dump 2

The first var_dump for the input array (post json encode) gives me this:
string(196) “[“80427d95-bb46-42d3-bf4d-408e9bdca49a”,“96b6073f-450d-4248-8ed4-988e28f3f759”,“cc8d5abb-39c9-4942-a1ee-e1558512119e”,“7616f6ea-7e3d-4501-a87c-c160d2bc1849”,“26301bb2-91a3-4272-8a9b-5bcea6db0fcd”]”
which looks like it’s formatted like the example given in the Twitch reference.

The response I get is:
array(3) {
[“error”]=>
string(11) “Bad Request”
[“status”]=>
int(400)
[“message”]=>
string(73) “Request body was not parsable. Attempted Content-Type: “application/json””
}

I’ve also tried creating the input ID’s as just a string so it visually looks the same, but the only response I get when I try that is ‘NULL’ and the tags don’t update:
Can some kind person please point out my idiotic mistake? I’ve tried searching for examples online (which is the only way I ever manage to learn anything) but haven’t found anything useful.

remove

curl_setopt($UST, CURLOPT_POST,true);

You ain’t posting you are PUT, so the curl_setopt($UST, CURLOPT_POST,true); is Not Applicable and may interfere, the CUSTOMREQUEST overrides and the POST may set other stuff, you only need the CUSTOMNREQUEST

As to the actual probblem:

Your body that you are sending is wrong I believe (as you stated with your var_dump output), since you need to send an object containing a key of tag_ids that is an array of ID’s

The body of this request should be:


$USTarray = [
    'tag_ids': [
        "80427d95-bb46-42d3-bf4d-408e9bdca49a",
        "96b6073f-450d-4248-8ed4-988e28f3f759",
        "cc8d5abb-39c9-4942-a1ee-e1558512119e",
        "7616f6ea-7e3d-4501-a87c-c160d2bc1849",
        "26301bb2-91a3-4272-8a9b-5bcea6db0fcd"
    ]
];

$USTinput = json_encode($USTarray)

This then matches the example on Reference | Twitch Developers

1 Like

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