I want to use a PHP form to update my Twitch channel’s “Title” and “Category” (Game) like on the Twitch Live Dashboard, except on my own form with just those two fields.
I understand that I need to use the commands at https://dev.twitch.tv/docs/v5/reference/channels/#update-channel but I have no clue how to. Can someone please help me?
Convert
curl -H 'Client-ID: uo6dggojyb8d6soh92zknwmi5ej1q2' \
-H 'Accept: application/vnd.twitchtv.v5+json' \
-H 'Authorization: OAuth cfabdegwdoklmawdzdo98xt2fo512y' \
-d 'channel[status]=The+Finalest+of+Fantasies&channel[game]=Final+Fantasy+XV&channel[channel_feed_enabled]=false' \
to a PHP cURL request.
You will need to have done an oAuth loop first
To get a token you can use
I am a bit confused since its a PUT request and with the OAuth loop I am a bit lost. Would you be able to help me with the actual code in PHP. Many thanks for the quick response.
Here is a none tested, not to be used in production, one pager example that should work.
<?php
$client_id = '';
$client_secret = '';
$redirect_uri = 'https://mywebsite.com/path/to/this/page/';
session_start();
if (isset($_GET['logout']) && $_GET['logout']) {
session_destroy();
header('Location: /path/to/this/page/');
} else if (isset($_GET['code']) && $_GET['code']) {
$curl = curl_init('https://id.twitch.tv/oauth2/token');
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, array(
'client_id' => $client_id,
'client_secret' => $client_secret,
'grant_type' => 'authorization_code',
'redirect_uri' => $redirect_uri,
'code' => $_GET['code']
));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($curl);
$i = curl_getinfo($curl);
curl_close($curl);
if ($i['http_code'] == 200) {
$result = json_decode($result, true);
$_SESSION['twitch'] = $result;
$curl = curl_init('https://id.twitch.tv/oauth2/validate');
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Authorization: OAuth ' . $_SESSION['twitch']['access_token']
));
$user = curl_exec($curl);
$i = curl_getinfo($curl);
curl_close($curl);
if ($i['http_code'] == 200) {
$validate = json_decode($user);
$_SESSION['twitch_id'] = $validate->user_id;
header('Location: /path/to/this/page/');
} else {
echo '<p>An Error Occucred please try again</p>';
}
} else {
echo '<p>An Error Occucred please try again</p>';
}
} else if (isset($_SESSION) && isset($_SESSION['twitch']) && $_SESSION['twitch']) {
if (isset($_POST) && $_POST) {
$curl = curl_init('https://api.twitch.tv/kraken/channels/' . $_SESSION['twitch_id']);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode([
'status' => $_POST['status'],
'game' => $_POST['game']
]));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Authorization: OAuth ' . $_SESSION['twitch']['access_token'],
'Content-Type: application/json'
));
$r = curl_exec($curl);
$i = curl_getinfo($curl);
curl_close($curl);
if ($i['http_code'] == 200) {
echo 'OK';
} else {
echo 'An Error Occured';
}
}
?>
<a href="?logout=1">Logout</a>
<form action="" method="post">
<fieldset>
<div>
<label for="game">Game</label>
<input type="text" name="game" id="game" />
</div>
<div>
<label for="status">Title</label>
<input type="text" name="status" id="status" />
</div>
<div>
<input type="submit" />
</div>
</fieldset>
</form>
<?php
} else {
?>
<p>You need to login with Twitch</p>
<a href="https://id.twitch.tv/oauth2/authorize?client_id=<?php echo $client_id; ?>&redirect_uri=<?php echo $redirect_uri; ?>&response_type=code&scope=">Login</a>
<?php
}
Some References
https://lornajane.net/posts/2009/putting-data-fields-with-php-curl
php, json, post
https://www.php.net/manual/en/function.curl-exec.php
I’d suggest learning the basics of PHP before trying to do something advanced such as this.
1 Like
Hi thanks so much for this. I used the code and replaced the values as needed. It logs in fine, authorized the account as well and returns to the form to update but after I fill and click Submit it gives me the error “An Error Occured”. Cant seem to figure out why. Is there some permission thats not given?
Tried adding scope=channel_editor
in the Login link as follows but its still the same as earlier.
<a href="https://id.twitch.tv/oauth2/authorize?client_id=<?php echo $client_id; ?>&redirect_uri=<?php echo $redirect_uri; ?>&response_type=code&scope=channel_editor">Login</a>
You might be right on the scope.
My understanding is you ONLY need the scope if you are updating someone else’s channel. If you are updating your own it should work without it. (But I don’t run code to update my own channel and this example is illustrative of what should work and should not be used in production)
Consider adapting
if ($i['http_code'] == 200) {
echo 'OK';
} else {
echo 'An Error Occured';
}
to
if ($i['http_code'] == 200) {
echo 'OK';
} else {
echo 'An Error Occured ' . $i['http_code'] . ' - ' . $r;
}
To spit out whatever the error is
I expect the issue is that
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode([
'status' => $_POST['status'],
'game' => $_POST['game']
]));
Should be, as I built the POST payload wrong.
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode([
'channel' => [
'status' => $_POST['status'],
'game' => $_POST['game']
]
]));
Additionally:
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Authorization: OAuth ' . $_SESSION['twitch']['access_token'],
'Content-Type: application/json'
));
should be
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Authorization: OAuth ' . $_SESSION['twitch']['access_token'],
'Accept: application/vnd.twitchtv.v5+json',
'Content-Type: application/json'
));
system
Closed
June 18, 2019, 4:26pm
8
This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.