離線圖片展示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>