Home  >  Article  >  Backend Development  >  How to query detailed address based on ip in PHP

How to query detailed address based on ip in PHP

醉折花枝作酒筹
醉折花枝作酒筹Original
2021-04-14 15:40:563506browse

php method to query the detailed address based on IP: 1. Use the "curl_setopt(curl_init(), set option, the value set on the option option)" function to set the transmission option; 2. Use "curl_exec ()" function, executes the session and returns data.

How to query detailed address based on ip in PHP

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer.

/**
 *  调用淘宝API根据IP查询地址
 */
public function ip_address()
{
    $ip = '219.134.104.255';
    $durl = 'http://ip.taobao.com/service/getIpInfo.php?ip='.$ip;
    // 初始化
    $curl = curl_init();
    // 设置url路径
    curl_setopt($curl, CURLOPT_URL, $durl);
    // 将 curl_exec()获取的信息以文件流的形式返回,而不是直接输出。
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true) ;
    // 在启用 CURLOPT_RETURNTRANSFER 时候将获取数据返回
    curl_setopt($curl, CURLOPT_BINARYTRANSFER, true) ;
    // 执行
    $data = curl_exec($curl);
    // 关闭连接
    curl_close($curl);
    // 返回数据
    return $data;
}

Just replace $ip with the IP you are looking for

Recommended learning:php video tutorial

The above is the detailed content of How to query detailed address based on ip in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn