wx.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. namespace app\components\OpenAuth\core;
  3. use app\common\helpers\Session;
  4. use Yii;
  5. class WX extends WXOAuth {
  6. function __construct() {
  7. parent::__construct();
  8. }
  9. function verify() {
  10. if (isset($_SESSION['wx_token']) && $_SESSION['wx_token'] && isset($_SESSION['wx_token']['uid'])) {
  11. return true;
  12. } else {
  13. return false;
  14. }
  15. }
  16. }
  17. class WXOAuth {
  18. public $appid;
  19. public $appsecret;
  20. public $access_token;
  21. public $refresh_token;
  22. public $timeout = 30;
  23. public $connecttimeout = 30;
  24. public $ssl_verifypeer = FALSE;
  25. public $format = 'json';
  26. public $decode_json = TRUE;
  27. public $useragent = 'WX OAuth2.0';
  28. public $debug = FALSE;
  29. public static $boundary = '';
  30. function __construct() {
  31. $this->appid = WX_APPID;
  32. $this->appsecret = WX_APPSECRET;
  33. }
  34. function codeUrl($redirectUrl,$state){
  35. return 'https://open.weixin.qq.com/connect/qrconnect?appid='.$this->appid.'&redirect_uri='.urlencode($redirectUrl).'&response_type=code&scope=snsapi_login&state='.$state.'#'.time();
  36. }
  37. function accessTokenUrl($code) {
  38. return 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$this->appid.'&secret='.$this->appsecret.'&code='.$code.'&grant_type=authorization_code';
  39. }
  40. function getAccessToken($code, $keys = array()) {
  41. $response = $this->https_request($this->accessTokenUrl($code));
  42. $token = json_decode($response,true);
  43. if (is_array($token)) {
  44. $this->access_token = $token['access_token'];
  45. $this->refresh_token = isset($token['refresh_token']) ? $token['refresh_token'] : '';
  46. } else {
  47. throw new \Exception("读取access_token错误:{$token['error']}");
  48. }
  49. return $token;
  50. }
  51. //Curl
  52. function https_request($url,$options = null,$data = null,$header = null){
  53. $curl = curl_init();
  54. curl_setopt($curl, CURLOPT_URL, $url);
  55. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
  56. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
  57. if (!empty($data)){
  58. curl_setopt($curl, CURLOPT_POST, 1);
  59. curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
  60. }
  61. (isset($options['header'])&&$options['header'])?curl_setopt($curl, CURLOPT_HEADER, 1): curl_setopt($curl, CURLOPT_HEADER, 0);//如果有响应头
  62. if(!empty($header))//如果有请求头
  63. {
  64. $curl_header = array();
  65. foreach($header as $key=>$value)
  66. {
  67. $curl_header[] = "$key:$value,";
  68. }
  69. curl_setopt($curl,CURLOPT_HTTPHEADER, $curl_header);
  70. }
  71. if(isset($options['gzip'])&&$options['gzip']==1) curl_setopt($curl, CURLOPT_ENCODING, "gzip"); //如果页面开启了GZIP压缩
  72. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  73. curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:30.0) Gecko/20100101 Firefox/30.0');
  74. if(!empty($options['time_out']))
  75. {
  76. curl_setopt($curl, CURLOPT_TIMEOUT, $options['time_out']);
  77. }
  78. else
  79. {
  80. curl_setopt($curl, CURLOPT_TIMEOUT, 15);
  81. }
  82. $output = curl_exec($curl);
  83. curl_close($curl);
  84. return $output;
  85. }
  86. }