发布和网页请求

时间:2013-06-25 21:25:32

标签: php html curl

我有一个记录每个成员的IP地址,浏览器和操作系统的系统。

我想以某种方式实施this

无论如何,我可以将用户IP发布到他们的网站,提取ISP和国家/地区等值,并将它们存储在我的本地MySQL数据库中,以便在对某些滥用用户运行查询时快速访问?

4 个答案:

答案 0 :(得分:0)

IP到位置并不总是准确且可以轻松克服,但this might help you

答案 1 :(得分:0)

PHP对Maxmind的GeoIP服务提供原生支持。有关详细信息,请参阅this PECL extension

答案 2 :(得分:0)

查看此网站:http://ipinfodb.com/ip_location_api.php

他们提供API将ip地址传递给服务,以返回XML / JSON中的地理位置,然后可以在PHP脚本上解析。

如何使用它的示例如下所示:

<?php
include('ip2locationlite.class.php');

//Load the class
$ipLite = new ip2location_lite;
$ipLite->setKey('<your_api_key>');

//Get errors and locations
$locations = $ipLite->getCity($_SERVER['REMOTE_ADDR']);
$errors = $ipLite->getError();

//Getting the result
echo "<p>\n";
echo "<strong>First result</strong><br />\n";
if (!empty($locations) && is_array($locations)) {
  foreach ($locations as $field => $val) {
    echo $field . ' : ' . $val . "<br />\n";
  }
}
echo "</p>\n";

//Show errors
echo "<p>\n";
echo "<strong>Dump of all errors</strong><br />\n";
if (!empty($errors) && is_array($errors)) {
  foreach ($errors as $error) {
    echo var_dump($error) . "<br /><br />\n";
  }
} else {
  echo "No errors" . "<br />\n";
}
echo "</p>\n";
?>

也许这会让你朝着正确的方向前进?

答案 3 :(得分:0)

如果你真的想从http://www.iplocation.net/提取数据,那么这里有一个快速的function。但要使用此功能,您需要下载并包含PHP Simple HTML DOM Parser

这是代码

<?php
  require_once( "path to simplehtmldom.php" );

  $ip_info  = ip_info( "223.196.190.40", 1 );
  print_r( $ip_info );
  /**
   * It will output...
    Array
    (
      [IP Address] => 223.196.190.40
      [Country] => India
      [Region] => Maharashtra
      [City] => Pune
      [ISP] => Idea Isp Subscriber Ip Pool
    )
  **/


  /**
   * ip_info()
   * @param $ip - IP address you want to fetch data from
   * @param $provider IP provider ( 1 = IP2Location, 2 = IPligence, 3 = IP Address Labs, 4 = MaxMind )
   * @return array
   */
  function ip_info( $ip = "127.0.0.1", $provider = 1 ) {
    $indx = array(
      1 =>  10,
      2 =>  11,
      3 =>  12,
      4 =>  13
    );
    $data = array();
    $url  = "http://www.iplocation.net/index.php";
    $ch   = curl_init();
    curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
    curl_setopt( $ch, CURLOPT_FRESH_CONNECT, true );
    curl_setopt( $ch, CURLOPT_FORBID_REUSE, true );
    curl_setopt( $ch, CURLOPT_HEADER, false );
    curl_setopt( $ch, CURLOPT_NOBODY, false );
    curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
    curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, 2 );
    curl_setopt( $ch, CURLOPT_BINARYTRANSFER, false );
    curl_setopt( $ch, CURLOPT_REFERER, $url );
    curl_setopt( $ch, CURLOPT_URL, $url );
    curl_setopt( $ch, CURLOPT_POST, true );
    curl_setopt( $ch, CURLOPT_POSTFIELDS, "query=".urlencode( $ip )."&submit=Query" );
    $response = curl_exec( $ch );

    $html = str_get_html( $response );
    if ( $table = $html->find( "table", $indx[$provider] ) ) {
      if ( $tr1 = $table->find( "tr", 1 ) ) {
        if ( $headers = $tr1->find( "td" ) ) {
          foreach( $headers as $header ) {
            $data[trim( $header->innertext )] = null;
          }
        }
      }
      if ( $tr2 = $table->find( "tr", 3 ) ) {
        reset( $data );
        if ( $values = $tr2->find( "td" ) ) {
          foreach( $values as $value ) {
            $data[key( $data )] = trim( $value->plaintext );
            next( $data );
          }
        }
      }
    }
    unset( $html, $table, $tr1, $tr2, $headers, $values );
    return $data;
  }
?>