// 这里的code是通过前端页面获取的 //$appid = "公众号在微信的appid"; //$secret = "公众号在微信的app secret"; public function userOpenId($appid,$secret,$code) { $url = "https://api.weixin.qq.com/sns/jscode2session?appid=$appid&secret=$secret&js_code=$code&grant_type=authorization_code"; $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE); if (!empty($data)) { curl_setopt($curl, CURLOPT_POST, 1); curl_setopt($curl, CURLOPT_POSTFIELDS, $data); } curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); $output = curl_exec($curl); curl_close($curl); $data = json_decode($output,true); if (isset($data['openid'])){ return $data; } return false; }
标签:openId,code,setopt,微信,secret,curl,php,data,CURLOPT From: https://www.cnblogs.com/dream-meng/p/18277930