Taobao IP Address query class sharing (using Taobao IP library) _php instance

Source: Internet
Author: User

Taobao Company provides a very useful IP geographic information query interface. Here: http://ip.taobao.com/

The following Taobaoipquery class will greatly simplify the relevant information query.

Copy Code code as follows:

<?php

Class Taobaoipquery {

Private $m _ip;
Private $m _content;

Public function __construct ($IP) {
if (Isset ($IP)) {
$this->m_ip = $ip;
} else {
$this->m_ip = "";
}
if (!empty ($this->m_ip)) {
$url _handle = Curl_init ();
curl_setopt ($url _handle, Curlopt_url, "http://ip.taobao.com/service/getIpInfo.php?ip=". $this->m_ip);
curl_setopt ($url _handle, Curlopt_returntransfer, true);
$this->m_content = curl_exec ($url _handle);
Curl_close ($url _handle);
if ($this->m_content) {
$this->m_content = Json_decode ($this->m_content);
if ($this->m_content->{' code '} = = 1) {
Exit ("Query error!");
}
} else {
Exit ("Curl error!");
}
} else {
Exit ("IP address must is not empty!");
}
}

Public Function get_region () {
return $this->m_content->{' data '}->{' region '};
}

Public Function Get_isp () {
return $this->m_content->{' data '}->{' ISP '};
}

Public Function Get_country () {
return $this->m_content->{' data '}->{' country '};
}

Public Function get_city () {
return $this->m_content->{' data '}->{' city '};
}

}

The call is simple

Copy Code code as follows:

$ip = $_server["REMOTE_ADDR"];
$ipquery = new Taobaoipquery ($IP);
$region = $ipquery->get_region ();
$country = $ipquery->get_country ();
$city = $ipquery->get_city ();

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.