【问题标题】:Get one array from output从输出中获取一个数组
【发布时间】:2013-11-05 16:33:15
【问题描述】:

帮助获取特定数组。 这个脚本的作用是去http://www.iplocation.net/index.php?query=223.196.190.40&submit=Query获取国家、城市、ips等信息,然后像这样输出;

      [IP Address] => xxx.xxx.xxx.xxx
      [Country] => Country
      [Region] => Region
      [City] => City
      [ISP] => Provider

现在我只希望它得到一个数组,即 [City] 的数组

这是我现在拥有的代码;

    <?php
  require_once( "simple_html_dom.php" );

  $ip_info  = ip_info( $_SERVER['REMOTE_ADDR'], 1 );
  print_r( $ip_info );
  /**
   * It will output...
    Array
    (
      [IP Address] => xxx.xxx.xxx.xxx
      [Country] => Country
      [Region] => Region
      [City] => City
      [ISP] => Provider
    )
  **/


  /**
   * 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;
  }
?>

这将输出

  [IP Address] => xxx.xxx.xxx.xxx
      [Country] => Country
      [Region] => Region
      [City] => City
      [ISP] => Provider

它必须是唯一的城市,所以如果城市是 new-york 对于 IP 而不是输出 New-york 而不是

  [IP Address] => xxx.xxx.xxx.xxx
  [Country] => Country
  [Region] => Region
  [City] => City
  [ISP] => Provider

【问题讨论】:

    标签: php curl location ip


    【解决方案1】:

    你可以这样做:

    $ip_info = ip_info($_SERVER['REMOTE_ADDR'], 1);
    $city = $ip_info->City;
    

    或者修改函数的return只返回城市,这样$ip_info就等于城市:

    return $data->City;
    

    如果您真的不关心其他信息,您可以在 foreach 循环中使用简单的 if 语句专门查找 City:

    ...
    foreach( $headers as $header ) {
        if (trim( $header->innertext ) == 'City') {
            $data[trim( $header->innertext )] = null;
        }
    }
    ...
    foreach( $values as $value ) {
        if (key( $data )) == 'City') {
            $data[key( $data )] = trim( $value->plaintext );
            next( $data );
        }
    }
    ...
    return $data->City;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-26
      • 1970-01-01
      • 2019-04-05
      • 1970-01-01
      相关资源
      最近更新 更多