离线图片展示Twitch WP插件
<p>我正在编写一个Twitch WordPress状态插件,如果Twitch在线或离线,它会显示一个简单的图片,但是我无法使其工作。以下是文件内容:</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>尝试使用不同的API端点,但结果为空。</p><p>期望的结果是显示在twitch.tv上在线的频道。</p><p><br /></p>