0

index.php と paypal.php の 2 つの PHP ファイルがあります。paypal.php のコードは次のとおりです。

    <?php 

session_start();


$client_id = 'xxxxxxxxxxxx';            
$client_secret = 'xxxxxxxxxxxxxxxxxxxx';    
$scopes = 'email profile';                    
$app_return_url = 'http://xxx.com/xxx/paypal.php';  
$nonce = time() . rand();

$code = $_REQUEST["code"];

if(empty($code)) {

    $_SESSION['state'] = md5(uniqid(rand(), TRUE)); 
    $paypal_auth_url = "https://www.paypal.com/webapps/auth/protocol/openidconnect/v1/authorize?"
            ."client_id=".$client_id
            ."&response_type=code"
            ."&scope=".$scopes
            ."&nonce=".$nonce
            ."&state=".$_SESSION['state']
            ."&redirect_uri=".urlencode($app_return_url);

    header("Location: $paypal_auth_url");     
}else{

    $token_url = "https://www.paypal.com/webapps/auth/protocol/openidconnect/v1/tokenservice";    
    $postvals = "client_id=".$client_id
            ."&client_secret=".$client_secret
            ."&grant_type=authorization_code"
            ."&code=".$code;


    $ch = curl_init($token_url);
    $options = array(
                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);
    $atoken = json_decode($response);


    $profile_url = "https://www.paypal.com/webapps/auth/protocol/openidconnect/v1/userinfo?"
            ."schema=openid"
            ."access_token=".$atoken->access_token;

    $ch = curl_init($profile_url);
    $options = array(
                CURLOPT_RETURNTRANSFER => 1,
                CURLOPT_SSLVERSION => 3
    );
    curl_setopt_array( $ch, $options );
    $response = curl_exec($ch);
    curl_close($ch);
    $profile= json_decode($response,true);  



    $_SESSION['paypal_user'] = "true";
    $_SESSION['profile'] = $profile;


    echo("<script> top.location.href='index.php'</script>");
}
?>

index.php のコードは次のとおりです。

    <?php
session_start();

// LOGOUT
if ($_GET['logout'] == 'true'){
    $_SESSION['paypal_user']="";    
}


if (strlen($_SESSION['paypal_user'])){
    // LOGGED USER
    echo "<pre>";
    print_r($_SESSION['profile']);
    echo "</pre>";
    echo "<br><BR> <a href='?logout=true'>LOGOUT</a>";
}else{
    // LOGIN
?>
    <a href='paypal.php' title='Paypal oAuth Login'>
    <img src='https://www.paypalobjects.com/en_US/Marketing/i/btn/login-with-paypal-button.png'>
    </a>
<?
}
?>

このコードが機能しない理由はありますか? var_dump json_decode を試しましたが、null が返されます。

ありがとうございました!

4

2 に答える 2

0

私は間違っているかもしれませんが、スコープは...

  "scopes": "email https://uri.paypal.com/services/paypalattributes",

URLはプロフィールです

于 2014-04-12T18:39:57.647 に答える