离线图片展示Twitch WP插件
P粉391677921
P粉391677921 2023-07-26 16:40:51
0
1
508
<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>
P粉391677921
P粉391677921

全部回复(1)
P粉832490510

你的$headers缺少Authorization: Bearer 2gbdx6oar67tqtcmt49t3wpcgycthx或类似的令牌。

根据https://dev.twitch.tv/docs/api/reference/#get-streams的文档,Authorization需要一个应用访问令牌或用户访问令牌。

阅读文档以了解如何获取这样的令牌。这可能是oAuth流程。基本上,你需要创建一个应用程序,然后获得client_id和client_secret。使用client_secret请求令牌。一旦获得令牌,你可以将其添加到头部,然后代表你的应用使用API。(用户访问令牌可能有不同的流程)。


热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!