This is the code behind the demo at https://www.daniweb.com/connect/oauth/demo
DaniWeb Connect PHP OAuth
// Store some stuff we will need into variables
$client_id = 'Your application's Client ID';
$client_secret = 'Your application's Client Secret';
$current_url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];
// Initiate a cURL POST request to exchange the ?code= that was passed in as a GET parameter for an access token
$ch = curl_init('https://www.daniweb.com/connect/oauth/access_token');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, array(
'code' => $_REQUEST['code'],
'redirect_uri' => $current_url,
'client_id' => $client_id,
'client_secret' => $client_secret,
'grant_type' => 'authorization_code'
));
// Retrieve the JSON response from the cURL request, and decode it into a PHP object
$response = json_decode(curl_exec($ch));
// Close the cURL request
curl_close($ch);
// Retrieve the access token from the PHP object
$access_token = $response->access_token;
// Initiate a GET request to fetch information about the end-user by passing in our newly acquired access token, and then decode that JSON response
$output = json_decode(file_get_contents("https://www.daniweb.com/connect/api/users/~?access_token=" . urlencode($access_token)));
// Print a message to the end user
echo 'Hi, '.$output->data->profile->first_name.'!';
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.