//获取小程序用户openid 返回值为json字符串 json
定义变量小程序
protected $appid = "******************";微信小程序
protected $secret = "************************************";api
protected $grant_type = "authorization_code";服务器
public function GetOpenid()
{
$request = I('param.');微信
//获取微信小程序传过来的code
$js_code = $request['code'];session
$curl = curl_init();app
//使用curl_setopt() 设置要得到url地址
$url = 'https://api.weixin.qq.com/sns/jscode2session?appid=' . $this->appid . '&secret=' . $this->secret . '&js_code=' . $js_code . '&grant_type=' . $this->grant_type;
curl_setopt($curl, CURLOPT_URL, $url);curl
//设置是否输出header
curl_setopt($curl, CURLOPT_HEADER, false);this
//设置是否输出结果
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
//设置是否检查服务器端的证书
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
//使用curl_exec()将curl返回的结果转换成正常数据并保存到一个变量中
$data = curl_exec($curl);
//关闭会话
curl_close($curl);
echo json_encode($data); }