通过code获取小程序openid

//获取openid
public function getwx(){//获取openid

	//声明CODE,获取小程序传过来的CODE
	$code = $_GET["code"];
	//配置appid
	$appid = "1111";你的openid
	//配置appscret
	$secret = "2222";你的secret
	//api接口
	$api = "https://api.weixin.qq.com/sns/jscode2session?appid={$appid}&secret={$secret}&js_code={$code}&grant_type=authorization_code";
	//获取GET请求
	
	//发送
	$str = $this->httpGet($api);
	print_r($str);exit;
	if($str){
     	$data=['code'=>1,'msg'=>'成功','data'=>$str]; 
     }else{
     	$data=['code'=>0,'msg'=>'失败'];
     }
 }
 function httpGet($url){
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
	curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
	$output = curl_exec($ch);
	curl_close($ch);
	return $output;
}

你可能感兴趣的:(通过code获取小程序openid)