爱玺玺

爱玺玺的生活日记本。wx:lb87626

通过企微的externalUserId查询到公众号的unionId

<?php

/**

 * 这个文件主要目的是实现了通过企微的externalUserId查询到公众号的unionId

 * limz

 */


/**

 * 获取企微获取客户信息用的token

 * @return string

 */

function getQwCusToken(){

    $url = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=ww891409cb13bc881e&corpsecret=Aawsz9WEVGXmSvRC0pjf218pIGQOdmhfSg4IZwntXls';

// 初始化 cURL

    $ch = curl_init();


// 设置 cURL 选项

    curl_setopt($ch, CURLOPT_URL, $url);

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    curl_setopt($ch, CURLOPT_TIMEOUT, 60); // 设置超时时间(秒)


// 发送请求并获取返回结果

    $response = curl_exec($ch);


// 检查是否有错误发生

    if (curl_errno($ch)) {

        $error = curl_error($ch);

        // 可以选择抛出异常或返回错误信息

        return "cURL Error: " . $error;

    }


// 关闭 cURL 资源

    curl_close($ch);


    $data = json_decode($response);


// 检查是否解码成功

    if ($data !== null && $data->errcode === 0) {

        // 提取access_token

        return $access_token = $data->access_token;

    } else {

        // 解码失败或errcode不为0,处理错误情况

        return $access_token = '{}';

    }

}


/**

 * 设置缓存 limz

 * @param $cname 缓存名

 * @param $token

 * @param $expiration

 * @return true

 */

function setCache($cname,$token, $expiration = 3600) {

    $cacheDir = 'cache/directory'; // 设置缓存目录

    $tokenFile = $cacheDir . '/'.$cname.'.cache';


    // 验证缓存目录是否存在,不存在则创建

    if (!is_dir($cacheDir)) {

        mkdir($cacheDir, 0755, true);

    }


    // 设置 token 和过期时间

    $data = [

        'token' => $token,

        'expire' => time() + $expiration, // 过期时间为当前时间加上设置的秒数

    ];


    // 将数据保存为 JSON 字符串并写入文件

    file_put_contents($tokenFile, json_encode($data));


    return true;

}


/**

 * 获取缓存

 * @param $cacheDir

 * @return false|mixed

 */

function getCache($cname) {

    $cacheDir = 'cache/directory';

    $tokenFile = $cacheDir . '/'.$cname.'.cache';


    // 检查文件是否存在且未过期

    if (file_exists($tokenFile) && (time() < (filemtime($tokenFile) + 3600))) {

        // 从文件中读取数据

        $data = json_decode(file_get_contents($tokenFile), true);


        // 检查数据是否有效(例如,是否包含 token 和 expire 字段)

        if (isset($data['token']) && isset($data['expire']) && $data['expire'] > time()) {

            return $data['token']; // 返回未过期的 token

        }

    }


    // 如果文件不存在、已过期或数据无效,则返回 false 或 null

    return false;

}



//echo getQwCusToken();

/**

 * 通过缓存机制获取企微access_token

 * @return false|mixed|string

 */

function getCacheQwToken(){

    if(!getCache('qwtoken')){

        $qwToken=getQwCusToken();

        setCache('qwtoken',$qwToken,7200);

        return $qwToken;

    }else{

        return getCache('qwtoken');

    }

}


/**

 * 获取企微用户信息 主要是unionId

 * @param $accessToken

 * @param $externalUserId

 * @return void

 */

function getQwCusDetail($accessToken,$externalUserId){

    $url = 'https://qyapi.weixin.qq.com/cgi-bin/externalcontact/get?access_token='.$accessToken.'&external_userid='.$externalUserId;

// 初始化 cURL

    $ch = curl_init();


// 设置 cURL 选项

    curl_setopt($ch, CURLOPT_URL, $url);

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    curl_setopt($ch, CURLOPT_TIMEOUT, 60); // 设置超时时间(秒)


// 发送请求并获取返回结果

    $response = curl_exec($ch);


// 检查是否有错误发生

    if (curl_errno($ch)) {

        $error = curl_error($ch);

        // 可以选择抛出异常或返回错误信息

        return "cURL Error: " . $error;

    }


// 关闭 cURL 资源

    curl_close($ch);


    $data = json_decode($response);


// 检查是否解码成功

    if ($data !== null && $data->errcode === 0) {

        return $data;

    } else {

        return '{}';

    }

}


$cusDetail=getQwCusDetail(getCacheQwToken(),'wmGnVoDAAAg4phRnOBHuxumRlOe8IrMQ');

print_r($cusDetail);


发表评论:

Powered By Z-BlogPHP 1.4 Deeplue Build 150101

Copyright Your WebSite.Some Rights Reserved.

蜀ICP备11021721号-5