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

小芳 小芳 关注 LV.5 普通会员
发表于php技术学习版块 教程

请求代码:/** * 发送curl get * @param string $url * @return mixed */ function curl_get($url) { $oCurl = cur

请求代码:

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

文章说明:

本文原创发布于探乎站长论坛,未经许可,禁止转载。

题图来自Unsplash,基于CC0协议

该文观点仅代表作者本人,探乎站长论坛平台仅提供信息存储空间服务。

评论列表 评论
发布评论

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

粉丝

0

关注

0

收藏

0

已有1次打赏
轻心小站打赏给楼主10金钱,2024-04-24 22:48:02