Dolohov
Dolohov

Reputation: 31

Getting Twitch clip data via Twitch API and php

I am trying to get data of certain Twitch clip, for example this one https://clips.twitch.tv/MushyJollyWalrusUWot

$videosApi = 'https://api.twitch.tv/kraken/clips/savjz/MushyJollyWalrusUWot';
$clientId = 'my client id';
$ch = curl_init();

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

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

$json = json_decode($response, TRUE);

print_r($json);

I am getting an array with 404 error, but this code works fine with another Twitch api stuff, for example Twitch vod:

$videosApi = 'https://api.twitch.tv/kraken/videos/125820676';
//the rest is same

Upvotes: 0

Views: 2599

Answers (2)

Robert Cezar
Robert Cezar

Reputation: 13

just add your key

parameters(optional):

limit=10

game=Overwatch

trending=true

    //set header for pretty print
    header('Content-Type: application/json');
    $videosApi = 'https://api.twitch.tv/kraken/clips/top?limit=100&channel=ratirl';
    $clientId = 'secret app id provided by twitch';
    $ch = curl_init();

    curl_setopt_array($ch, array(
    CURLOPT_HTTPHEADER => array(
//standard api requirement from twitch api headers
    'Accept: application/vnd.twitchtv.v5+json',
    'Client-ID: ' . $clientId
    ),
    CURLOPT_SSL_VERIFYPEER => false,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_URL => $videosApi
    ));

    $response = curl_exec($ch);
    curl_close($ch);
    //decode the response
    $json = json_decode($response, JSON_PRETTY_PRINT);
    //print response
    print_r($json);

Upvotes: 0

Pysis
Pysis

Reputation: 1586

Like I have found with videos, clips are addressed by their name and nothing else, so I removed the username from your example.

I ended up using this URL https://api.twitch.tv/kraken/clips/MushyJollyWalrusUWot, which successfully returned a slightly large JSON blob containing the clip's information, and I have saved it at this URL: PasteBin - "SO Answer - Getting Twitch clip data via Twitch API and php"

I used to be able to query some base URL like https://api.twitch.tv/kraken/ in order to see a list of available resources that I could query from then on by successively adding to the path, but the /kraken and /kraken/ roots give user information, and /kraken/base does not seem to be a valid resource.

The API documentation for accessing this from the shell seems to be here: Twitch Developers - Twitch API Overview

By the way, I just used the shell, and may have had to provide both a Client ID and an OAuth token with user_read scope. I followed this guide for my reoccurring situation: GitHub - raine/twitch-cli - Setup. It's not PHP, so I did not review your code in full detail, but some of these steps may help you along with your conceptual troubleshooting.

Upvotes: 2

Related Questions