使用 PHP 时出现 Twitch API 连接错误


Twitch API connection error with using PHP

我的英语水平很差。如果我犯了错误,我很抱歉。我正在尝试通过 php 连接抽搐 api,但出现错误。

<?php
	$json=@json_decode(file_get_contents("https://api.twitch.tv/kraken/streams/artemis"));
	$json->display_Name;
?>

请帮帮我,谢谢。

您需要向下潜入响应树。您要查找的属性位于流>通道中。要得到它,只需做

echo $json->stream->channel->display_name

这应该有效:

$json=json_decode(file_get_contents("https://api.twitch.tv/kraken/streams/artemis"));
$json->stream->channel->display_name;

检查$json它是对象的对象的var_dump:

object(stdClass)[2]
  public '_links' => 
    object(stdClass)[3]
      public 'self' => string 'https://api.twitch.tv/kraken/streams/artemis' (length=44)
      public 'channel' => string 'https://api.twitch.tv/kraken/channels/artemis' (length=45)
  public 'stream' => 
    object(stdClass)[4]
      public '_id' => int 13346966656
      public 'game' => string 'Call of Duty: Advanced Warfare' (length=30)
      public 'viewers' => int 136
      public 'created_at' => string '2015-02-27T10:32:10Z' (length=20)
      public 'video_height' => int 720
      public 'average_fps' => float 29.8274593932
      public '_links' => 
        object(stdClass)[5]
          public 'self' => string 'https://api.twitch.tv/kraken/streams/artemis' (length=44)
      public 'preview' => 
        object(stdClass)[6]
          public 'small' => string 'http://static-cdn.jtvnw.net/previews-ttv/live_user_artemis-80x45.jpg' (length=68)
          public 'medium' => string 'http://static-cdn.jtvnw.net/previews-ttv/live_user_artemis-320x180.jpg' (length=70)
          public 'large' => string 'http://static-cdn.jtvnw.net/previews-ttv/live_user_artemis-640x360.jpg' (length=70)
          public 'template' => string 'http://static-cdn.jtvnw.net/previews-ttv/live_user_artemis-{width}x{height}.jpg' (length=79)
      public 'channel' => 
        object(stdClass)[7]
          public '_links' => 
            object(stdClass)[8]
              ...
          public 'background' => null
          public 'banner' => string 'http://static-cdn.jtvnw.net/jtv_user_pictures/artemis-channel_header_image-00576f41c413af25-640x125.jpeg' (length=104)
          public 'broadcaster_language' => string 'en' (length=2)
          public 'display_name' => string 'Artemis' (length=7)
          public 'game' => string 'Call of Duty: Advanced Warfare' (length=30)
          public 'logo' => string 'http://static-cdn.jtvnw.net/jtv_user_pictures/artemis-profile_image-bfe1b3c2c66796ec-300x300.jpeg' (length=97)
          public 'mature' => boolean false
          public 'status' => string '[Sarah] [XB1] Masters Ranked' (length=28)
          public 'partner' => boolean true
          public 'url' => string 'http://www.twitch.tv/artemis' (length=28)
          public 'video_banner' => string 'http://static-cdn.jtvnw.net/jtv_user_pictures/artemis-channel_offline_image-0da061c7900b14ab-640x360.png' (length=104)
          public '_id' => int 45016072
          public 'name' => string 'artemis' (length=7)
          public 'created_at' => string '2013-06-21T05:54:07Z' (length=20)
          public 'updated_at' => string '2015-02-27T13:17:57Z' (length=20)
          public 'delay' => int 0
          public 'followers' => int 48448
          public 'profile_banner' => string 'http://static-cdn.jtvnw.net/jtv_user_pictures/artemis-profile_banner-6d385d7e747f485d-480.jpeg' (length=94)
          public 'profile_banner_background_color' => null
          public 'views' => int 704212
          public 'language' => string 'en' (length=2)