Skip to content

Instantly share code, notes, and snippets.

@Edocsyl
Created May 22, 2013 15:30
Show Gist options
  • Select an option

  • Save Edocsyl/5628503 to your computer and use it in GitHub Desktop.

Select an option

Save Edocsyl/5628503 to your computer and use it in GitHub Desktop.

Revisions

  1. Edocsyl created this gist May 22, 2013.
    56 changes: 56 additions & 0 deletions twitchtv-followers.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,56 @@
    <?php

    /**
    * Twitch.tv get all channel follower with PHP
    * @author Edocsyl <kaj@edocsyl.ch>
    * @date 18.05.13
    * @version 1.0
    */

    class twitchfollow {

    private function createUrl($data = array()){
    return'https://api.twitch.tv/kraken/channels/' . $data[0] . '/follows.json?limit=' . $data[1] . '&offset=' . $data[2] . ($data[3] == true ? '&callback=?' : '');
    }

    private function makeJSONCall($url){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_REFERER, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    $result = curl_exec($ch);
    curl_close($ch);

    return json_decode($result);
    }

    public function getFollower($channel, $limit, $offset = 0, $callback = false){
    $callback = $this->makeJSONCall($this->createUrl(array($channel, $limit, $offset, $callback)));
    $callback = $callback->follows;
    $followerarray = array();
    foreach($callback as $flow){
    $followertmp = $flow->user;
    $follower = array(
    'name' => $followertmp->name,
    'display_name' => $followertmp->display_name,
    'logo' => $followertmp->logo,
    );
    array_push($followerarray, $follower);
    }

    return $followerarray;
    }
    }


    /**
    * Usage
    */

    $twitchfollow = new twitchfollow();
    var_dump($twitchfollow->getFollower('Edocsyl', 100));

    ?>