PHP根据手机号获取手机号基本信息(归属地、运营商)

    public function getAddress($phoneNumber)
    {
        $phoneNumber = trim($phoneNumber);

        if (!preg_match('/^1[3456789]\d{9}$/', $phoneNumber)) {
            return '';
        }

        $host = "http://mobsec-dianhua.baidu.com/dianhua_api/open/location?tel=";
        $method = "GET";
        $headers = array();
        $url = $host . $phoneNumber;

        $curl = curl_init();
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
        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_TIMEOUT, 10);//设置超时时间
        if (1 == strpos("$".$host, "https://"))
        {
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        }
        $message = curl_exec($curl);
        $addressObject = json_decode($message);
        var_dump($addressObject);
    }

 

你可能感兴趣的:(php)