//发起获得code值链接 public function doPageGetcode() { appid=′yourappid′;//修改你的appidif(!appid = 'yourappid';//修改你的appid if (!appid=′yourappid′;//修改你的appidif(!appid) { return this->result(10008, '参数错误', ''); } //这里的redirect_uri地址需要http://,跳转对于登录doPageOpenid方法,在微信公众号上面也有添加这个域名wx.owen.com/redirect_uri=urlencode("[http://wx.owen.com/doPageOpenid](http://wx.owen.com/doPageOpenid)");url = "open.weixin.qq.com/connect/oau…" . appid . "&redirect\_uri=" . redirect_uri . "&response_type=code&scope=snsapi_userinfo&state=123#wechat_redirect"; header('location:' . $url); }
public function httpRequest($url, $data = null) { $curl = curl_init(); curl\_setopt($curl, CURLOPT\_URL, $url); curl\_setopt($curl, CURLOPT\_SSL_VERIFYPEER, FALSE); curl\_setopt($curl, CURLOPT\_SSL_VERIFYHOST, FALSE); if (!empty($data)) { curl\_setopt($curl, CURLOPT\_POST, 1); curl\_setopt($curl, CURLOPT\_POSTFIELDS, $data); } curl\_setopt($curl, CURLOPT\_RETURNTRANSFER, 1); $output = curl_exec($curl); curl_close($curl); return $output; } //获取用户信息和注册 public function doPageOpenid() { $appid = "yourappid";//修改你的appid $secret = "yourappsecret";//修改你的appsecret $code = $_GPC\["code"\]; //小程序
// url="[https://api.weixin.qq.com/sns/jscode2session?appid=](https://api.weixin.qq.com/sns/jscode2session?appid=)".appid . "&secret=" . secret . "&js\_code=" . code . "&grant_type=authorization_code"; //公众号// 获得access_token//access\_token // access_token//url = "api.weixin.qq.com/cgi-bin/tok…" . appid . "&secret=" . secret; //公众号// 获得当前用户的Openid和access_token url=′[https://api.weixin.qq.com/sns/oauth2/access_token?appid=](https://api.weixin.qq.com/sns/oauth2/accesstoken?appid=)′.appid . '&secret=' . secret . '&code=' . code . '&grant_type=authorization_code';
$res = $this->httpRequest($url); $jsoninfo = json_decode($res, true); $access\_token = $jsoninfo\["access\_token"\]; $openid = $jsoninfo\["openid"\];
// // 获得所有关注用户的Openid // url="[https://api.weixin.qq.com/cgi-bin/user/get?access\_token=](https://api.weixin.qq.com/cgi−bin/user/get?accesstoken=)".access_token; //result=this->httpRequesturl);//jsoninfo = json_decoderesult,true);//openid = $jsoninfo["data"]["openid"][0];
if ($access_token && $openid) { $url = "https://api.weixin.qq.com/sns/userinfo?access\_token=" . $access\_token . "&openid=" . $openid . "&lang=zh_CN"; $output = $this->httpRequest($url);
//用户信息数组jsonUserinfojsonUserinfo jsonUserinfo=json_decode(output, true); return this−>result(0,′登录成功′,jsonUserinfo); } } }