企业微信外部联系人批量获取工具 – 介绍与使用教程
一、工具介绍
核心特点
二、环境准备
1. 运行环境要求
2. 企业微信权限准备(必做)
三、快速上手教程
步骤 1:配置工具参数
步骤 2:部署与访问
步骤 3:使用核心功能
功能 1:获取所有联系人
功能 2:导出联系人数据
功能 3:刷新与重新操作
四、功能详情说明
1. 界面模块解析
2. 批量处理机制
五、常见问题解决
1. Token 获取失败
2. 联系人列表获取失败(错误码 48002/60020)
3. 部分联系人姓名获取失败
4. HTTP2 协议错误(ERR_HTTP2_PROTOCOL_ERROR)
六、扩展使用建议

代码示例:
<?php
// 修复版 - 快速获取所有联系人姓名和ID
set_time_limit(300);
header('Content-Type: text/html; charset=utf-8');
// 配置
$config = [
'corp_id' => '企业微信的id',
'app_secret' => '企业微信创建应用-应用秘钥',
'member_userid' => '你的企业微信ID-是企业微信的哈 管理员账户 查询你名下的外部联系人'
];
// HTML界面
echo "<!DOCTYPE html>
<html lang='zh-CN'>
<head>
<meta charset='UTF-8'>
<meta name='viewport' content='width=device-width, initial-scale=1.0'>
<title>快速获取所有联系人</title>
<style>
body { font-family: 'Microsoft YaHei', Arial, sans-serif; margin: 20px; background: #f8f9fa; }
.container { max-width: 1000px; margin: 0 auto; background: white; padding: 30px; border-radius: 8px; box-shadow: 0 2px 10px rgba(0,0,0,0.1); }
h1 { color: #333; text-align: center; margin-bottom: 30px; }
.success { color: #28a745; background: #d4edda; padding: 15px; border-radius: 5px; margin: 10px 0; }
.error { color: #dc3545; background: #f8d7da; padding: 15px; border-radius: 5px; margin: 10px 0; }
.loading { color: #007bff; background: #cce5ff; padding: 10px; border-radius: 5px; margin: 10px 0; }
.contact-item { background: #f8f9fa; padding: 10px; margin: 5px 0; border-radius: 3px; border-left: 3px solid #007bff; display: inline-block; width: 300px; vertical-align: top; }
.btn { display: inline-block; padding: 10px 20px; background: #007bff; color: white; text-decoration: none; border-radius: 5px; margin: 10px 5px; }
.btn:hover { background: #0056b3; }
.btn-success { background: #28a745; }
.btn-success:hover { background: #218838; }
.contact-grid { display: flex; flex-wrap: wrap; gap: 10px; }
.stats { background: #fff3cd; padding: 15px; border-radius: 5px; margin: 20px 0; }
</style>
</head>
<body>
<div class='container'>
<h1>🏢 快速获取所有联系人</h1>";
// 简化的API请求函数
function quick_api_request($url, $timeout = 5) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3);
curl_setopt($ch, CURLOPT_NOSIGNAL, 1);
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
$res = curl_exec($ch);
if (curl_errno($ch)) {
curl_close($ch);
return null;
}
curl_close($ch);
return json_decode($res, true);
}
// 显示配置信息
echo "<div style='background: #e9ecef; padding: 15px; border-radius: 5px; margin: 20px 0;'>
<strong>📋 当前配置:</strong><br>
Corp ID: {$config['corp_id']}<br>
Member UserID: {$config['member_userid']}<br>
服务器IP: " . ($_SERVER['SERVER_ADDR'] ?? '未知') . "<br>
当前时间: " . date('Y-m-d H:i:s') . "
</div>";
// 操作按钮
echo "<div style='text-align: center; margin: 20px 0;'>";
echo "<a href='?action=get_all' class='btn btn-success'>🚀 快速获取所有联系人</a> ";
echo "<a href='?action=export_simple' class='btn'>📄 导出简单列表</a> ";
echo "<a href='?' class='btn'>🔄 刷新页面</a>";
echo "</div>";
$action = isset($_GET['action']) ? $_GET['action'] : '';
if ($action == 'get_all') {
// 第一步:获取Token
echo "<h2>🔐 步骤1:获取Access Token</h2>";
$token_url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={$config['corp_id']}&corpsecret={$config['app_secret']}";
$token_data = quick_api_request($token_url, 10);
if (!$token_data || $token_data['errcode'] != 0) {
echo "<div class='error'>❌ 获取Token失败:" . ($token_data['errmsg'] ?? '网络错误') . "</div>";
exit;
}
$access_token = $token_data['access_token'];
echo "<div class='success'>✅ Access Token获取成功!</div>";
// 第二步:获取联系人列表
echo "<h2>👥 步骤2:获取联系人列表</h2>";
$list_url = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/list?access_token={$access_token}&userid={$config['member_userid']}";
$list_data = quick_api_request($list_url, 15);
if (!$list_data || $list_data['errcode'] != 0) {
echo "<div class='error'>❌ 获取联系人列表失败:" . ($list_data['errmsg'] ?? '网络错误') . "</div>";
exit;
}
if (empty($list_data['external_userid'])) {
echo "<div class='error'>📭 暂无外部联系人</div>";
exit;
}
$all_contact_ids = $list_data['external_userid'];
$total_count = count($all_contact_ids);
echo "<div class='success'>✅ 找到 {$total_count} 个联系人ID!</div>";
// 第三步:批量获取姓名和ID
echo "<h2>📝 步骤3:获取联系人姓名和ID</h2>";
echo "<div class='loading'>🚀 正在快速获取所有联系人信息(仅获取姓名和ID以提高速度)...</div>";
$contacts = [];
$batch_size = 25;
$batches = ceil($total_count / $batch_size);
for ($batch = 0; $batch < $batches; $batch++) {
$start = $batch * $batch_size;
$end = min($start + $batch_size, $total_count);
echo "<div class='loading'>🔄 处理批次 " . ($batch + 1) . "/{$batches} (联系人 " . ($start + 1) . "-{$end})</div>";
ob_flush();
flush();
for ($i = $start; $i < $end; $i++) {
$external_userid = $all_contact_ids[$i];
$detail_url = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/get?access_token={$access_token}&external_userid={$external_userid}";
$detail_data = quick_api_request($detail_url, 3);
if ($detail_data && $detail_data['errcode'] == 0 && isset($detail_data['external_contact'])) {
$contacts[] = [
'index' => $i + 1,
'name' => $detail_data['external_contact']['name'] ?? '未知',
'external_userid' => $external_userid
];
} else {
$contacts[] = [
'index' => $i + 1,
'name' => '获取失败',
'external_userid' => $external_userid
];
}
}
// 短暂休息防止API限制
usleep(50000); // 0.05秒
}
echo "<div class='success'>✅ 所有联系人信息获取完成!</div>";
// 统计信息
$success_count = 0;
foreach ($contacts as $contact) {
if ($contact['name'] != '获取失败') {
$success_count++;
}
}
echo "<div class='stats'>
<strong>📊 统计信息:</strong><br>
总联系人: {$total_count} 个<br>
成功获取: {$success_count} 个<br>
获取失败: " . ($total_count - $success_count) . " 个<br>
成功率: " . round(($success_count / $total_count) * 100, 1) . "%
</div>";
// 显示所有联系人
echo "<h2>👥 所有联系人列表</h2>";
echo "<div class='contact-grid'>";
foreach ($contacts as $contact) {
$success = $contact['name'] != '获取失败';
$bg_color = $success ? '#f8f9fa' : '#f8d7da';
$border_color = $success ? '#007bff' : '#dc3545';
echo "<div class='contact-item' style='background: {$bg_color}; border-left-color: {$border_color};'>";
echo "<strong>【{$contact['index']}】</strong><br>";
echo "<strong>姓名:</strong>" . htmlspecialchars($contact['name']) . "<br>";
echo "<strong>ID:</strong>" . htmlspecialchars($contact['external_userid']) . "<br>";
if (!$success) {
echo "<span style='color: red;'>❌ 获取失败</span>";
}
echo "</div>";
}
echo "</div>";
// 导出功能
echo "<h2>💾 导出功能</h2>";
echo "<div style='text-align: center;'>";
echo "<a href='?action=export_full' class='btn btn-success'>📊 导出完整CSV</a> ";
echo "<a href='?action=export_simple' class='btn'>📄 导出简单文本</a> ";
echo "<a href='?' class='btn'>🔄 重新获取</a>";
echo "</div>";
} elseif ($action == 'export_full') {
// 导出完整CSV
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename="wechat_all_contacts.csv"');
// 重新获取数据
$token_url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={$config['corp_id']}&corpsecret={$config['app_secret']}";
$token_data = quick_api_request($token_url, 10);
if ($token_data && $token_data['errcode'] == 0) {
$access_token = $token_data['access_token'];
$list_url = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/list?access_token={$access_token}&userid={$config['member_userid']}";
$list_data = quick_api_request($list_url, 15);
if ($list_data && $list_data['errcode'] == 0) {
$all_contact_ids = $list_data['external_userid'];
$output = fopen('php://output', 'w');
fwrite($output, "\xEF\xBB\xBF");
fputcsv($output, ['序号', '姓名', '客户ID']);
foreach ($all_contact_ids as $index => $external_userid) {
$detail_url = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/get?access_token={$access_token}&external_userid={$external_userid}";
$detail_data = quick_api_request($detail_url, 3);
$name = '获取失败';
if ($detail_data && $detail_data['errcode'] == 0 && isset($detail_data['external_contact'])) {
$name = $detail_data['external_contact']['name'] ?? '未知';
}
fputcsv($output, [$index + 1, $name, $external_userid]);
}
fclose($output);
}
}
exit;
} elseif ($action == 'export_simple') {
// 导出简单文本
header('Content-Type: text/plain; charset=utf-8');
header('Content-Disposition: attachment; filename="wechat_contacts_simple.txt"');
$token_url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={$config['corp_id']}&corpsecret={$config['app_secret']}";
$token_data = quick_api_request($token_url, 10);
if ($token_data && $token_data['errcode'] == 0) {
$access_token = $token_data['access_token'];
$list_url = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/list?access_token={$access_token}&userid={$config['member_userid']}";
$list_data = quick_api_request($list_url, 15);
if ($list_data && $list_data['errcode'] == 0) {
$all_contact_ids = $list_data['external_userid'];
echo "企业微信联系人简单列表\n";
echo "生成时间: " . date('Y-m-d H:i:s') . "\n";
echo "总计: " . count($all_contact_ids) . " 个联系人\n";
echo "================================\n\n";
foreach ($all_contact_ids as $index => $external_userid) {
echo ($index + 1) . ". " . $external_userid . "\n";
}
}
}
exit;
}
echo "</div>
</body>
</html>";
?>
声明与免责说明
- 1. 本站部分图片来源于 Unsplash,版权归原作者所有。
- 2. 本站文章、开源代码及免费下载资源仅供个人学习、研究或非商业用途参考,禁止用于商业盈利,版权归原作者所有。
- 3. 内容(含图片、文章、代码)部分转载自网络,若存在侵权,请联系 meng@yimiaonet.com 处理。
- 4. 未经本站书面许可,不得复制、盗用、采集、传播本站内容至任何平台。
- 5. 本站内容不构成专业建议,“OKMG”为注册商标,官方网站:www.okmg.cn,本站保留修改本声明的权利。

