Coding idiocy - Create Poll End Point (PHP)

Apparently I’m having a brain fail day - can someone please fix my code because I’m getting a ‘bad request, choices parameters missing’ response from Twitch. I’ve set it up in the same way I’ve done other CURL requests which require arrays within the main array but clearly I’ve missed something!

	$choices = array('title' => 'Yes','title' => 'No');
	$bodydata = array(
		'broadcaster_id' => '501071947',
		'title' => 'Can Kat ask for Shambos help?',
		'choices' => $choices,
		'channel_points_voting_enabled' => true,
		'channel_points_per_vote' => 10,
		'duration' => 120
	);
	$body = json_encode($bodydata);
    $CCR = curl_init();
    curl_setopt($CCR, CURLOPT_URL, "https://api.twitch.tv/helix/polls");
    curl_setopt($CCR, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($CCR, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($CCR, CURLOPT_HTTPHEADER, array(  
        'Content-type: application/json',
        'Authorization: Bearer '.$authtoken,     
        'Client-ID: '.$clientid, 
    ));   
    curl_setopt($CCR, CURLOPT_POST,true);
	curl_setopt($CCR, CURLOPT_POSTFIELDS,$body);
    $polldata = json_decode(curl_exec($CCR),true);
    curl_close($CCR);      
<?php

$choices = array('title' => 'Yes','title' => 'No');

print_r($choices);

$choices = array([ 'title' => 'Yes' ], [ 'title' => 'No' ]);

print_r($choices);

output

Array
(
    [title] => No
)
Array
(
    [0] => Array
        (
            [title] => Yes
        )

    [1] => Array
        (
            [title] => No
        )

)

You borked your choices

1 Like

Thank you @BarryCarlyon - you absolute legend you! Yes, I very much did bork that - and now I see how! I did not spot that specific requirement in the docs!

Well you created an associative array and then set the title key of that array twice so you only had one item in there

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