webapi/twitch.php

87 lines
2.7 KiB
PHP

<?php
include 'vars.php';
global $twtoken;
if ($_SERVER['PATH_INFO'] == "/werfolgt" || $_SERVER['PATH_INFO'] == "/wemfolgt") {
if ($_GET["user"] != null) {
$curl_h = curl_init('https://id.twitch.tv/oauth2/validate');
curl_setopt($curl_h, CURLOPT_HTTPHEADER,
array(
'Authorization: Bearer ' . $twtoken,
)
);
curl_setopt($curl_h, CURLOPT_RETURNTRANSFER, true);
$json = json_decode(curl_exec($curl_h));
$client_id = $json->{'client_id'};
$curl_h = curl_init('https://api.twitch.tv/helix/users?login=' . $_GET["user"]);
curl_setopt($curl_h, CURLOPT_HTTPHEADER,
array(
'Authorization: Bearer ' . $twtoken,
'Client-ID: ' . $client_id,
)
);
curl_setopt($curl_h, CURLOPT_RETURNTRANSFER, true);
$json = json_decode(curl_exec($curl_h));
$userid = $json->{'data'}[0]->{'id'};
$username = $json->{'data'}[0]->{'display_name'};
if ($userid != null) {
if ($_SERVER['PATH_INFO'] == "/werfolgt") {
echo "<h1>" . $username . " wird gefolgt von:</h1>";
getFollows("from");
}
else if ($_SERVER['PATH_INFO'] == "/wemfolgt") {
echo "<h1>" . $username . " folgt:</h1>";
getFollows("to");
}
} else {
echo "Der Nutzer " . $_GET["user"] . " existiert nicht.";
}
} else {
echo "Nutzernamen angeben: ?user=(twitch username) an den Link anhängen";
}
} else {
echo "<a href='/api/twitch.php/werfolgt'>Wer folgt...</a><br><a href='/api/twitch.php/wemfolgt'>Wem folgt...</a><br>";
}
function getFollows($dir) {
$cursor = null;
do {
$json = req($cursor, $dir);
$cursor = $json->{'pagination'}->{'cursor'};
foreach ($json->{'data'} as $fol) {
echo "<a href='https://twitch.tv/" . $fol->{$dir . '_name'} . "'>" . $fol->{$dir . '_name'} . "</a></br>";
}
} while($cursor != null);
}
function req($cursor, $dir) {
global $client_id;
global $userid;
global $dctoken;
$side = "from";
if ($dir == "from")
$side = "to";
if ($cursor == null) {
$curl_h = curl_init('https://api.twitch.tv/helix/users/follows?' . $side . '_id='. $userid . '&first=100');
}
else {
$curl_h = curl_init('https://api.twitch.tv/helix/users/follows?' . $side . '_id=' . $userid .'&first=100&after=' . $cursor);
}
curl_setopt($curl_h, CURLOPT_HTTPHEADER,
array(
'Authorization: Bearer ' . $dctoken,
'Client-ID: ' . $client_id,
)
);
curl_setopt($curl_h, CURLOPT_RETURNTRANSFER, true);
return json_decode(curl_exec($curl_h));
}