微信开发之网页授权获取用户信息(二)(2)

<?php namespace common\tools; /** * https请求相关类库 */ class HttpsTool { const TIMEOUT = ; // 设置超时时间 private $ch; // curl对象 /** * 发送curl请求,并获取请求结果 * @param string 请求地址 * @param array 如果是post请求则需要传入请求参数 * @param string 请求方法,get 或者 post, 默认为get * @param bool 是否以https协议请求 */ public function send_request($requests, $params = null, $method = 'get', $https = true) { // 以get方式提交 if ($method == 'get') { if($params){ $request = $requests . $this->create_url($params); }else{ $request = $requests; } }else{ $request = $requests; } $this->ch = curl_init($request); curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, );// 设置不显示结果,储存入变量 curl_setopt($this->ch, CURLOPT_TIMEOUT, self::TIMEOUT); // 设置超时限制防止死循环 // 判断是否以https方式访问 if ($https) { curl_setopt($this->ch, CURLOPT_SSL_VERIFYPEER, ); // 对认证证书来源的检查 curl_setopt($this->ch, CURLOPT_SSL_VERIFYHOST, ); // 从证书中检查SSL加密算法是否存在 } if ($method == 'post') { // 以post方式提交 //curl_setopt($this->ch, CURLOPT_SAFE_UPLOAD, false); //php .文件上传必加内容,.不需要 curl_setopt($this->ch, CURLOPT_POST, ); // 发送一个常规的Post请求 curl_setopt($this->ch, CURLOPT_POSTFIELDS, $params); // Post提交的数据包 curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, ); } $tmpInfo = curl_exec($this->ch); // 执行操作 if (curl_errno($this->ch)) { echo 'Errno:'.curl_error($this->ch);//捕抓异常 } curl_close($this->ch); // 关闭CURL会话 //var_dump($tmpInfo);exit; return $tmpInfo; // 返回数据 } /** * 生成url */ public function create_url($data) { $temp = '?'; foreach ($data as $key => $item) { $temp = $temp . $key . '=' . $item . '&'; } return substr($temp, , -); } }

关于curl_setopt($this->ch, CURLOPT_SAFE_UPLOAD, false)会在微信图片资源上传博文中详细讲述它出现的心酸史,这里暂时用不到,不做解释

c. 授权基类

<?php namespace common\tools\wechat; use common\tools\wechat\ConfigTool; use common\tools\HttpsTool; /** * Weixin_oauth 类库 */ class OauthTool { public $conf; public function __construct(){ $re = new ConfigTool; $this->conf = $re->setConfig(); } /** * 生成用户授权的地址 * @param string 自定义需要保持的信息 * @param sting 请求的路由 * @param bool 是否是通过公众平台方式认真 */ public function authorize_addr($route, $state='', $mp=false) { if ($mp) { $data = [ 'appid' => $this->conf['appid'], 'secret' => $this->conf['token'], 'grant_type' => 'client_credential' ]; $url = $this->conf['mp_authorize_url']; } else { $data = [ 'appid' => $this->conf['appid'], //公众号唯一标识 'redirect_uri' => urlencode($this->conf['redirect_uri'] . $route), //授权后重定向的回调链接地址 'response_type' => 'code', //返回类型,此处填写code 'scope'=>$this->conf['scope'], //应用授权作用域 'state'=>$state, //重定向后带上state参数,开发者可以填写任意参数 '#wechat_redirect'=>'' //直接在微信打开链接,可不填,做页面重定向时必须带此参数 ]; $url = $this->conf['authorize_url']; } $send = new HttpsTool; //var_dump($url . $send->create_url($data));exit; return $url . $send->create_url($data); } /** * 获取 access token * @param string 用于换取access token的code,微信提供 */ public function access_token($code) { $data = [ 'appid' => $this->conf['appid'], 'secret' => $this->conf['secret'], 'code' => $code, 'grant_type' => 'authorization_code' ]; // 生成授权url $url = $this->conf['access_token_url']; $send = new HttpsTool; return $send->send_request($url, $data); } /** * 获取用户信息 * @param string access token * @param string 用户的open id */ public function userinfo($token, $openid) { $data = [ 'access_token' => $token, 'openid' => $openid, 'lang' => $this->conf['lang'] ]; // 生成授权url $url = $this->conf['userinfo_url']; $send = new HttpsTool; return $send->send_request($url, $data); } }

d. 授权基类调用及用户数据处理(在控制器调用前,先对用户数据存入或更新)

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/a33dae39c367c97cbb30b8523e803776.html