PHP implementation PayPal Authorized Login _php Tutorial

Source: Internet
Author: User
Tags oauth openid

PHP enables PayPal authorized login


This article to you to share is the PHP implementation of the PayPal authorization login code, very simple and practical, the need for small partners can refer to the next.

PHP enables PayPal authorized login

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21st

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

/**

* @project PayPal Login

* @author Jiangjianhe

* @date 2015-04-03

*/

Class Paypallogin

{

Sandbox token link

Private $_sanbox_oauth2_auth_uri = ' https://www.sandbox.paypal.com/webapps/auth/protocol/openidconnect/v1/authorize ';

Private $_live_oauth2_auth_uri = ' https://www.paypal.com/webapps/auth/protocol/openidconnect/v1/authorize ';

Private $_acquire_user_profile_sandbox_url = ' https://www.sandbox.paypal.com/webapps/auth/protocol/openidconnect/ V1/userinfo?schema=openid&access_token= ';

Private $_acquire_user_profile_live_url = ' Https://www.paypal.com/webapps/auth/protocol/openidconnect/v1/userinfo ? schema=openid&access_token= ';

Sandbox token link

Private $_token_service_sandbox_url = ' https://www.sandbox.paypal.com/webapps/auth/protocol/openidconnect/v1/ Tokenservice ';

Private $_token_service_live_url = ' https://www.paypal.com/webapps/auth/protocol/openidconnect/v1/tokenservice ';

Private $_sanbox_flag = true;

Private $_client_id = null;

Private $_client_secret = null;

Private $_redirect_uri = null;

Private $_state = ';

Private $_scope = ' OpenID Email phone profile address https://uri.paypal.com/services/paypalattributes '; The scope parameter determines access permissions for the access token. Each parameter is detailed Url;:https://www.paypal-biz.com/product/login-with-paypal/index.html#configurebutton

public $token = null;

Public $protocol = "http";

/**

* @name Constructors

* @param $flag Sandbox environment

*/

Public function __construct ($redirect _uri, $client _id, $client _secret, $scope, $state, $flag = True)

{

$this->_sanbox_flag = $flag;

$this->_redirect_uri = $redirect _uri;

$this->_client_id = $client _id;

$this->_client_secret = $client _secret;

$this->_scope = $scope;

$this->_state = $state;

}

/**

* Create a PayPal request URL

* @return String

*/

Public Function Create_request_url ()

{

$oauth 2_auth_uri = $this->_sanbox_flag? $this->_sanbox_oauth2_auth_uri: $this->_live_oauth2_auth_uri;

$url = $oauth 2_auth_uri. '? '.

Http_build_query (

Array

' client_id ' = $this->_client_id,//Unique client identifier obtained through the application registration process. Necessary.

' Response_type ' = ' code ',//indicates that the authorization code is sent back to the application return URL. To make the access token invisible in the user agent, code a value is recommended. If you want to receive both the authorization code and the Id_token in the response, pass Code+id_token. Another possible response_type value is token--, which is mostly used by public clients such as JavaScript and mobile clients.

' Scope ' = $this->_scope,//;implode (', ', $this->scope),

' Redirect_uri ' = UrlEncode ($this->_redirect_uri),//The return URL of the application. The structure, host name, and Port must match the return URL that you set when registering the application.

' Nonce ' = Time (). Rand (),//opaque random identifiers, reduces the risk of replay attacks. The simple functions are: (timestamp + BASE64 encoding (random\[16\])).

' state ' = $this->_state,//CSRF Verification Code

)

);

return $url;

}

/**

* Get PayPal access token

* @param string $code?

* @return String access token

*/

Public Function Acquire_access_token ($code) {

$accessToken = null;

try {

$postvals = sprintf ("client_id=%s&client_secret=%s&grant_type=authorization_code&code=%s", $this- _client_id, $this->_client_secret, $code);

if ($this->_sanbox_flag)

$ch = Curl_init ($this->_token_service_sandbox_url);

Else

$ch = Curl_init ($this->_token_service_live_url);

$options = Array (

Curlopt_post = 1,

Curlopt_verbose = 1,

Curlopt_postfields = $postvals,

Curlopt_returntransfer = 1,

Curlopt_ssl_verifypeer = FALSE,

Curlopt_sslversion = 2

);

Curl_setopt_array ($ch, $options);

$response = curl_exec ($ch);

$error = Curl_error ($ch);

Curl_close ($ch);

if (! $response) {

throw new Exception ("Error Retrieving access token:".) Curl_error ($ch));

}

$jsonResponse = Json_decode ($response);

if (Isset ($jsonResponse->access_token)) {

$accessToken = $jsonResponse->access_token;

}

} catch (Exception $e) {

throw new Exception ($e->getmessage (), 1);

}

return $accessToken;

}

/**

* Get the PayPal user profile, decoded

* @param string $accessToken

* @return Object

*/

Public Function Acquire_paypal_user_profile ($accessToken) {

try {

if ($this->_sanbox_flag)

$url = $this->_acquire_user_profile_sandbox_url. $accessToken;

Else

$url = $this->_acquire_user_profile_live_url. $accessToken;

$ch = Curl_init ($url);

$options = Array (

Curlopt_returntransfer = 1,

Curlopt_ssl_verifypeer = FALSE,

Curlopt_sslversion = 2

);

Curl_setopt_array ($ch, $options);

$response = curl_exec ($ch);

$error = Curl_error ($ch);

Curl_close ($ch);

if (! $response)

{

return false;

}

Return Json_decode ($response);

} catch (Exception $e) {

return false;

}

}

}

?>

The above mentioned is the whole content of this article, I hope you can like.

http://www.bkjia.com/PHPjc/1007644.html www.bkjia.com true http://www.bkjia.com/PHPjc/1007644.html techarticle PHP Implementation PayPal Authorized login This article to share is the PHP implementation of the PayPal authorization login code, very simple and practical, the need for small partners can refer to the next. PHP implementation PayPal authorization ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.