PopAccessTokenClient.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. namespace Com\Pdd\Pop\Sdk;
  3. use Com\Pdd\Pop\Sdk\Token\AccessTokenRequest;
  4. use Com\Pdd\Pop\Sdk\Token\RefreshAccessTokenRequest;
  5. /**
  6. * Pop接口调用的客户端类
  7. */
  8. class PopAccessTokenClient
  9. {
  10. /**
  11. * SDK版本号
  12. */
  13. public static $VERSION = "0.0.2";
  14. /**
  15. * 接口超时时间
  16. */
  17. private static $SECONDS = "30";
  18. private static $OAUTH_SERVER_URL = "https://open-api.pinduoduo.com/oauth/token";
  19. private $clientSecret;
  20. private $clientId;
  21. /**
  22. * 构造函数
  23. * @param $clientId 开放平台分配的clientId
  24. * @param $clientSecret 开放平台分配的clientSecret
  25. */
  26. public function __construct($clientId, $clientSecret){
  27. $this->clientId = $clientId;
  28. $this->clientSecret = $clientSecret;
  29. }
  30. /**
  31. * @param $code 授权获取到的code
  32. * @return 返回AccessTokenResponse对象
  33. */
  34. public function generate($code){
  35. if(empty($code)){
  36. throw new PopHttpException("Code不能为空");
  37. }
  38. $request = new AccessTokenRequest();
  39. $request->setClientId($this->clientId);
  40. $request->setClientSecret($this->clientSecret);
  41. $request->setCode($code);
  42. $request->setGrantType("authorization_code");
  43. $response = $this->postCurl($request);
  44. return $response;
  45. }
  46. /**
  47. * @param $refreshToken generate接口获取到的refresh_token,refresh_token有效期是30天
  48. * @return 返回刷新后的access_token
  49. */
  50. public function refresh($refreshToken){
  51. if(empty($refreshToken)){
  52. throw new PopHttpException("Refresh token 不能为空");
  53. }
  54. $request = new RefreshAccessTokenRequest();
  55. $request->setClientId($this->clientId);
  56. $request->setClientSecret($this->clientSecret);
  57. $request->setRefreshToken($refreshToken);
  58. $request->setGrantType("refresh_token");
  59. $response = $this->postCurl($request);
  60. return $response;
  61. }
  62. private function postCurl($request){
  63. $ch = curl_init();
  64. $curlVersion = curl_version();
  65. $ua = "PopSDK/".self::$VERSION." (".PHP_OS.") PHP/".PHP_VERSION." CURL/".$curlVersion['version']." "
  66. .$this->clientId;
  67. //设置超时
  68. curl_setopt($ch, CURLOPT_TIMEOUT, self::$SECONDS);
  69. curl_setopt($ch,CURLOPT_URL, self::$OAUTH_SERVER_URL);
  70. curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
  71. curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,0);//严格校验
  72. curl_setopt($ch,CURLOPT_USERAGENT, $ua);
  73. //设置header
  74. $headers = array(
  75. "Content-type: application/json;charset='utf-8'",
  76. "Accept: application/json",
  77. "Cache-Control: no-cache",
  78. "Pragma: no-cache",
  79. "Pdd-Sdk-Version: ".self::$VERSION,
  80. "Pdd-Sdk-Type: PHP"
  81. );
  82. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  83. //要求结果为字符串且输出到屏幕上
  84. curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
  85. //post提交方式
  86. curl_setopt($ch, CURLOPT_POST, TRUE);
  87. $param = json_encode($request);
  88. curl_setopt($ch, CURLOPT_POSTFIELDS, $param);
  89. //运行curl
  90. $raw_response = curl_exec($ch);
  91. $status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  92. //返回结果
  93. if($raw_response){
  94. curl_close($ch);
  95. $response = new PopHttpResponse();
  96. $response->setStatusCode($status_code);
  97. $response->setBody($raw_response);
  98. return $response;
  99. } else {
  100. $error = curl_errno($ch);
  101. curl_close($ch);
  102. throw new PopHttpException("curl出错,错误码:$error");
  103. }
  104. }
  105. }