New Twitch API and Auth from PHP

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 = "...";
$SCOPES = "channel:read:subscriptions";

function AuthorizeApplication(){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, "{$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;

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 :slight_smile:

Thank you!

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

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

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, client id, client secret etc.)

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.

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.

Line 55 is blank?

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

To help debug

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

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…

    [url] =>
    [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] =>
    [certinfo] => Array

    [primary_port] => 443
    [local_ip] =>
    [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

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

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.

1 Like

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