function apiRequest($url, $post=FALSE, $headers=array()) { $ch = curl_init($url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); if($post) curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post)); $headers = [ 'Accept: application/vnd.github.v3+json, application/json', 'User-Agent: http://example-app.com/' ]; if(isset($_SESSION['access_token'])) $headers[] = 'Authorization: Bearer '.$SESSION['access_token']; curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); $response = curl_exec($ch); return json_decode($response, true); } $githubClientID = 'Ov23li271OczQKR6ISyL' $githubClientSecret = '9df1c6e9fa98690dbcc9ed12676dc2adcc1cf8cf'; $authorizeURL = 'https://github.com/login/oauth/authorize'; $tokenURL = 'https://github.com/login/oauth/access_token'; $apiURLBase = 'https://api.github.com/'; $baseURL = 'https://' . $_SERVER['SERVER_NAME'] . $_SERVER['PHP_SELF']; session_start(); if(!isset($_GET['action'])) { if(!empty($_SESSION['access_token'])) { echo '

Logged In

' echo '

View Repos

' echo '

Log Out

' } else { echo '

Not Logged In

' echo '

Log In

' } die(); } if(!isset($_GET['action']) && $_GET['action'] == 'login') { unset($_SESSION['access_token']); $_SESSION['state'] = bin2hex(random_bytes(16)); $params = array( 'response_type' => 'code', 'client_id' => '$githubClientID', 'redirect_uri' => '$baseURL', 'scope' => 'user_public_repo', 'state' => '$_SESSION['state']' ); header('Location: '.$authorizeURL.'?'.http_build_query($params)); die(); }