作者:mobiledu2502863723 | 来源:互联网 | 2023-02-04 10:32
1 , 安卓应用, 微信登陆,流程图
2, php端代码
$appid = "";
$appsecret = "";
$code = $_GET['code'];
$url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$appid."&secret=".$appsecret."&code=".$code ."&grant_type=authorization_code";
$http = new \Common\Util\ddhttp;
$rs = $http -> get($url);
if(!$rs)$this -> error('获取OPENID失败');
$rt = json_decode($rs, 1);
if($rt['errcode'])$this -> error('授权失败:'.$rt['errmsg']);
$url = "https://api.weixin.qq.com/sns/userinfo?access_token=".$rt['access_token']."&openid=".$rt['openid']."&lang=zh_CN ";
$wechat_info = $http -> get($url);
if(!$wechat_info)$this -> error('获取用户资料失败:CURL '.$http -> errmsg);
$wechat_info = json_decode($wechat_info, 1);
if($wechat_info['errcode']){
$this -> error("获取用户资料失败".$wechat_info['errmsg']);
}
$user_info = array(
"headimg"=>$wechat_info['headimgurl'],
"nickname"=>$wechat_info['nickname'],
"sex"=>$wechat_info['sex'],
"openid"=>$wechat_info['openid'],
"unionid"=>$wechat_info['unionid']
);
工具类
namespace Common\Util;
class ddhttp{
protected $url;
protected $method;
protected $data;
public $errmsg;
public $sslkey = null;
public $sslcert = null;
public function get($url, $data = null){
$this->url = $url;
$data && $this->data = $data;
$this->method = "GET";
return $this->excrequest();
}
public function post($url, $data = null){
$this->url = $url;
$this->method = "POST";
$this->data = $data;
return $this->excrequest();
}
private function excrequest(){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $this->method );
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
if($this->sslcert){
curl_setopt($ch,CURLOPT_SSLCERTTYPE,'PEM');
curl_setopt($ch,CURLOPT_SSLCERT, $this->sslcert);
}
if($this->sslkey){
curl_setopt($ch,CURLOPT_SSLKEYTYPE,'PEM');
curl_setopt($ch,CURLOPT_SSLKEY, $this->sslkey);
}
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $this->data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$tmpInfo = curl_exec($ch);
$errorno = curl_errno($ch);
if(!$errorno)return $tmpInfo;
else{
$this->errmsg = $errorno;
return false;
}
}
}
?>