-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathexample.php
72 lines (55 loc) · 2.29 KB
/
example.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<?php
/**
* This is an example file to help understanding how the api works.
* This script assumes that you have already created an app on the LCTV API website
* and that you have selected the "confidential/authorization-grant" app type.
* The constants $CLIENT_ID, $CLIENT_SECRET, and $REDIRECT_URL below
* must match your app configuration on the LCTV API website.
* Use this script like http://your-site.net/online-status.php?channel=my-lctv-channel.
**/
require('livecodingAuth.php');
session_start();
define("CLIENT_ID", getenv('LCTV_CLIENT_ID'));
define("CLIENT_SECRET", getenv('LCTV_CLIENT_SECRET'));
define("REDIRECT_URL", getenv('LCTV_REDIRECT_URL'));
if(isset($_SESSION['channel']))
define("CHANNEL_NAME", $_SESSION['channel']);
else if (isset($_GET['channel']))
define("CHANNEL_NAME", htmlspecialchars($_GET['channel']));
else
define("CHANNEL_NAME", null);
define('CHANNEL_DATA_PATH', 'livestreams/' . CHANNEL_NAME . '/');
define('INVALID_CHANNEL_MSG', 'You must specify a channel name like: example.php?channel=my-channel .');
// Validate channel name param
$CHANNEL_NAME = CHANNEL_NAME;
if (empty( $CHANNEL_NAME ))
die(INVALID_CHANNEL_MSG);
else
$_SESSION['channel'] = CHANNEL_NAME;
unset($CHANNEL_NAME);
// Instantiate auth helper
try {
$LivecodingAuth = new LivecodingAuth(CLIENT_ID, CLIENT_SECRET, REDIRECT_URL);
}
catch(Exception $ex) {
die($ex->getMessage());
}
// Check for previous authorization
if (!$LivecodingAuth->getIsAuthorized()) {
// Here we have not yet been authorized
// Display a link for the user to authorize the app with this script as the redirect URL
$auth_link = $LivecodingAuth->getAuthLink();
echo "This app is not yet authorized. Use the link or URL below to authorize it.<br/>";
echo "<a href=\"$auth_link\">Connect my account</a><br/>" ;
// Here we wait for the user to click the authorization link
// which will result in another request for this page
// with $LivecodingAuth->getIsAuthorized() then returning true.
} else {
// Here we are authorized from a previous request
// Fetch some data from the API
$data = $LivecodingAuth->fetchData('livestreams/'.$_SESSION['channel'].'/', CHANNEL_STATUS_DATA_PATH);
// Present the data
$is_online = $data->is_live;
echo CHANNEL_NAME . " is " . (($is_online) ? 'online' : 'offline') ;
}
?>