Check current stream time with PHP and record time to CSV

<?php
	$curl = curl_init();
	curl_setopt($curl, CURLOPT_HTTPHEADER, array(
		'Accept: application/vnd.twitchtv.v5+json',
        'Client-ID: client_id_here'
	));

	curl_setopt($curl, CURLOPT_URL, 'https://api.twitch.tv/kraken/channels/115084217/videos?broadcast_type=archive&limit=1');
	curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
	$results = json_decode(curl_exec($curl));
    $output_file = fopen('output.csv', 'a');  // Append data with 'a' while 'w' overwrites the file
    
    // Uncomment the following if you want to print to stdout the data in a readable format.
    // print_r($results);

    foreach($results->videos as $video) {
        print_r($video->created_at);
        fputs($output_file, 'created_at,'.$video->created_at.PHP_EOL);   // PHP_EOL writes a new line
    }
    fclose($output_file);
?>
[morpheus] /opt/iobot2
illusion% !php
php ./getData.php 
2017-08-20T11:59:48Z

Note that since the API v3 is going away, I am calling v5.

2 Likes