#### 1. 手机号归属地查询 --- **前期准备** 没有自己的信息库去哪里查询号码信息, 当然是找一个第三方API了 **有很多网站提供了在线查询手机号归属地的方式** [https://www.chahaoba.com](https://www.chahaoba.com) [https://m.ip138.com](https://m.ip138.com) [https://www.ip138.com/sj](https://www.ip138.com/sj) #### 2. 使用第三方接口查询手机号归属地 --- PHP 手机号归属地查询接口【阿里云】 购买地址: [https://market.aliyun.com/products/57126001/cmapi00035993.html#sku=yuncode2999300001](https://market.aliyun.com/products/57126001/cmapi00035993.html#sku=yuncode2999300001) **调用示例** ```php $result = mobilePlace(1503784xxxx); halt($result); ``` ```php /** * 手机号归属地查询 * * @param $mobile 查询的手机号 * @return array resultCode 0 查询成功 -1 查询失败 */ function mobilePlace($mobile) { if (!preg_match('/^1[34578]{1}\d{9}$/', $mobile, $match)) { return ['resultCode' => -1, 'resultMsg' => '手机号格式错误']; } $api = 'https://mobapi.market.alicloudapi.com/gsd'; $appcode = '85ac5eff462e433ea373b27xxxxx'; $headers = array(); array_push($headers, "Authorization:APPCODE " . $appcode); $url = $api . "?mobile=" . $mobile; $curl = curl_init(); curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST'); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); curl_setopt($curl, CURLOPT_FAILONERROR, false); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); // curl_setopt($curl, CURLOPT_HEADER, true); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); $result = curl_exec($curl); return json_decode($result, true); } ``` **查询成功** province 归属地省份、city 归属地城市、carrier 运营商名称 ```php ^ array:6 [▼ "carrier" => "移动" "province" => "河南" "city" => "开封" "mobile" => "1503784xxxx" "resultCode" => "0" "resultMsg" => "查询成功!" ] ``` **查询失败** ```php ^ array:2 [▼ "resultCode" => -1 "resultMsg" => "手机号格式错误" ] ```