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 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?