小程序端wx.login 方法获取openid
通过接口传到服务器
//小程序登录
public function wxlogin(Request $request)
{
//此2个值是小程序后台提供
$appid = 'wxbf93676ffdb9a4d5';
$secret = "10fa9f759d3236de150875c30384d0db";
//小程序传过来的
$code = $request->input('code');
// return $code;
//请求地址
$url = "https://api.weixin.qq.com/sns/jscode2session?appid=%s&secret=%s&js_code=%s&grant_type=authorization_code";
$url = sprintf($url,$appid,$secret,$code);
//申请请求客户端 verify不检查证书
$client = new Client(['timeout'=>5,'verify'=>false]);
$response = $client->get($url);
$json = (string)$response->getBody();
//json转为数组
$arr = json_decode($json,true);
//写入到数据表中
try {
Renting::create(['openid'=>$arr['openid']]);
}catch (\Exception $exception){
}
return $json;
}
获取openid需要用到官方的请求地址,填写需要的参数