PHP through the Taobao API query IP address attribution and other information _php skills

Source: Internet
Author: User
Tags curl parse error php class sprintf

Taobao Company provides a very useful IP geographic information query interface.

Here: http://ip.taobao.com/

TaobaoIPQuery2 This class will greatly simplify the relevant information query.

Class TaobaoIPQuery2 File:

<?php/* Usage: * $IPInfo = Taobaoipquery2::getipinfo (' ipaddress ');
  * * Class taobaoipquery2{private static $_requesturl = ' http://ip.taobao.com/service/getIpInfo.php ';
    public static function Getipinfo ($IP) {$long = Ip2long ($IP);
    if ($long = = 0) {throw new Exception (' IP address error ', 5);
    $ip =long2ip ($long);
    $IPInfo = Self::queryipinfo ($IP);
  Return self::p Arsejson ($IPInfo);
    The private static function Queryipinfo ($IP) {$query = Http_build_query (An array (' IP ' => $ip));
    $ch = Curl_init ();  $options = Array (curlopt_url => sprintf ('%s?%s ', Self::$_requesturl, $query), Curlopt_returntransfer =>
      True, Curlopt_autoreferer => false, Curlopt_followlocation => false, Curlopt_header => false,
    Curlopt_timeout => 3.0,);
    Curl_setopt_array ($ch, $options);
    $content = curl_exec ($ch);
    Curl_close ($ch);
  return $content;
    private static function Parsejson ($json) {$O = Json_decode ($json, true);
    if (false = = = Is_null ($O)) {return $O;
      } if (Version_compare (php_version, ' 5.3.0 ', ' >= ')) {$errorCode = Json_last_error ();
      if (Isset (self::$_jsonparseerror[$errorCode])) {throw new Exception (self::$_jsonparseerror[$errorCode], 5);
  } throw new Exception (' JSON parse error ', 5); private static $_jsonparseerror = Array (json_error_none=> ' No ERROR has occurred ', JSON_ERROR_DEPTH=&G t; ' The maximum stack depth has been exceeded ', json_error_ctrl_char=> ' control character ERROR, possibly incorrectly  
    Encoded ', json_error_state_mismatch=> ' Invalid or malformed JSON ', json_error_syntax=> ' SYNTAX ERROR ',
Json_error_utf8=> ' Malformed UTF-8 characters, possibly incorrectly ',);

 }

TaobaoIPQuery2.Class.php:

<?php Class taobaoipquery2{private static $_requesturl = ' http://ip.taobao.com/service/getIpInfo.php ';
    public static function Getipinfo ($IP) {$long = Ip2long ($IP);
    if ($long = = 0) {throw new Exception (' IP address error ', 5);
    $ip =long2ip ($long);
    $IPInfo = Self::queryipinfo ($IP);
  Return self::p Arsejson ($IPInfo);
    The private static function Queryipinfo ($IP) {$query = Http_build_query (An array (' IP ' => $ip));
    $ch = Curl_init ();  $options = Array (curlopt_url => sprintf ('%s?%s ', Self::$_requesturl, $query), Curlopt_returntransfer =>
      True, Curlopt_autoreferer => false, Curlopt_followlocation => false, Curlopt_header => false,
    Curlopt_timeout => 3.0,);
    Curl_setopt_array ($ch, $options);
    $content = curl_exec ($ch);
    Curl_close ($ch);
  return $content;
    The private static function Parsejson ($json) {$O = Json_decode ($json, true);
    if (false = = Is_null ($O)) {  return $O;
      } if (Version_compare (php_version, ' 5.3.0 ', ' >= ')) {$errorCode = Json_last_error ();
      if (Isset (self::$_jsonparseerror[$errorCode])) {throw new Exception (self::$_jsonparseerror[$errorCode], 5);
  } throw new Exception (' JSON parse error ', 5); private static $_jsonparseerror = Array (json_error_none=> ' No ERROR has occurred ', json_error_depth=> ' The maximum stack depth has been exceeded ', json_error_ctrl_char=> ' control character ERROR, possibly incorrectly  
    Encoded ', json_error_state_mismatch=> ' Invalid or malformed JSON ', json_error_syntax=> ' SYNTAX ERROR ',
Json_error_utf8=> ' Malformed UTF-8 characters, possibly incorrectly ',);

 }

Call:

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

Let's take a look at Tencent API interface

 
 * * According to Tencent IP sharing program address to obtain IP location, more 
 accurate 
/function getiploc_qq ($queryIP) { 
  $url = ' http://ip.qq.com/ Cgi-bin/searchip?searchip1= '. $queryIP; 
  $ch = Curl_init ($url); 
  curl_setopt ($ch, curlopt_encoding, ' gb2312 '); 
  curl_setopt ($ch, Curlopt_timeout, ten); 
  curl_setopt ($ch, Curlopt_returntransfer, true); Get Data back 
  $result = curl_exec ($ch); 
  $result = mb_convert_encoding ($result, "Utf-8", "gb2312"); Code conversion, otherwise garbled 
  curl_close ($ch); 
  Preg_match ("@<span> (. *) </span></p> @iU", $result, $ipArray); 
  $loc = $ipArray [1]; 
  return $loc; 

Tencent's IP sharing program's query interface connection is: Http://ip.qq.com/cgi-bin/searchip, the connection will be followed by a get parameter searchip1, that is, searchip1= you want to query the IP address. Using PHP to send HTTP GET request to http://ip.qq.com/cgi-bin/searchip, and then obtain the corresponding results, the results in regular expressions will need to extract the location information is OK. Of course, there are a lot of ways to send get requests in PHP, and I use curl to simulate the way HTTP requests

Curl is an extension of PHP, before testing, verify that PHP has loaded the curl extension. Open the PHP configuration file php.ini, search "Extension=php_curl.dll", remove the semicolon (;) before it, and then restart the HTTP server to view Phpinfo (), and if you can see the curl information, the PHP extension curl successful

Next is Sina's API interface

Sina IP Query Interface address is: http://int.dpool.sina.com.cn/iplookup/iplookup.php, the connection is also followed by get parameters, parameters ip= you want to query the IP address, format= returned the query result format. For convenience, the returned format uses JSON, or Format=json, so that PHP extends the curl outside, and also extends JSON:

 
 * * Based on Sina IP query interface to obtain IP location 
/function Getiploc_sina ($queryIP) { 
  $url = ' http://int.dpool.sina.com.cn/ Iplookup/iplookup.php?format=json&ip= '. $queryIP; 
  $ch = Curl_init ($url); 
  curl_setopt ($ch, curlopt_encoding, ' UTF8 '); 
  curl_setopt ($ch, Curlopt_timeout, ten); 
  curl_setopt ($ch, Curlopt_returntransfer, true); Get Data back 
  $location = curl_exec ($ch); 
  $location = Json_decode ($location); 
  Curl_close ($ch); 
   
  $loc = ""; 
  if ($location ===false) return ""; 
  if (Emptyempty ($location->desc)) { 
    $loc = $location->province. $location->city. $location->district . $location->isp; 
  } else{ 
    $loc = $location->desc; 
  } 
  return $loc; 
} 

Use Getiploc_sina ("183.37.209.57") to get the address location of the IP address.

Related Article

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.