public function signup_github() { $client_id = CLIENT_ID; $redirect_url = 'your_callback_url'; //get request , either code from github, or login request if ($_SERVER['REQUEST_METHOD'] == 'GET') { //authorised at github if (isset($_GET['code'])) { $code = $_GET['code']; //perform post request now $post = http_build_query(array('client_id' => $client_id, 'redirect_uri' => $redirect_url, 'client_secret' => 'your_client_secret', 'code' => $code)); $context = stream_context_create(array("http" => array("method" => "POST", "header" => "Content-Type: application/x-www-form-urlencodedrn" . "Content-Length: " . strlen($post) . "rn" . "Accept: application/json", "content" => $post))); $json_data = file_get_contents("https://github.com/login/oauth/access_token", false, $context); $r = json_decode($json_data, true); $access_token = $r['access_token']; $url = "https://api.github.com/user?access_token={$access_token}"; $data = file_get_contents($url); //echo $data; $user_data = json_decode($data, true); $username = $user_data['login']; $emails = file_get_contents("https://api.github.com/user/emails?access_token={$access_token}"); $emails = json_decode($emails, true); $email = $emails[0]; $signup_data = array('username' => $username, 'email' => $email, 'source' => 'github'); signup_login_user($signup_data); } else { $url = "https://github.com/login/oauth/authorize?client_id={$client_id}&redirect_uri={$redirect_url}&scope=user"; header("Location: {$url}"); } } }
/** * This function Creates a new authorization for this app */ function signup_github() { //get request , either code from github, or login request if ($_SERVER['REQUEST_METHOD'] == 'GET') { //authorised at github if (isset($_GET['code'])) { $code = $_GET['code']; $fields = array('client_id' => CLIENT_ID, 'client_secret' => CLIENT_SECRET, 'code' => $code); $postvars = ''; foreach ($fields as $key => $value) { $postvars .= $key . "=" . $value . "&"; } $data = array('url' => 'https://github.com/login/oauth/access_token', 'data' => $postvars, 'header' => array("Content-Type: application/x-www-form-urlencoded", "Accept: application/json"), 'method' => 'POST'); $gitResponce = json_decode(curlRequest($data)); if ($gitResponce->access_token) { $data = array('url' => 'https://api.github.com/user?access_token=' . $gitResponce->access_token, 'header' => array("Content-Type: application/x-www-form-urlencoded", "User-Agent: " . APP_NAME, "Accept: application/json"), 'method' => 'GET'); $gitUser = json_decode(curlRequest($data)); $signup_data = array('username' => $gitUser->login, 'email' => $gitUser->email, 'source' => 'github', 'token' => $gitResponce->access_token); signup_login_user($signup_data); } } } }
<?php include_once 'config.php'; include_once 'functions.php'; signup_login_user(); //Call this function so user is redirected to the home page is already signed up ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="description" content=""> <meta name="author" content=""> <title>Swordfish - Test</title> <!-- Bootstrap Core CSS --> <link href="http://klaasy.koding.io/swordfish/Lib/css/bootstrap/bootstrap.min.css" rel="stylesheet"> <!-- Custom CSS --> <link href="http://klaasy.koding.io/swordfish/Lib/css/small-business.css" rel="stylesheet"> <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries --> <!-- WARNING: Respond.js doesn't work if you view the page via file:// --> <!--[if lt IE 9]> <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script> <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>