所以基本上我想做的是实时获取当前直播的流媒体的观众人数。我检查一下流是否是实时的。您如何不断地 ping API 以获取更新的值,以便我可以将更新的值发送到文档,在这种情况下,它最终会更新$viewers
我设置的变量。如果它有所作为,我正在制作的应用程序正在使用CodeIgniter framework
. 以下是我目前必须拨打电话的代码。现在我必须重新加载页面才能获得实际值,它使用 cURL 进行 API 调用。
class Streaming {
var $base_url = "https://api.twitch.tv/kraken/";
var $client_id = 'client_id_here';
public function load_stream_stats($channel) {
$curl = curl_init();
curl_setopt_array($curl, array( CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => $this->base_url . 'streams/'. $channel .'?client_id=' . $this->client_id
)
);
$result = curl_exec($curl);
//makes sure that the cURL was excuted if not it generates the error stating that it didn't succeed.
if(!curl_exec($curl)){
die('Error: "' . curl_error($curl) . '" - Code: ' . curl_errno($curl));
} else {
//cURL Response worked
if(!empty($channel)) {
$return = json_decode($result);
if($return->stream == null) {
return;
// stream is offline no need to do anything
} else {
$stream_details = array('viewers' => $return->stream->viewers);
// $return->stream->viewers gives a number e.g. 1052
return $stream_details;
}
}
}
curl_close($curl);
}
}
手头的问题是我如何 ping TwitchtV API 以从 REST API 获取更新的观众人数?