# New Twitch API and Auth from PHP

**URL:** <https://discuss.dev.twitch.com/t/new-twitch-api-and-auth-from-php/27984>\
**Category:** API\
**Created:** [August 31, 2020, 6:40pm UTC](https://discuss.dev.twitch.com/t/new-twitch-api-and-auth-from-php/27984 "2020-08-31T18:40:53Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Marek\_Klofac](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/marek_klofac/32/10123_2.png) [@Marek\_Klofac](https://discuss.dev.twitch.com/u/Marek_Klofac)\
**Post date:** [August 31, 2020, 6:40pm UTC](https://discuss.dev.twitch.com/t/new-twitch-api-and-auth-from-php/27984/1 "2020-08-31T18:40:53Z")

</div>

Hey guys. So I have been trying to write an application using New Twitch API using PHP and I got stuck at the first step which is authentication. I have read through various blogs and API references and now I know, that I need to use “Authorization code flow” to obtain user access token for my needs.

I am having problem with redirection and the Twitch auth. Below you can see my code snippet. In the end, I would like to get instantly redirected to obtained link via cURL where I can login with twitch credentials and then get redirected back to localhost.

My code:

```
$CLIENT_ID = "...";
$CLIENT_SECRET = "...";
$SCOPES = "channel:read:subscriptions";

function AuthorizeApplication(){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, "https://id.twitch.tv/oauth2/authorize?client_id={$GLOBALS['CLIENT_ID']}&redirect_uri=http://localhost:5500&response_type=code&scope={$GLOBALS['SCOPES']}");
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        
        $response = curl_exec($ch);
        echo $response;
        curl_close($ch);
}

```

This snippet shows clickable text on my website, on which when I click, I am redirected to Twitch and can login. So this is exactly what I want. But! I don’t want clickable button, I want to follow the URL from cURL post, so when I have added this line I though it would work:

`curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);`

But unfortunately it doesn’t work, and I stay on my localhost as nothing happened. I tried also different ways, such as cURLing again the obtained href from the response before, but it also doesn’t work.

Could you please help? I am desperate 🙂

Thank you!

---

<div class="post-metadata">

**Author:** ![BarryCarlyon](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/barrycarlyon/32/253_2.png) [@BarryCarlyon](https://discuss.dev.twitch.com/u/BarryCarlyon)\
**Post date:** [August 31, 2020, 6:54pm UTC](https://discuss.dev.twitch.com/t/new-twitch-api-and-auth-from-php/27984/2 "2020-08-31T18:54:01Z")

</div>

First step of “user authentication” is to redirect the user to Twitch.

Not make a cURL request.

Here is a PHP example that may help

> **[BarryCarlyon/twitch\_misc](https://github.com/BarryCarlyon/twitch_misc/tree/main/authentication/user_access_generator/php)**
>
> main/authentication/user\_access\_generator/php

---

<div class="post-metadata">

**Author:** ![Marek\_Klofac](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/marek_klofac/32/10123_2.png) [@Marek\_Klofac](https://discuss.dev.twitch.com/u/Marek_Klofac)\
**Post date:** [August 31, 2020, 6:55pm UTC](https://discuss.dev.twitch.com/t/new-twitch-api-and-auth-from-php/27984/3 "2020-08-31T18:55:32Z")

</div>

Huh, great resource! Thanks! Will investigate probably tommorow and maybe ask about something 🙂

---

<div class="post-metadata">

**Author:** ![Marek\_Klofac](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/marek_klofac/32/10123_2.png) [@Marek\_Klofac](https://discuss.dev.twitch.com/u/Marek_Klofac)\
**Post date:** [September 1, 2020, 9:17am UTC](https://discuss.dev.twitch.com/t/new-twitch-api-and-auth-from-php/27984/4 "2020-09-01T09:17:06Z")

</div>

I dont know why, but after successful login I get this messages on your page:

`Parameter redirect_uri does not match registered URI`

I am prettu sure, that I have it in Twitch dev console correctly ([http://localhost:8000](http://localhost:8000), client id, client secret etc.)

---

<div class="post-metadata">

**Author:** ![Marek\_Klofac](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/marek_klofac/32/10123_2.png) [@Marek\_Klofac](https://discuss.dev.twitch.com/u/Marek_Klofac)\
**Post date:** [September 1, 2020, 9:59am UTC](https://discuss.dev.twitch.com/t/new-twitch-api-and-auth-from-php/27984/5 "2020-09-01T09:59:12Z")

</div>

Ok, I have solved this issues. Problem was `http://localhost:8000/` and `http://localhost:8000`.

As of now I am having different problem with your code. I am getting `An Error Occured at code for token exchange:` because at lines 55 in index.php, for some reason the return http\_code is 0.

Did you encounter this? Thanks.

---

<div class="post-metadata">

**Author:** ![BarryCarlyon](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/barrycarlyon/32/253_2.png) [@BarryCarlyon](https://discuss.dev.twitch.com/u/BarryCarlyon)\
**Post date:** [September 1, 2020, 12:20pm UTC](https://discuss.dev.twitch.com/t/new-twitch-api-and-auth-from-php/27984/6 "2020-09-01T12:20:00Z")

</div>

HTTP Code 0 is odd

That means theres something super wrong on your server, and it’s ability to make HTTP requests

Expand the code to spit out more information about the error.

> [@Marek\_Klofac](#):
>
> because at lines 55 in index.php,

Line 55 is blank?

> <https://github.com/BarryCarlyon/twitch_misc/blob/main/authentication/user_access_generator/php/index.php#L55>

Line line 80 `$error = 'An Error Occured at code for token exchange: ' . print_r($r, true);` you probably want to update, to dump out the whole `$i` object and trace the issue.

HTTP Code 0 can be “failed to find the server” or other issue

You can also use

[https://www.php.net/curl\_error](https://www.php.net/curl_error)

To help debug

> [@Marek\_Klofac](#):
>
> Did you encounter this? Thanks.

Not this code tested fine on localtest and is similar to what I use in production

---

<div class="post-metadata">

**Author:** ![Marek\_Klofac](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/marek_klofac/32/10123_2.png) [@Marek\_Klofac](https://discuss.dev.twitch.com/u/Marek_Klofac)\
**Post date:** [September 1, 2020, 12:32pm UTC](https://discuss.dev.twitch.com/t/new-twitch-api-and-auth-from-php/27984/7 "2020-09-01T12:32:34Z")

</div>

I tried to print out the the $i variable from line 52. This is the output. I guess something is wrong with my PHP7 installation, will try it on XAMPP server instead…

```
Array
(
    [url] => https://id.twitch.tv/oauth2/token
    [content_type] => 
    [http_code] => 0
    [header_size] => 0
    [request_size] => 0
    [filetime] => -1
    [ssl_verify_result] => 20
    [redirect_count] => 0
    [total_time] => 0.36283
    [namelookup_time] => 0.018771
    [connect_time] => 0.189843
    [pretransfer_time] => 0
    [size_upload] => 0
    [size_download] => 0
    [speed_download] => 0
    [speed_upload] => 0
    [download_content_length] => -1
    [upload_content_length] => -1
    [starttransfer_time] => 0
    [redirect_time] => 0
    [redirect_url] => 
    [primary_ip] => 34.209.6.223
    [certinfo] => Array
        (
        )

    [primary_port] => 443
    [local_ip] => 10.0.0.3
    [local_port] => 52537
    [http_version] => 0
    [protocol] => 2
    [ssl_verifyresult] => 0
    [scheme] => HTTPS
    [appconnect_time_us] => 0
    [connect_time_us] => 189843
    [namelookup_time_us] => 18771
    [pretransfer_time_us] => 0
    [redirect_time_us] => 0
    [starttransfer_time_us] => 0
    [total_time_us] => 362830
)
```

---

<div class="post-metadata">

**Author:** ![Marek\_Klofac](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/marek_klofac/32/10123_2.png) [@Marek\_Klofac](https://discuss.dev.twitch.com/u/Marek_Klofac)\
**Post date:** [September 1, 2020, 12:51pm UTC](https://discuss.dev.twitch.com/t/new-twitch-api-and-auth-from-php/27984/8 "2020-09-01T12:51:41Z")

</div>

Hmm, got it working with XAMPP server. Must be my PHP bad install. Thanks!

---

<div class="post-metadata">

**Author:** ![BarryCarlyon](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/barrycarlyon/32/253_2.png) [@BarryCarlyon](https://discuss.dev.twitch.com/u/BarryCarlyon)\
**Post date:** [September 1, 2020, 12:55pm UTC](https://discuss.dev.twitch.com/t/new-twitch-api-and-auth-from-php/27984/9 "2020-09-01T12:55:11Z")

</div>

It’s also possible that the Server provider that you are using is blocking outbound requests.  
Could also be the firewall.  
It could also be the SSL Cert bundles being out of date.

But trying cURL on the command line may help track that one down.

---

<div class="post-metadata">

**Author:** ![system](https://d2opxh93rbxzdn.cloudfront.net/original/2X/0/05909729875cce3b53e59d5561c4d23bb14948fc.png) [@system](https://discuss.dev.twitch.com/u/system)\
**Post date:** [October 1, 2020, 12:55pm UTC](https://discuss.dev.twitch.com/t/new-twitch-api-and-auth-from-php/27984/10 "2020-10-01T12:55:11Z")

</div>

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