Offline picture display Twitch WP plug-in
P粉391677921
P粉391677921 2023-07-26 16:40:51
0
1
588
<p>I'm writing a Twitch WordPress status plugin that displays a simple image if Twitch is online or offline, but I can't get it to work. The following is the content of the file: </p> <pre class="brush:php;toolbar:false;"><?php /* Plugin Name: Twitch Status - InvidiousAus Description: Displays an image if the Twitch channel "invidiousaus" is online. Author: Your Name Version: 1.0 */ function smfr_twitch_get_stream_status($channel) { $client_id = 'we57gvgtlmhqnmk44d3mcbc68rgrd1'; // Replace with your Twitch API Client ID $api_url = "https://api.twitch.tv/helix/streams?user_login={$channel}"; $headers = array( 'Client-ID' => $client_id, ); $args = array( 'headers' => $headers, ); $response = wp_remote_get($api_url, $args); if (is_wp_error($response)) { return "Error fetching Twitch status."; } $response_code = wp_remote_retrieve_response_code($response); if ($response_code === 200) { $body = wp_remote_retrieve_body($response); $data = json_decode($body, true); if (!empty($data['data'])) { return 'online'; } } return 'offline'; } function smfr_twitch_status_fnc() { $channel = 'invidiousaus'; // Update to "invidiousaus" $stream_status = smfr_twitch_get_stream_status($channel); if ($stream_status === 'online') { // If the channel is live, show the "online" image $html = "<div class='smfr-twitch-status'><img src='" . plugin_dir_url(__FILE__) . "img/en_ligne.jpg'></div>"; } else { // If the channel is offline, show the "offline" image $html = "<div class='smfr-twitch-status'><img src='" . plugin_dir_url(__FILE__) . "img/hors_ligne.jpg'></div>"; } return $html; } function smfr_twitch_status_style() { // Register the stylesheet for the plugin (if needed) // wp_enqueue_style('smfr_twitch_status_style', plugin_dir_url(__FILE__) . 'style.css'); } add_action('wp_enqueue_scripts', 'smfr_twitch_status_style'); add_shortcode('smfr_twitch_status', 'smfr_twitch_status_fnc');</pre> <p>Tried using different API endpoints but got empty results. </p><p>The desired result is to display channels that are online on twitch.tv. </p><p><br /></p>
P粉391677921
P粉391677921

reply all(1)
P粉832490510

Your $headers are missing Authorization: Bearer 2gbdx6oar67tqtcmt49t3wpcgycthx or similar token.

According to the documentation at https://dev.twitch.tv/docs/api/reference/#get-streams, Authorization requires an application access token or user access token.

Read the documentation to learn how to obtain such a token. This may be the oAuth flow. Basically, you need to create an application and then get the client_id and client_secret. Use client_secret to request the token. Once you have the token, you can add it to the header and then use the API on behalf of your app. (There may be a different flow for user access tokens).


Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template