Home  >  Article  >  Backend Development  >  php curl API code to obtain all information of a specified IP

php curl API code to obtain all information of a specified IP

WBOY
WBOYOriginal
2016-07-25 08:57:171168browse
This article introduces the API code for using the curl function in PHP to obtain all information under a specified IP address. Friends in need can refer to it.

Share the api code that uses curl to obtain all information under the specified IP. For related information about curl, you can refer to the following articles: Simple example of php curl uploading files Simple example of php curl post php curl application example analysis Example code of php curl usage php code for using curl to determine whether a remote file exists php uses curl to fake IP source code php curl learning summary Charge it first, and then look at the code below. The code is as follows:

<?php
/**
* curl取指定IP信息的API代码
* edit by bbs.it-home.org
*/
function getIpInfo($ip,$timeout=15) {
    if(!function_exists('curl_init') or !function_exists('simplexml_load_string')) return false;
    $ch = curl_init("http://bbs.it-home.org/ip_query2.php?ip={$ip}&timezone=true");
    $options = array(
        CURLOPT_RETURNTRANSFER => true,
    );
    curl_setopt_array($ch,$options);
    $res = curl_exec($ch);
    curl_close($ch);
    if($xml = simplexml_load_string($res)) {
        $dt = array();
        foreach ($xml->Location->children() as $key=>$item)  {
            $dt[$key] = strtolower($item);
        }
        return $dt;
    } else {
        return false;
    }
}
$current_Ip_Info = getIpInfo('61.164.140.55');
var_dump($current_Ip_Info);  

/*
返回内容:
array(13) {
  ["Ip"]=>
  string(13) "61.164.140.55"
  ["Status"]=>
  string(2) "ok"
  ["CountryCode"]=>
  string(2) "cn"
  ["CountryName"]=>
  string(5) "china"
  ["RegionCode"]=>
  string(2) "02"
  ["RegionName"]=>
  string(8) "zhejiang"
  ["City"]=>
  string(5) "ruian"
  ["ZipPostalCode"]=>
  string(0) ""
  ["Latitude"]=>
  string(7) "27.7814"
  ["Longitude"]=>
  string(7) "120.628"
  ["TimezoneName"]=>
  string(14) "asia/chongqing"
  ["Gmtoffset"]=>
  string(5) "28800"
  ["Isdst"]=>
  string(1) "0"
}
*/
?>


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