获取微信公众号的用户信息( ThinkphpP 5.0 )

request($url);
				if (!$result) {
					return false;
				}
				$arr=json_decode($result,true);
				cache('code_array',$arr,7200);
			
			}
			//获取access_token,openid
			$code_array=cache('code_array');
			$access_token=$code_array['access_token'];
			$openid=$code_array['openid'];
			$url="https://api.weixin.qq.com/sns/userinfo?access_token=$access_token&openid=$openid&lang=zh_CN";
			$result=$this->request($url);
			if (!$result) {
				return false;
			}
            //$arr 用户信息数组
			$arr=json_decode($result,true);			
            return $arr;
			
		}else{
			//发送获取code连接
			$url="https://open.weixin.qq.com/connect/oauth2/authorize?appid=$appid&redirect_uri=*****网站地址*****&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect";
			//发送GET请求
			header("Location: ".$url);
			exit();//必要
		 }

	
	}

	/*
	*发送GET请求方法
	*@param string $url URL
	*@param bool $ssl  是否为https协议
	*@return string   响应主体内容 
	*/

	private function request($url,$data=null){
		$curl=curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);

        //设定为不验证证书和host
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);

        if(!empty($data)){
            curl_setopt($curl, CURLOPT_POST, true);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        }

        // 将curl_exec()获取的信息以文件流的形式返回,而不是直接输出
        curl_setopt($curl,CURLOPT_RETURNTRANSFER,true);

        $output=curl_exec($curl);
        if (false===$output) {
        	echo "
",curl_error($curl),"
"; return false; } curl_close($curl); return $output; } }

你可能感兴趣的:(php)