PHP code example of xu42 / china-same-name
1. Go to this page and download the library: Download xu42/china-same-name library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
xu42 / china-same-name example snippets
const CONFIG = [
'henan' => [
... //此处配置省略
],
'taiwang' => [
// common 为本省一些通用的配置, 例如method\key\pattern等,优先级低于具体地级市配置
'common' => [
'method' => 'get',
'key' => 'data.count',
],
// default 为全省的统计
'default' => [
'method' => 'get', //get or post
'url' => 'https://people.gov.cn/count.json?name=@NAME@', // @NAME@会被替换为URL编码后的姓名 (post暂不支持)
'key' => 'data.count', //对于接口返回是Json格式的,可以通过此字段进行提取 {'data':{'count':123}}
'pattern' => '#all(\d+)people#', //对于接口返回非Json格式的文本的,可以通过正则匹配进行提取(暂不支持 欢迎提交PR或后续有需求再加)
'params' => [], //method=post时的参数 (post暂不支持)
],
// 台北市统计
'taibei' => [
... //此处配置省略
],
],
... //此处配置省略
];
php
查询在北京与张伟同名的人数
$count = \Xu42\ChinaSameName\ChinaSameName::count('张伟', 'beijing');
// 查询在河南全省与张伟同名的人数
$count = \Xu42\ChinaSameName\ChinaSameName::count('张伟', 'henan');
// 查询在河南郑州与张伟同名的人数
$count = \Xu42\ChinaSameName\ChinaSameName::count('张伟', 'henan.zhengzhou');
var_dump($count);