所有分类
  • 所有分类
  • WordPress主题
  • WordPress插件
  • 发卡中心
  • 建站插件源码
  • 建站模版主题
  • 微信小程序
  • 手机Apps/Mobile
  • 游戏娱乐
  • 源码百宝箱
  • 站长工具箱
  • 知识分享

PHP获取微信用户电话号码示例代码

请求示例代码

/** 
 * 发送curl get 
 * @param string $url 
 * @return mixed 
 */ 
function curl_get($url) 
{ 
    $oCurl = curl_init(); 
    if (stripos($url, "https://") !== FALSE) { 
        curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE); 
        curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, FALSE); 
        curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1 
    } 
    if (defined('CURLOPT_IPRESOLVE') && defined('CURL_IPRESOLVE_V4')) { 
        curl_setopt($oCurl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4); 
    } 
    curl_setopt($oCurl, CURLOPT_URL, $url); 
    curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1); 
    $sContent = curl_exec($oCurl); 
    $aStatus = curl_getinfo($oCurl); 
    curl_close($oCurl); 
    if (intval($aStatus["http_code"]) == 200) { 
        return $sContent; 
    } else { 
        return false; 
    } 
} 
if (!function_exists('http_post_json')){ //这一行是判断公共方法有无这个方法,避免重名~ 
    /** 
     * PHP发送Json对象数据 
     * @param $url string 
     * @param $jsonStr string 
     * @param string[] $headers 
     * @return array 
     */ 
    function http_post_json(string $url, string $jsonStr, array $headers = array( 
        'Content-Type: application/json; charset=utf-8', 
    )): array 
    { 
        $headers[] =         'Content-Length: ' . strlen($jsonStr); 
        $ch = curl_init(); 
        curl_setopt($ch, CURLOPT_POST, 1); 
        curl_setopt($ch, CURLOPT_URL, $url); 
        curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonStr); 
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
        $response = curl_exec($ch); 
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); 
        curl_close($ch); 
        return array($httpCode, $response); 
    } 
}

调用接口代码

/**必须先进入登录状态,然后拿到phone的code去请求然后拿到access_code,请求phone的接口 */ 
            $appid = getConfig('appid_y');   //填写自己的appid,小程序中看 
            $secret = getConfig('secret_y');    //填自己的secret,公众平台看 
            $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$secret"; 
            $access_token = json_decode(curl_get($url),true);     
            if(isset($access_token['errcode'])) 
                return ['errcode'=>$access_token['errcode'],'msg'=>'请求失败','data'=>$access_token]; 
            $access_token = $access_token['access_token'];   //获取到了access_token 
            //请求电话号使用方法只能在公网能访问的目录下进行,本地进行没有返回值 
            $url = 'https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token='.$access_token; 
            $json_code = json_encode(['code'=>$param['code']]); 
            $headers = [ 
                'Accept: application/json', 
                'User-Agent: */*', 
                'Content-Type: application/json; charset=utf-8', 
            ]; 
            $phone = http_post_json($url,$json_code,$headers); 
            $phone[1] = json_decode($phone[1],true);  
            if(emptyempty($phone[1])||$phone[1]['errcode']!=0) 
                throw new Exception('系统获取手机号失败'); 
            $phoneNumber = $phone[1]['phone_info']['phoneNumber']; 
            /**拿到电话号码end */

原文链接:https://c.larjie.com/php-to-get-wechat-users/,转载请注明出处。
0

站点公告

本站SVIP会员,限时优惠!无限下载、全部功能、专属客服、资源订制、悬赏任务、专享资源... 随着资源不断增多,每90天提价10%!升级VIP 免责声明 赚钱赚积分
显示验证码
没有账号?注册  忘记密码?