# Update Twitch Channel

**URL:** <https://staging-discuss.dev.twitch.com/t/update-twitch-channel/21227>\
**Category:** API\
**Created:** [May 19, 2019, 10:12am UTC](https://staging-discuss.dev.twitch.com/t/update-twitch-channel/21227 "2019-05-19T10:12:42Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![godeya007](https://d1d6mrmdu7ztse.cloudfront.net/user_avatar/staging-discuss.dev.twitch.com/godeya007/32/6393_2.png) [@godeya007](https://staging-discuss.dev.twitch.com/u/godeya007)\
**Post date:** [May 19, 2019, 10:12am UTC](https://staging-discuss.dev.twitch.com/t/update-twitch-channel/21227/1 "2019-05-19T10:12:42Z")

</div>

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](https://dev.twitch.tv/docs/v5/reference/channels/#update-channel) but I have no clue how to. Can someone please help me?

---

<div class="post-metadata">

**Author:** ![BarryCarlyon](https://d1d6mrmdu7ztse.cloudfront.net/user_avatar/staging-discuss.dev.twitch.com/barrycarlyon/32/253_2.png) [@BarryCarlyon](https://staging-discuss.dev.twitch.com/u/BarryCarlyon)\
**Post date:** [May 19, 2019, 10:14am UTC](https://staging-discuss.dev.twitch.com/t/update-twitch-channel/21227/2 "2019-05-19T10:14:05Z")

</div>

Convert

```auto
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

> **[Getting OAuth Access Tokens](https://dev.twitch.tv/docs/authentication/getting-tokens-oauth/#oauth-authorization-code-flow)**
>
> Getting OAuth Access Tokens

To get a token you can use

---

<div class="post-metadata">

**Author:** ![godeya007](https://d1d6mrmdu7ztse.cloudfront.net/user_avatar/staging-discuss.dev.twitch.com/godeya007/32/6393_2.png) [@godeya007](https://staging-discuss.dev.twitch.com/u/godeya007)\
**Post date:** [May 19, 2019, 10:23am UTC](https://staging-discuss.dev.twitch.com/t/update-twitch-channel/21227/3 "2019-05-19T10:23:10Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![BarryCarlyon](https://d1d6mrmdu7ztse.cloudfront.net/user_avatar/staging-discuss.dev.twitch.com/barrycarlyon/32/253_2.png) [@BarryCarlyon](https://staging-discuss.dev.twitch.com/u/BarryCarlyon)\
**Post date:** [May 19, 2019, 10:42am UTC](https://staging-discuss.dev.twitch.com/t/update-twitch-channel/21227/4 "2019-05-19T10:42:30Z")

</div>

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](https://lornajane.net/posts/2009/putting-data-fields-with-php-curl)

> <https://stackoverflow.com/questions/16920291/post-request-with-json-body>

> **[Getting OAuth Access Tokens](https://dev.twitch.tv/docs/authentication/getting-tokens-oauth/#oauth-authorization-code-flow)**
>
> Getting OAuth Access Tokens

> **[Reference](https://dev.twitch.tv/docs/api/reference/)**
>
> Twitch Developer tools and services to integrate Twitch into your development or create interactive experience on twitch.tv.

[https://www.php.net/manual/en/function.curl-exec.php](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.

---

<div class="post-metadata">

**Author:** ![godeya007](https://d1d6mrmdu7ztse.cloudfront.net/user_avatar/staging-discuss.dev.twitch.com/godeya007/32/6393_2.png) [@godeya007](https://staging-discuss.dev.twitch.com/u/godeya007)\
**Post date:** [May 19, 2019, 1:01pm UTC](https://staging-discuss.dev.twitch.com/t/update-twitch-channel/21227/5 "2019-05-19T13:01:48Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![godeya007](https://d1d6mrmdu7ztse.cloudfront.net/user_avatar/staging-discuss.dev.twitch.com/godeya007/32/6393_2.png) [@godeya007](https://staging-discuss.dev.twitch.com/u/godeya007)\
**Post date:** [May 19, 2019, 2:38pm UTC](https://staging-discuss.dev.twitch.com/t/update-twitch-channel/21227/6 "2019-05-19T14:38:38Z")

</div>

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>`

---

<div class="post-metadata">

**Author:** ![BarryCarlyon](https://d1d6mrmdu7ztse.cloudfront.net/user_avatar/staging-discuss.dev.twitch.com/barrycarlyon/32/253_2.png) [@BarryCarlyon](https://staging-discuss.dev.twitch.com/u/BarryCarlyon)\
**Post date:** [May 19, 2019, 4:25pm UTC](https://staging-discuss.dev.twitch.com/t/update-twitch-channel/21227/7 "2019-05-19T16:25:58Z")

</div>

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'
    ));
```

---

<div class="post-metadata">

**Author:** ![system](https://d2spem0quai2ol.cloudfront.net/original/2X/0/05909729875cce3b53e59d5561c4d23bb14948fc.png) [@system](https://staging-discuss.dev.twitch.com/u/system)\
**Post date:** [June 18, 2019, 4:26pm UTC](https://staging-discuss.dev.twitch.com/t/update-twitch-channel/21227/8 "2019-06-18T16:26:00Z")

</div>

Closed on June 18, 2019, 4:26pm UTC

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