public function getPhoneNumber(Request $request)
{
$token = $this->getAccessToken();
$phone = $this->getPhone($request->param('code'),$token['access_token']);
if ($phone['errcode'] == -1){
return error('获取手机号失败');
}
return success($phone);
}
public function getAccessToken(){
$wechat = config('easywechat.easywechat');
$appid = $wechat['app_id'];
$secret = $wechat['secret'];
$get_token_url = 'https://api.weixin.qq.com/cgi-bin/token?appid=' . $appid . '&secret=' . $secret . '&grant_type=client_credential';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $get_token_url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
$res = curl_exec($ch);
curl_close($ch);
return json_decode($res, true);
}
public function getPhone($code , $token){
$url = 'https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=' . $token;
$request_data = array(
'code' => $code
);
$return = json_decode($this->https_request($url, $request_data, 'json'), true);
return $return;
}
public function https_request($url, $data, $type)
{
if ($type == 'json') {
$headers = array("Content-type: application/json;charset=UTF-8", "Accept: application/json", "Cache-Control: no-cache", "Pragma: no-cache");
$data = json_encode($data);
}
$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);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
$output = curl_exec($curl);
curl_close($curl);
return $output;
}