QQ 達人 (city.qq.com) 曾提供城市用戶搜尋介面。本文記錄歷史實作方式,供參考。注意:該服務可能已調整或下線,實際開發請以騰訊開放平台官方 API 為準。
https://city.qq.com/json.php?mod=search&act=page&callback=showSrchResult&jsontype=str&perpage=10&cd=城市代碼&nl=3&xb=0&type=city&pg=頁碼Referer: https://city.qq.com 繞過防盜鏈<?php
function fetchQqUsers($cityCode = '610900', $page = 1, $perPage = 10)
{
$url = 'https://city.qq.com/json.php';
$params = [
'mod' => 'search',
'act' => 'page',
'callback' => 'showSrchResult',
'jsontype' => 'str',
'perpage' => $perPage,
'cd' => $cityCode,
'nl' => 3,
'xb' => 0,
'type' => 'city',
'pg' => $page,
];
$context = stream_context_create([
'http' => [
'method' => 'GET',
'header' => "Referer: https://city.qq.com\r\n" .
"User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36\r\n",
'timeout' => 10,
],
]);
$jsonp = @file_get_contents($url . '?' . http_build_query($params), false, $context);
if (!$jsonp) {
return ['error' => '請求失敗'];
}
// 移除 JSONP callback 包裝: showSrchResult({...});
if (preg_match('/^\w+\((.*)\);?$/s', $jsonp, $m)) {
$json = $m[1];
} else {
$json = $jsonp;
}
$data = json_decode($json, true);
if (json_last_error() !== JSON_ERROR_NONE) {
return ['error' => 'JSON 解析失敗: ' . json_last_error_msg()];
}
return $data;
}
// 使用範例
$result = fetchQqUsers('610900', 1, 20);
if (isset($result['error'])) {
echo '錯誤: ' . $result['error'];
} else {
echo "<pre>";
print_r($result);
echo "</pre>";
}
?>
| 城市 | 代碼 (cd) |
|---|---|
| 北京 | 110000 |
| 上海 | 310000 |
| 廣州 | 440100 |
| 深圳 | 440300 |
| 成都 | 510100 |
| 西安 | 610100 |
| 武漢 | 420100 |
騰訊開放平台提供官方 API:
https://wiki.connect.qq.com/本文記錄歷史技術實作,現代開發請改用騰訊官方 OAuth 2.0 介面,確保合法合規、穩定可靠。