我正在编写一个 Twitch Wordpress 状态插件,如果在线或离线,它会显示一个简单的图像,但我无法让它工作。下面是文件
<?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');
尝试尝试不同的 API 端点但结果为空
期望在线显示 twitch.tv 上的在线频道