0

特定の配列を取得するのに役立ちます。このスクリプトは、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

今、私はそれを1つの配列、[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

都市のみである必要があるため、IP の都市が new-york の場合は、代わりに New-york を出力します。

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

1 に答える 1

0

あなたはこれを行うことができます:

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

returnまたは、 City のみを返すように関数を変更すると、City$ip_infoと等しくなります。

return $data->City;

そして、他の情報を本当に気にしない場合は、単純なステートメントforeachを使用して、ループ内で City を具体的に探すことができます。if

...
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;
于 2013-11-05T17:45:20.880 に答える