微信网页授权是服务号才有的高级功能。开发者授权后可获取用户基本信息;在此之前,想要获取消息信息的用户只能在用户与公众号交互时,根据openid获取用户信息;微信网页授权,无需消息交互,无需关注,即可获取用户基本信息。微信网页授权是通过OAuth2.0完成的。整个过程分为三个步骤:用户授权,获取代码;根据代码获取access_token[refresh_token可以刷新获取更长的有效期]通过access_token和openid获取用户信息对微信网页的授权流程做一个简单的封装:app_id}&redirect_uri={$redirect_uri}&response_type=code&scope=snsapi_userinfo&state={$state}#wechat_redirect";}/**获取授权token@paramstring$codeget_authorize_url*/publicfunctionget_access_token($app_id='',$app_secret='',$code=''){$token_url="https://api.weixin.qq.com/sns/oauth2/access_token?appid={$this->app_id}&secret={$this->app_secret}&code={$code}&grant_type=authorization_code";$token_data=$this->http($token_url);if($token_data[0]==200){returnjson_decode($token_data[1],TRUE);}returnFALSE;}/**获取授权后的微信用户信息@paramstring$access_token@paramstring$open_id*/publicfunctionget_user_info($access_token='',$open_id=''){if($access_token&&$open_id){$info_url="https://api.weixin.qq.com/sns/userinfo?access_token={$access_token}&openid={$open_id}&lang=zh_CN";$info_data=$this->http($info_url);if($info_data[0]==200){returnjson_decode($info_data[1],TRUE);}}returnFALSE;}公共函数http($url,$method,$postfields=null,$headers=array(),$debug=false){$ci=curl_init();/curlsettings/curl_setopt($ci,CURLOPT_HTTP_VERSION,CURL_HTTP_VERSION_1_1);curl_setopt($ci,CURLOPT_CONNECTTIMEOUT,30);curl_setopt($ci,CURLOPT_TIMEOUT,30);curl_setopt($ci,CURLOPT_RETURNTRANSFER,true);开关($method){case'POST':curl_setopt($ci,CURLOPT_POST,true);if(!empty($postfields)){curl_setopt($ci,CURLOPT_POSTFIELDS,$postfields);$this->postdata=$postfields;}break;}curl_setopt($ci,CURLOPT_URL,$url);curl_setopt($ci,CURLOPT_HTTPHEADER,$headers);curl_setopt($ci,CURLINFO_HEADER_OUT,true);$response=curl_exec($ci);$http_code=curl_getinfo($ci,CURLINFO_HTTP_CODE);if($debug){echo"=====发布数据======rn";var_dump($postfields);echo'=====info====='。“恩”;print_r(curl_getinfo($ci));echo'=====$response====='。“恩”;print_r($response);}curl_close($ci);返回数组($http_code,$response);}}
