PHP API Calls to Twitch Helix

PHP API Calls to Twitch Helix

None developers might be amazed, how easy it is to make a call to the Twitch API within the TwitchPress plugin. If done in the correct place, anyone can easily copy the code on this page and make a request using one of the many endpoints (URL) available in the Twitch API documentation.

Always WP_HTTP_Curl

It is possible to make HTTP requests without using the WP core – but it’s not the way WordPress developers prefer. So the examples on this page always use the WP_HTTP_Curl and are designed to teach how much layers there are to building a request that eventually ends with WP_HTTP_Curl.

Method A

This is a simplistic method designed to use fool-proof (pre-made) post-request (after the request) functions. Such functions will apply as much validation as possible to determine if we have the data we need (sent from or not.

    $helix = new TwitchPress_Twitch_API();
    $user_object = $helix->get_users( null, $twitch_username );

You can change “get_users” to another method from those available in the TwitchPress_Twitch_API() class.

The class TwitchPress_Twitch_API() uses TwitchPress_Curl() class as does TwitchPress_Streamlabs_API() and eventually TwitchPress_Steam_API().

Method B

It is acceptable to use the TwitchPress_Twitch_API() class without a method already stated within the class itself.

$twitch_api = new TWITCHPRESS_Twitch_API();
$endpoint = '';
$headers = array( 'Client-ID' => twitchpress_get_app_id() );
$curl = $twitch_api->WP_Http_Curl( 'GET', $endpoint, $headers );       

This example shows how we can access the values that build our HTTP request. We can also control when $curl finally makes the call and it’s up to you how to process the response.

Method C

The next method uses very little of what TwitchPress offers. Even the functions adding id, secret and token can easily be replaced. What does happen, that is very TwitchPress, is the creation of a call ID for troubleshooting.

                // Create new curl object for performing an API call...
                $new_curl = new TwitchPress_Extend_WP_Http_Curl(

                // Add headers if the default does not add them in the current package...
                $new_curl->option_headers_additional( array(            
                    'Client-ID' => twitchpress_get_app_id(),
                ) );

                // Add body parameters if the package hasn't been designed to add them automatically...

                // Now add miscellanous values that will make up our curl request...

                // Finish the request, this makes the call and sets $this->final_output...

The important thing about this class, is that it extends the core class and so it’s development will continue in that direction. The purpose of doing this, is to add custom troubleshooting, diagnostic and validation that suits TwitchPress or Twitch. So expect changes to this class.


Someone new to all of this might be wondering where to place my examples. My advice is to create a child-theme which is basically a partial copy of your main theme. We copy the folder name, add “child” and only copy the theme files that we are going to edit, into our child theme folder. This way we don’t lose changes when our theme is updated.

I recommended doing more reading on child-themes, a very important aspect to WordPress and key knowledge for taking your WP site in the direction you want. Then you should understand where to put some of these examples – for testing only!

The final place for my code, should be a functions.php file (or similar) and please do not ever replace functions like “twitchpress_get_app_id” with raw values that a hacker could access if they obtained your themes original files.