| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- <?php
- /**
- * 微信授权服务
- * Class AuthService
- * Author SHUI
- * @package alipayAop
- */
- define('WEIXIN_SDK_DIR', __DIR__);
- require_once WEIXIN_SDK_DIR . '/lib/WxPay.Config.php';
- class AuthService {
- public function getOpenidFromWeixin($code) {
- $url = $this->_createOauthUrl($code);
- $res = self::curlGet($url);
- $data = json_decode($res, true);
- $openid = isset($data['openid']) ? $data['openid'] : '';
- return $openid;
- }
- private function _createOauthUrl($code) {
- $data = [
- 'appid' => WxPayConfig::APPID,
- 'secret' => WxPayConfig::APPSECRET,
- 'code' => $code,
- 'grant_type' => 'authorization_code'
- ];
- $queryStr = $this->_buildUrlParam($data);
- return "https://api.weixin.qq.com/sns/oauth2/access_token?{$queryStr}";
- }
- private function _buildUrlParam($data) {
- $buff = '';
- foreach ( $data as $k => $v ) {
- if ( 'sign' != $k ) {
- $buff .= "{$k}={$v}&";
- }
- }
- return trim($buff, '&');
- }
- public static function curlGet($url = '', $options = []) {
- $ch = curl_init();
-
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_HEADER, 0);
- curl_setopt($ch, CURLOPT_TIMEOUT, 30);
- if ( !empty($options) ) {
- curl_setopt_array($ch, $options);
- }
- //https请求 不验证证书和host
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
- curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
- $data = curl_exec($ch);
- curl_close($ch);
- return $data;
- }
- }
|