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 Twitch.tv) 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 = 'https://api.twitch.tv/helix/users?login=LOLinDark1';
$headers = array( 'Client-ID' => twitchpress_get_app_id() );
$curl = $twitch_api->WP_Http_Curl( 'GET', $endpoint, $headers );       
$curl->finish_new_request(); 

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(
                    twitchpress_get_app_id(),
                    twitchpress_get_app_secret(),
                    twitchpress_get_app_token(),
                    'GET',                                                                       
                    'https://api.twitch.tv/helix/users?login=ZypheREvolved'
                );

                // 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...
                $new_curl->option_body_additional(); 

                // Now add miscellanous values that will make up our curl request...
                $new_curl->option_other_additional(); 

                // Finish the request, this makes the call and sets $this->final_output...
                $new_curl->finish_new_request();

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.

Summary

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.

Channel List Shortcode (Pro)

Channel List Shortcode (Pro)

The channel list shortcode displays a single stack of Twitch.tv channels…with style. This is a highly flexible shortcode with a lot of development potential when the options available aren’t enough for your needs.

Pro Upgrade Required

You must have the pro upgrade installed to use this shortcode. The upgrade is a reward for supporting the project – it is not for sale. Read more about TwitchPress Pro and also consider joining the projects Discord server for more information.

Examples

If you don’t see an example that suits your needs. Please come to the projects Discord and discuss your project.

Stream Team

List a the giving members of a stream team. The default content for this shortcode is also a stream-team. To replace the default content we need to add the type and team parameters.

[twitchpress_shortcodes type="team" team="test" shortcode="channel_list" refresh="60"]

Order-by

The giving example of ordering channels uses “name” but we can pass any string (word) that matches one of the values for a channel.

[twitchpress_shortcodes shortcode="channel_list" orderby="name" refresh="2"]

Online/Offline Only

Take care with the “display” attribute and consider what happens when no-channels are online. Additional attributes may need to be considered to ensure there is always content.

[twitchpress_shortcodes shortcode="channel_list" orderby="name" refresh="2" display="online"]
trim() expects parameter 1 to be string

trim() expects parameter 1 to be string

When activating the BugNet feature within TwitchPress you may see the following PHP log entry…

PHP Warning:  trim() expects parameter 1 to be string, object given in \wp-includes\class-wp-user.php on line 206

This warning begun to appear many weeks before I got around to investigating it. My findings confirm that the core is to blame. A back-trace I setup in class-wp-user.php near line 206 points to the dashboard.

PHP back-trace shows dashboard.php to be involved in the warning

On examining the dashboard source I can confirm that the warning happens within the Recent Comments widget and will not investigate this issue any further without evidence that suggests a cause by TwitchPress.

PHP error in WordPress dashboard page 2019

TwitchPress Pro Beta 0.0.2

The professional edition of TwitchPress Beta can be downloaded from Patreon.com and Discord (backers only). The pro edition has changed in ways not everyone will see i.e. the code has evolved while I try different approaches used in the free edition and it should lead to better performance.

The freely downloadable plugin will continue to improve but my work will focus on performance and bugs with very little new features being added unless I’m paid to do so. The free edition will always have basic support with respect to the Twitch.tv and WordPress.org communities.

Request Beta 0.0.3

Already, version 0.0.3 offers something new in the form of a sensible shortcode. The sensibility of this shortcode comes from two factors. The first is that it has been designed in a generic way, where other shortcodes are very close to what has been requested by specific users. The new shortcode taps into an object of many values and makes each value available as an individual output. Rather than a design that includes difficult to customize output i.e. HTML is included that might not suit everyone’s needs.

The second factor relates to the fact that the new shortcode can be used many times on a single page, without making many calls to Twitch API. This is nothing special, I’ve not done anything technically clever and this type of shortcode is a natural approach within a plugin like this. What makes it interesting is how long it has taking for this to be done, simply because I’ve been wrapped up in finishing other things within the project and completing the features I was hired to create.

So now, we’ll begin to see a plugin that adds a wealth of content to pages. This will trickle down to the free edition just a little but the pro edition will offer far more.

Feel free to request 0.0.3 if you ever backed** the project.

* * All funds raised through Patreon.com or donations will be spent on the project and achieving a professional solution that can only be done with funding. This is not for-profit.