中国大陆身份证号码验证及身份证信息获取

id-card.svg?sanitize=true

一个简单的身份证号码获取用户信息工具


English Documentation


中国(大陆地区)公民身份证,数据来源于国家标准GB/T 2260-2007 (中华人民共和国行政区划代码)


安装


    composer require ofcold/identity-card

说明

一个基于中华人民共和国公民身份证的组件可以获取用户信息。这个适用于任何php框架,但是只有当php版本>=7.1时才可以。

使用

验证你的身份证号码


    //  返回false 或 Ofcold\IdentityCard\IdentityCard
    $result = Ofcold\IdentityCard\IdentityCard::make('32010619831029081');

    if ( $result === false ) {

        return '您的身份证号码不正确';
    }

    print_r($result->toArray());

或运行测试文件

    php test
$idCard = Ofcold\IdentityCard\IdentityCard::make('320106198310290811', 'en');
//  Use locale, Current supported zh-cn,en
// $idCard = Ofcold\IdentityCard\IdentityCard::make('320106198310290811', 'zh-cn');
if ( $idCard === false ) {

    return '您的身份证号码不正确';
}

$area = $idCard->getArea();
$gender = $idCard->getGender();
$birthday = $idCard->getBirthday();
$age = $idCard->getAge();
$constellation = $idCard->getConstellation();

返回结果:

{
    "area": "山西省 运城地区 运城市",
    "province": "山西省",
    "city": "运城地区",
    "county": "运城市",
    "gender": "男",
    "birthday": "1980-03-12",
    "age": 38,
    "constellation": "双鱼座"
}

Api

  • getArea():string 获取地区
  • getConstellation():string 获取星座
  • getAge():int 获取年龄
  • getBirthday(string $foramt = 'Y-m-d'):string 获取生日
  • getGender():string 获取性别
  • getCounty():string|null 获取县城
  • getCity():string|null 获取城市
  • getProvince():string|null 获取省
  • toArray():array 全部信息
  • toJson(int $option):string 全部信息

修改历史

V2.0.0

  • 新增 #2 __get()
  • 新增 #2 __toString()
  • 修改 #2 static make() 方法返回当前对象或者boolean类型
  • 移除 #2 构造方法异常验证

Repository for Github

Mujin
本帖已被设为精华帖!
本帖由系统于 5年前 自动加精
Mumujin
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
讨论数量: 27

file

文档错了,应该改为$result === false

5年前 评论
Mumujin

@PHPer
感谢感谢!马上修复 :+1: :+1:

5年前 评论

很给力啊,请问这个数据准确吗?我现在用了聚合的验证

5年前 评论
Mumujin

@hedeqiang Hi 是准确的哈!

5年前 评论

嗯嗯,我正在试着用到项目中

5年前 评论

94年 年龄是23?

5年前 评论
Mumujin

@hedeqiang 你的生日今年已经过了则为24岁,如果大于今日则23岁。如:431122199411170545, 24岁

5年前 评论

@Olivia-outshine 搜嘎,没毛病

5年前 评论

但是只是根据规则验证真伪,没有办法识别伪造的吧?

5年前 评论
Mumujin

@guansixu 是得!那个得api接口措施吧。这仅仅基础处理方法。

5年前 评论

遥想起了1年前评论的一篇文章 博客:PHP 身份证精确匹配验证

5年前 评论

就问下,有多少人是看着头像进来的。

5年前 评论

测试了下,不错哦,可以考虑用到项目中。

5年前 评论
Mumujin

@雪风 ?感谢支持

5年前 评论

之前写过一篇文章 身份证的编码规则,用这个包就方便了。

5年前 评论
Mumujin

@fanhaobai 文章写得很给力,我的包是针对18位身份证号码且新版本的php。????

5年前 评论

硬广一下,这个轮子我早在几年前就已写过,不过没有发过到论坛,最近看到一大堆重复轮子,当然百家争鸣也好,我的数据源基本上一年一更新。https://github.com/douyasi/identity-card 也提供 typescript/javascript/node 版:https://github.com/ycrao/id.js

5年前 评论

@Olivia-outshine 这篇文章评论中已回复过了

5年前 评论

为什么要求PHP 7.1+呢

5年前 评论
Mumujin

@mingyun bro, 别停留在历史……

5年前 评论
ThinkQ

这个很好用啊!

5年前 评论
Mumujin

@ThinkCsly 谢谢?

5年前 评论

试了,好用 :+1:

5年前 评论

通过身份证不能够获取他的姓名吗

5年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!