123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 |
- <?php
- namespace app\common\library;
- class WxService
- {
- protected $appid;
- protected $appKey;
- public $data = null;
- public function __construct($appid, $appKey)
- {
- $this->appid = $appid;
- $this->appKey = $appKey;
- }
-
- public function GetOpenid()
- {
-
- if (!isset($_GET['code'])){
-
- $baseUrl = $this->getCurrentUrl();
- $url = $this->__CreateOauthUrlForCode($baseUrl);
- Header("Location: $url");
- exit();
- } else {
-
- $code = $_GET['code'];
- $openid = $this->getOpenidFromMp($code);
- return $openid;
- }
- }
- public function getCurrentUrl()
- {
- $scheme = $_SERVER['HTTPS']=='on' ? 'https://' : 'http://';
- $uri = $_SERVER['PHP_SELF'].$_SERVER['QUERY_STRING'];
- if($_SERVER['REQUEST_URI']) $uri = $_SERVER['REQUEST_URI'];
- $baseUrl = urlencode($scheme.$_SERVER['HTTP_HOST'].$uri);
- return $baseUrl;
- }
-
- public function GetOpenidFromMp($code)
- {
- $url = $this->__CreateOauthUrlForOpenid($code);
- $res = self::curlGet($url);
- $data = json_decode($res,true);
- $this->data = $data;
- return $data;
- }
-
- private function __CreateOauthUrlForOpenid($code)
- {
- $urlObj["appid"] = $this->appid;
- $urlObj["secret"] = $this->appKey;
- $urlObj["code"] = $code;
- $urlObj["grant_type"] = "authorization_code";
- $bizString = $this->ToUrlParams($urlObj);
- return "https://api.weixin.qq.com/sns/oauth2/access_token?".$bizString;
- }
-
- private function __CreateOauthUrlForCode($redirectUrl)
- {
- $urlObj["appid"] = $this->appid;
- $urlObj["redirect_uri"] = "$redirectUrl";
- $urlObj["response_type"] = "code";
- $urlObj["scope"] = "snsapi_userinfo";
- $urlObj["state"] = "STATE";
- $bizString = $this->ToUrlParams($urlObj);
- return "https://open.weixin.qq.com/connect/oauth2/authorize?".$bizString;
- }
-
- private function ToUrlParams($urlObj)
- {
- $buff = "";
- foreach ($urlObj as $k => $v)
- {
- if($k != "sign") $buff .= $k . "=" . $v . "&";
- }
- $buff = trim($buff, "&");
- return $buff;
- }
-
- public function getUserInfo($openid,$access_token)
- {
- $response = self::curlGet('https://api.weixin.qq.com/sns/userinfo?access_token='.$access_token.'&openid='.$openid.'&lang=zh_CN');
- return json_decode($response,true);
- }
- public static function curlGet($url = '', $options = array())
- {
- $ch = curl_init($url);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_TIMEOUT, 30);
- if (!empty($options)) {
- curl_setopt_array($ch, $options);
- }
-
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
- curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
- $data = curl_exec($ch);
- curl_close($ch);
- return $data;
- }
- public static function curlPost($url = '', $postData = '', $options = array())
- {
- if (is_array($postData)) {
- $postData = http_build_query($postData);
- }
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_POST, 1);
- curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
- curl_setopt($ch, CURLOPT_TIMEOUT, 30);
- if (!empty($options)) {
- curl_setopt_array($ch, $options);
- }
-
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
- curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
- $data = curl_exec($ch);
- curl_close($ch);
- return $data;
- }
- }
|