Home > Backend Development > PHP Tutorial > How to use PHP to locate IP location

How to use PHP to locate IP location

little bottle
Release: 2023-04-06 09:12:01
forward
5198 people have browsed it

This article mainly talks about using PHP to locate the IP location. It has certain reference value. Friends in need can take a look. I hope it can help you.

The API interface provided by Taobao IP address library is used here.

Taobao IP address library: http://ip.taobao.com/instructions.html

API document description:

Usage example:

    /**
     *  调用淘宝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;
    }
Copy after login

Result:

## Related tutorials:

PHP video tutorial

The above is the detailed content of How to use PHP to locate IP location. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:cnblogs.com
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template