首先要有域名,然后在微信测试号中的网页开发填写自己的域名。成功后就可以写代码了。
<?php namespace Home\Controller; use Think\Controller; class VoteController extends Controller { public function vote(){ $appid='wxae5eac671b6ce322'; $redirect_uri=urlencode('http://supinpin.top/layuilaogai/index.php/home/vote/validUser'); $url="https://open.weixin.qq.com/connect/oauth2/authorize?appid=$appid&redirect_uri=$redirect_uri&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect"; header("locationL".$url); } public function validUser(){ $code=$_GET['code']; // echo $code; // $access_token=$this->access_token($code); // echo $access_token; $json=$this->access_token($code); $arr=json_decode($json,true); $arr=array_change_key_case($arr,CASE_LOWER); if(isset($arr['access_token']) && isset($arr['openid'])){ $this->userinfo($arr['access_token'],$arr['openid']); }else{ echo '获取access_token出错'.$json; } } private function access_token($code){ $appid='wxae5eac671b6ce322'; $appsecret-"99c80bb2bccb8eb631179ceb42ca474c"; $url="https://api.weixin.qq.com/sns/oauth2/access_token?appid=$appid&secret=$appsecret&code=$code&grant_type=authorization_code"; $ret=https_request($url); return $ret; } private function userinfo($access_token,$openid){ $url="https://api.weixin.qq.com/sns/userinfo?access_token=$access_token&openid=$openid"; $json=https_request($url); echo $json; } } ?>
最后放到域名里实现。