2016-09-17 64 views
0

有幾個PHP腳本來查詢Twitch API的某些信息。 但是Twitch做出了改變,從現在開始,您必須包含一個客戶ID或您的請求無法通過。Twitch API包含一個Cleint ID和php

我的劇本是這樣的:

$url = "https://api.twitch.tv/kraken/users/" . $_GET['username'] . "/follows/channels/" . $_GET['channel']; 

$result = file_get_contents($url); 
$result = json_decode($result, true); 

#echo $result["created_at"]; 
#echo date('d-m-Y h:i:s ',strtotime($result["created_at"])); 


    $followdate = new DateTime(date('d-m-Y h:i:s',strtotime($result["created_at"]))); 
    $heute = new DateTime(date('d-m-Y h:i:s')); 
    $diff = $followdate->diff($heute); 

有一些研究,我發現一個網站塔tprovides一些幫助,PHP和客戶端ID。我發現瞭如何將其納入這個例子:

<?php 
$channelsApi = 'https://api.twitch.tv/kraken/channels/'; 
$channelName = 'twitch'; 
$clientId = 'axjhfp777tflhy0yjb5sftsil'; 
$ch = curl_init(); 

curl_setopt_array($ch, array(
    CURLOPT_HTTPHEADER => array(
     'Client-ID: ' . $clientId 
    ), 
    CURLOPT_RETURNTRANSFER => true, 
    CURLOPT_URL => $channelsApi . $channelName 
)); 

$response = curl_exec($ch); 
curl_close($ch); 
?> 

但是我不知道怎麼冒出這個例子中了我的。在考試中有一個頻道用戶名,如果我必須爲每個頻道執行腳本,我會感到困惑,因爲現在這些腳本可以被多個頻道訪問。

腳本請與:URL /腳本/ followageTEST.php用戶名= XY &通道= XY

會apreciate任何幫助^ -^

回答

1

按照readme of Twitch API on github

在無法設置標題的情況下,還可以將客戶端ID指定爲查詢字符串參數:client_id = [CLIENT_ID]

下面是應該工作代碼:

$client_id = 'YOUR CLIENT ID'; 
$url = "https://api.twitch.tv/kraken/users/" . $_GET['username'] . "/follows/channels/" . $_GET['channel'] . '?client_id=' . $client_id; 

$result = file_get_contents($url); 
$result = json_decode($result, true); 

#echo $result["created_at"]; 
#echo date('d-m-Y h:i:s ',strtotime($result["created_at"])); 

$followdate = new DateTime(date('d-m-Y h:i:s',strtotime($result["created_at"]))); 
$heute = new DateTime(date('d-m-Y h:i:s')); 
$diff = $followdate->diff($heute); 

-

編輯: 下面是如何使用file_get_contents功能做出Twitch.tv API的請求:

$client_id = 'YOUR CLIENT ID'; 
$opts = array('http' => 
    array('header' => "Client-ID:" . $client_id) 
]; 

$context = stream_context_create($opts); 
$result = file_get_contents($url, false, $context); 
(...) 
+0

我想我錯過了twitch API自述文件中的那部分內容。我試試這個。提前致謝! – user3220962

+0

好吧,它的工作方式。真棒。非常感謝你! :) – user3220962