douguanyan9928 2015-06-30 13:30
浏览 34

使用PayPal登录会发出'通知:尝试获取非对象的属性'

I want to implement a 'Login with PayPal' button into my page and everything works fine, except that I can't receive the information from the user because I always get an 'Notice: Trying to get property of non-object in ...' error.

My JS code on the initial page looks like that:

<span id="myContainer" style="position: absolute;top: 0;left: 0;z-index: 1000;"></span>
        <script src="https://www.paypalobjects.com/js/external/api.js"></script>
        <script>
        paypal.use( ["login"], function(login) {
          login.render ({
            "appid": "ATA...fIi",
            "authend": "sandbox",
            "scopes": "profile email address https://uri.paypal.com/services/paypalattributes",
            "containerid": "myContainer",
            "locale": "en-us",
            "returnurl": "http://www.url.com/return.php"
          });
        });
        </script>

And the code of my return.php page looks like that:

$CLIENT_ID = 'ATA...fIi';
$CLIENT_SECRET = 'EKo...Adx';
define("CLIENT_ID", $CLIENT_ID);
define("CLIENT_SECRET", $CLIENT_SECRET);

define("URI_LIVE", "https://api.sandbox.paypal.com/v1/");
//define("URI_LIVE", "https://api.paypal.com/v1/");


class paypal{
    private $access_token;
    private $token_type;
    /**
    * Constructor
    *
    * Handles oauth 2 bearer token fetch
    * @link https://developer.paypal.com/webapps/developer/docs/api/#authentication--headers
    */
    public function __construct($code=''){
        $postvals = "grant_type=authorization_code&code=".$code."&redirect_uri=http://www.url.com/return.php";
        $uri = URI_LIVE . "oauth2/token";
        $auth_response = self::curl($uri, 'POST', $postvals, true);
        $this->access_token = $auth_response['body']->access_token;
        $this->token_type = $auth_response['body']->token_type;
    }

    ///////////////////////////////////////////////////////////

    public function getInfo(){
        $uri = URI_LIVE . "identity/openidconnect/userinfo/?schema=openid";
        return self::curl($uri, 'GET');
    }

    ///////////////////////////////////////////////////////////
    /**
    * cURL
    *
    * Handles GET / POST requests for auth requests
    * @link http://php.net/manual/en/book.curl.php
    */
    private function curl($url, $method = 'GET', $postvals = null, $auth = false){
        $ch = curl_init($url);
        //if we are sending request to obtain bearer token
        if ($auth){
            $headers = array("Accept: application/json", "Accept-Language: en_US");
            curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
            curl_setopt($ch, CURLOPT_USERPWD, CLIENT_ID . ":" .CLIENT_SECRET);
            curl_setopt($ch, CURLOPT_SSLVERSION, 3);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
            //if we are sending request with the bearer token for protected resources
        } else {
            $headers = array("Content-Type:application/json", "Authorization:{$this->token_type} {$this->access_token}");
        }
        $options = array(
            CURLOPT_HEADER => true,
            CURLINFO_HEADER_OUT => true,
            CURLOPT_HTTPHEADER => $headers,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_VERBOSE => true,
                CURLOPT_TIMEOUT => 10
        );
        if ($method == 'POST'){
            $options[CURLOPT_POSTFIELDS] = $postvals;
            $options[CURLOPT_CUSTOMREQUEST] = $method;
        }
        curl_setopt_array($ch, $options);
        $response = curl_exec($ch);
          //print_r($response);
        $header = substr($response, 0, curl_getinfo($ch,CURLINFO_HEADER_SIZE));
        $body = json_decode(substr($response, curl_getinfo($ch,CURLINFO_HEADER_SIZE)));
        curl_close($ch);
        return array('header' => $header, 'body' => $body);
    }
}

##########################################################################################################
##########################################################################################################
##########################################################################################################


print_r('We\'ve Got More Milk!');
print_r('<br>');
print_r($_GET);
print_r('<br>');
print_r('We\'ve Got Access!');
print_r('<br>');
$code = $_GET['code'];
$z = new paypal($code);
print_r($z);
print_r('<br>');

print_r('We\'ve Got Info!');
print_r('<br>');
$zz = $z->getInfo();

print_r($zz);

EDIT

The following two lines are responsible for the error:

$this->access_token = $auth_response['body']->access_token;
$this->token_type = $auth_response['body']->token_type;
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
    • ¥30 截图中的mathematics程序转换成matlab
    • ¥15 动力学代码报错,维度不匹配
    • ¥15 Power query添加列问题
    • ¥50 Kubernetes&Fission&Eleasticsearch
    • ¥15 報錯:Person is not mapped,如何解決?
    • ¥15 c++头文件不能识别CDialog
    • ¥15 Excel发现不可读取的内容
    • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
    • ¥20 yolov5自定义Prune报错,如何解决?
    • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积