【问题标题】:Laravel and Twitch APILaravel 和 Twitch API
【发布时间】:2016-03-14 18:16:12
【问题描述】:

在我的 laravel 项目中更新我的流时遇到了一个小问题,我正在尝试定义流媒体是否是实时的,如果他们不在我的应用程序中,则隐藏它们但是当有一个流媒体不是实时的它给了我..

Trying to get property of non-object

我的 cronjob 回发看起来像这样:

$twitch_api = json_decode(file_get_contents('https://api.twitch.tv/kraken/streams/'.$stream->channel));
if($twitch_api->stream == null) $live = 0;
else $live = 1;

$update = Streams::where('id', $stream->id)->firstOrFail();
$update->thumb = $twitch_api->stream->preview->large;
$update->game = $twitch_api->stream->channel->game;
$update->status = $live;
$update->save();

Twitch API 在离线时会显示这个...

{"stream":null,"_links":{"self":"https://api.twitch.tv/kraken/streams/chan","channel":"https://api.twitch.tv/kraken/channels/chan"}}

知道为什么它给了我非对象吗?

谢谢!

【问题讨论】:

  • 我怀疑 "stream":null$twitch_api->stream->preview 会让你感到不适。 (因为null 不是对象。)
  • 感谢 HPierce,就是这样。我为预览和游戏做了一个 if 语句,现在它就像一个魅力。再次感谢=D

标签: php api laravel postback twitch


【解决方案1】:

当流处于离线状态并且"stream":null 时,您的以下代码行将不起作用:

$update->thumb = $twitch_api->stream->preview->large;
$update->game = $twitch_api->stream->channel->game;

您需要将其封装在 ifcase 语句中,以便在流离线时不会运行。您已经将 $live 设置为 1 如果它是实时的,您只需要稍后在控制语句中使用它。

类似这样的:

$twitch_api = json_decode(file_get_contents('https://api.twitch.tv/kraken/streams/'.$stream->channel));
if($twitch_api->stream == null) $live = 0;
else $live = 1;
$update = Streams::where('id', $stream->id)->firstOrFail();
switch($live){
   case 0:
     // do stuff when the stream is offline
     break;
   case 1: 
     // do stuff when the stream is online
     $update->thumb = $twitch_api->stream->preview->large;
     $update->game = $twitch_api->stream->channel->game;
     break;
}
$update->status = $live;
$update->save();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-09
    • 1970-01-01
    相关资源
    最近更新 更多