Skip to content

Commit

Permalink
Add getTwitchStream function
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinBacas committed Apr 4, 2017
1 parent 729f9af commit f39eba3
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
4 changes: 3 additions & 1 deletion functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,10 @@

/** Constants */
define('HOME_PAGE_ID', 2);
define('DEBUG_CURL', FALSE);

/** Custom actions */
require_once( 'library/actions.php' );

require_once( 'getlatestvids.php' );
require_once( 'getLatestVids.php' );
require_once( 'getTwitchStream.php' );
34 changes: 34 additions & 0 deletions getTwitchStream.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php
add_action('rest_api_init', function() {
register_rest_route('getTwitchStream/v1', '/getTwitchStream', array(
'methods' => 'GET',
'callback' => 'getTwitchStream',
));
});


function getTwitchStream($data) {
$twitch_client_id = get_field('twitch-api-key', HOME_PAGE_ID);
$curl = curl_init();
if (DEBUG_CURL) {
curl_setopt($curl, CURLOPT_VERBOSE, true);
$verbose = fopen('php://temp', 'w+');
curl_setopt($curl, CURLOPT_STDERR, $verbose);
}
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_URL, 'https://api.twitch.tv/kraken/streams/accropolis');
curl_setopt($curl, CURLOPT_HTTPHEADER, array("Client-ID: $twitch_client_id"));
curl_setopt($curl, CURLOPT_RETURNTRANSFER , TRUE);
$res = curl_exec($curl);
if (DEBUG_CURL) {
if ($result === FALSE) {
printf("cUrl error (#%d): %s<br>\n", curl_errno($curl),
htmlspecialchars(curl_error($curl)));
}
rewind($verbose);
$verboseLog = stream_get_contents($verbose);
echo "Verbose information:\n<pre>", htmlspecialchars($verboseLog), "</pre>\n";
}
curl_close($curl);
return json_decode($res);
}

0 comments on commit f39eba3

Please sign in to comment.