WeixinServer.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. namespace App\Servers\WeChat;
  3. class WeixinServer
  4. {
  5. /**
  6. * 单列对象
  7. * @var
  8. */
  9. private static $server = '';
  10. public $appid = '';
  11. public $appsecret = '';
  12. private function __construct(){
  13. $this->appid=env('WX_APPID','');
  14. $this->appsecret=env('WX_APPSECRET','');
  15. }
  16. /**
  17. * 创建对象
  18. * @return WeixinServer
  19. */
  20. static function creatServer()
  21. {
  22. if(empty(self::$server)){
  23. self::$server = new WeixinServer();
  24. }
  25. return self::$server;
  26. }
  27. /**
  28. * 网页授权
  29. * @return mixed
  30. */
  31. function authorize()
  32. {
  33. $code = request()->input('code', '');
  34. if (empty($code)) {
  35. $redirect_url = $this->getServerUrl();
  36. $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid={$this->appid}&redirect_uri={$redirect_url}&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect";
  37. $this->redirect($url);
  38. return false;
  39. } else {
  40. $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid={$this->appid}&secret={$this->appsecret}&code={$code}&grant_type=authorization_code";
  41. $result = $this->sendRequest($url);
  42. $result = json_decode($result, true);
  43. if (isset($result['errcode'])) {
  44. return false;
  45. } else {
  46. // $openid = $result['openid'];
  47. // $access_token = $result['access_token'];
  48. // $url = "https://api.weixin.qq.com/sns/oauth2/refresh_token?appid={$this->appid}&grant_type=refresh_token&refresh_token={$result['refresh_token']} ";
  49. // $result = $this->sendRequest($url);
  50. // $result = json_decode($result, true);
  51. // if (isset($result['access_token'])) {
  52. // $access_token = $result['access_token'];
  53. // $openid = $result['openid'];
  54. // }
  55. // $url = "https://api.weixin.qq.com/sns/userinfo?access_token={$access_token}&openid={$openid}&lang=zh_CN";
  56. // $result = $this->sendRequest($url);
  57. // $result = json_decode($result, true);
  58. // if (isset($result['errcode'])) {
  59. // return false;
  60. // }
  61. return $result;
  62. }
  63. }
  64. }
  65. /**
  66. * 获取当前连接
  67. * @return string
  68. */
  69. function getServerUrl()
  70. {
  71. return urlencode('http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);
  72. }
  73. function redirect($uri = '', $method = 'location', $http_response_code = 302)
  74. {
  75. if (!preg_match('#^https?://#i', $uri)) {
  76. $uri = site_url($uri);
  77. }
  78. switch ($method) {
  79. case 'refresh' :
  80. header("Refresh:0;url=" . $uri);
  81. break;
  82. default :
  83. header("Location: " . $uri, TRUE, $http_response_code);
  84. break;
  85. }
  86. exit();
  87. }
  88. /**
  89. * @param $url
  90. * @param string $type 请求方式
  91. * @param string $data 数据 数组格式
  92. * @return mixed
  93. */
  94. protected function sendRequest($url, $type = 'get', $data = '')
  95. {
  96. $ch = curl_init();
  97. if ($type == 'get' && $data) {
  98. $url = $url . '?' . http_build_query($data);
  99. }
  100. curl_setopt($ch, CURLOPT_URL, $url); //设置访问路径
  101. curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); //设置可以返回字符串
  102. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  103. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  104. $head = array('User-Agent:Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36');
  105. curl_setopt($ch, CURLOPT_HTTPHEADER, $head);
  106. if ($type == 'post') {
  107. curl_setopt($ch, CURLOPT_POST, TRUE);//post请求
  108. $data = json_encode($data, JSON_UNESCAPED_UNICODE);
  109. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);//设置传递的参数
  110. }
  111. $request = curl_exec($ch);
  112. curl_close($ch);
  113. return $request;
  114. }
  115. }