Here I tried to modify your script you are using to something that might work, have not tested it.
<?php
$client_id = '';
$client_secret = '';
$redirect_uri = '';
if (isset($_GET['code']) && $_GET['code']) {
$token_url = "https://id.twitch.tv/oauth2/token";
$data = array(
'client_id' => $client_id,
'client_secret' => $client_secret,
'grant_type' => 'authorization_code',
'redirect_uri' => $redirect_uri,
'code' => $_GET['code']
);
$curl = curl_init($token_url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_TIMEOUT, 1000);
$result = curl_exec($curl);
$i = curl_getinfo($curl);
curl_close($curl);
if ($i['http_code'] == 200) {
$result = json_decode($result, true);
$curl = curl_init('https://api.twitch.tv/helix/user');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Client-ID: ' . $client_id,
'Authorization: Bearer ' . $result['access_token']
));
curl_setopt($curl, CURLOPT_TIMEOUT, 1000);
$user = curl_exec($curl);
$i = curl_getinfo($curl);
curl_close($curl);
if ($i['http_code'] == 200) {
$user = json_decode($user);
echo '<p>Thanks ' . $user->data[0]->display_name . ' <3</p>';
} else {
echo '<p>An error occured, please <a href="/index.php">click here and try again</a></p>';
}
} else {
echo '<p>An error occured, please <a href="/index.php">click here and try again</a></p>';
}
} else {
$auth_url = 'https://api.twitch.tv/kraken/oauth2/authorize?response_type=code';
$auth_url .= '&client_id=' . $client_id;
$auth_url .= '&redirect_uri=' . $redirect_uri;
$auth_url .= '&scope=user_read';
$auth_url .= '&force_verify=true';
echo '<a href="' . $auth_url . '">Please Click this Link to Authenticate with Twitch</a>';
}