Created
May 5, 2012 00:06
-
-
Save xcommerce-gists/2598636 to your computer and use it in GitHub Desktop.
PHP OAuth 2 Implementation for PayPal Access
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
define('KEY', 'YOUR APPLICATION ID'); | |
define('SECRET', 'YOUR APPLICATION SECRET'); | |
define('CALLBACK_URL', 'YOUR CALLBACK PATH - TO COMPLETE.PHP'); | |
define('AUTHORIZATION_ENDPOINT', 'https://identity.x.com/xidentity/resources/authorize'); | |
define('ACCESS_TOKEN_ENDPOINT', 'https://identity.x.com/xidentity/oauthtokenservice'); | |
define('PROFILE_ENDPOINT', 'https://identity.x.com/xidentity/resources/profile/me'); | |
/*************************************************************************** | |
* Function: Run CURL | |
* Description: Executes a CURL request | |
* Parameters: url (string) - URL to make request to | |
* method (string) - HTTP transfer method | |
* headers - HTTP transfer headers | |
* postvals - post values | |
**************************************************************************/ | |
function run_curl($url, $method = 'GET', $postvals = null){ | |
$ch = curl_init($url); | |
//GET request: send headers and return data transfer | |
if ($method == 'GET'){ | |
$options = array( | |
CURLOPT_URL => $url, | |
CURLOPT_RETURNTRANSFER => 1, | |
CURLOPT_SSLVERSION => 3 | |
); | |
curl_setopt_array($ch, $options); | |
//POST / PUT request: send post object and return data transfer | |
} else { | |
$options = array( | |
CURLOPT_URL => $url, | |
CURLOPT_POST => 1, | |
CURLOPT_VERBOSE => 1, | |
CURLOPT_POSTFIELDS => $postvals, | |
CURLOPT_RETURNTRANSFER => 1, | |
CURLOPT_SSLVERSION => 3 | |
); | |
curl_setopt_array($ch, $options); | |
} | |
$response = curl_exec($ch); | |
curl_close($ch); | |
return $response; | |
} | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once "common.php"; | |
//capture code from auth | |
$code = $_GET["code"]; | |
//construct POST object for access token fetch request | |
$postvals = sprintf("client_id=%s&client_secret=%s&grant_type=authorization_code&code=%s&redirect_uri=%s", KEY, SECRET, $code, urlencode(CALLBACK_URL)); | |
//get JSON access token object (with refresh_token parameter) | |
$token = json_decode(run_curl(ACCESS_TOKEN_ENDPOINT, 'POST', $postvals)); | |
//construct URI to fetch profile information for current user | |
$profile_url = sprintf("%s?oauth_token=%s", PROFILE_ENDPOINT, $token->access_token); | |
//fetch profile of current user | |
$profile = run_curl($profile_url); | |
var_dump($profile); | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once "common.php"; | |
$auth_url = sprintf("%s?scope=%s&response_type=code&redirect_uri=%s&client_id=%s", | |
AUTHORIZATION_ENDPOINT, | |
urlencode("https://identity.x.com/xidentity/resources/profile/me"), | |
urlencode(CALLBACK_URL), | |
KEY); | |
//forward user to PayPal auth page | |
header("Location: $auth_url"); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment