欢迎光临
我们一直在努力

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 */

上一篇:微信小程序常用api大全
下一篇:返回列表

PHP获取微信用户电话号码示例代码插图虚位以待

PHP获取微信用户电话号码示例代码插图1虚位以待
虚位以待

PHP获取微信用户电话号码示例代码插图赞助
PHP获取微信用户电话号码示例代码插图2虚位以待
最新更新
  • 01

    PHP获取微信用户电话号码示例代码
    2个月前

  • 02

    微信小程序常用api大全
    6个月前

  • 03

    微信小程序系统常用api大全
    6个月前

  • 04

    微信小程序授权登录功能详解+示例代码
    6个月前

  • 05

    微信扫码关注公众号登录功能php实战分享
    7个月前

热门推荐
  • 01

    微信视频号更新,新增功能介绍
    230热度

  • 02

    微信小程序反编译教程
    195热度

  • 03

    微信小程序反编译提取源代码方法
    128热度

  • 04

    利用URL Scheme接口无感跳转微信小程序教程
    122热度

  • 05

    微信小程序的注册和申请方法(图文)
    112热度

赞(0)
未经允许不得转载:桔子博客 » PHP获取微信用户电话号码示例代码
分享到: 更多 (0)

相关推荐

  • 暂无文章

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址