php 获取微信用户的openId
// 这里的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);
if (curl_errno($curl)) {
return false;
}
curl_close($curl);
$data = json_decode($output,true);
if (isset($data['openid'])){
return $data;
}
return false;
}