dongliu8559 2016-09-17 14:10
浏览 60
已采纳

Twitch API包含带有php的客户端ID

Got a couple PHP scripts that query the Twitch API for certain informations. However Twitch made a change and from now on you have to include a Client ID or your request doesnt get through.

My scripts look like this:

$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);

with some research i found a website tha tprovides some help with PHP and client IDs. i found this example on how to include it:

<?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);
?>

But im not sure how to emerge this example into mine. In the exampel there is a Channel username and im confused if i have to do the scripts for each channel since right now the scripts are accessed by more than one channel.

Scripts are requested with: URL/scripts/followageTEST.php?username=XY&channel=XY

Would apreciate any help ^-^

  • 写回答

1条回答 默认 最新

  • dongxing1412 2016-09-17 14:25
    关注

    According to the readme of Twitch API on github:

    In situations where headers cannot be set, you can also specify a client ID as a querystring parameter: client_id=[CLIENT_ID]

    Here is a code that should work:

    $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);
    

    --

    Edit: Here is how to make a request to Twitch.tv API using file_get_contents function:

    $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 );
    (...)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 phython如何实现以下功能?查找同一用户名的消费金额合并—
  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址