Hello past boardcast on website

i am trying to pull my past broadcast to my website and heres a sample of the code i used and now its not working at all <?php
//twitch return
$json2 = json_decode(file_get_contents(“https://api.twitch.tv/kraken/videos?channel_id=76727141client_id=o&broadcasts=true&limit=10”));
$videosArray = $json2->videos;
?>
and i have php pulling them to show on my website with this

<?php echo $videosArray[0]->title; ?>

<?php echo $videosArray[0]->game; ?> and any help will be great

file_get_contents is likely blocked.

Generally speaking web hosts block file_get_contents ability to fetch URL’s.

You should be using the PHP cURL functions instead.

Define not working at all?

Your code does not error checking or checking of Status Codes.

URL is constructed wrong, Client ID needs to be sent as a header. And you have no v5 header in here at all. And channel_id is not a query string paramater

Documentation:

The correct URL is

https://api.twitch.tv/kraken/channels/<channel ID>/videos

Some sample code, off the top of my head that is untested:

<?php

    $ch = curl_init('https://api.twitch.tv/kraken/channels/76727141/videos?broadcast_type=archive&limit=10');
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'Accept: application/vnd.twitchtv.v5+json',
        'Client-ID: CLIENTID'
    ));
    curl_setopt($ch, CURLOPT_RETRUNTRANSFER, true);

    $r = curl_exec($ch);
    $i = curl_getinfo($ch);
    curl_close($ch);

    if ($i['http_code'] == 200) {
        $videos = json_decode($r);
        if (json_last_error() == JSON_ERROR_NONE) {
            foreach ($videos as $video) {
                // do stuff
            }
        } else {
            // an error occured
        }
    } else {
        // an error occured
    }

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.