Obtener variables desde un array
Publicado por siREZ (203 intervenciones) el 12/04/2016 15:59:20
Cordial saludo.
estoy confundido con algo que no he podido resolver y mi esperanza está en alguno de ustedes que sepa como manejar el siguiente asunto:
tengo un código que me arroja el siguiente resultado:
Array ( [domain] => not found [country] => CO - Colombia [state] => Antioquia [town] => Medellín )
esto lo imprime cuando:
$ciudad = array_column($ipInfo, 'country');print_r($ciudad);
lo que deseo es pasar a variables PHP: country, state, town, osea que $state, $country y $town tomen el valor correspondiente cuando se conecten a mi página.
el script es el siguiente:
gracias.
siREZ
estoy confundido con algo que no he podido resolver y mi esperanza está en alguno de ustedes que sepa como manejar el siguiente asunto:
tengo un código que me arroja el siguiente resultado:
Array ( [domain] => not found [country] => CO - Colombia [state] => Antioquia [town] => Medellín )
esto lo imprime cuando:
$ciudad = array_column($ipInfo, 'country');print_r($ciudad);
lo que deseo es pasar a variables PHP: country, state, town, osea que $state, $country y $town tomen el valor correspondiente cuando se conecten a mi página.
el script es el siguiente:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<?php
@$ip = $_SERVER['REMOTE_ADDR'];
//$ip='94.219.40.96';
print_r(geoCheckIP($ip));
//Array ( [domain] => dslb-094-219-040-096.pools.arcor-ip.net [country] => DE - Germany [state] => Hessen [town] => Erzhausen )
//Get an array with geoip-infodata
function geoCheckIP($ip)
{
//check, if the provided ip is valid
if(!filter_var($ip, FILTER_VALIDATE_IP))
{
throw new InvalidArgumentException("IP is not valid");
}
//contact ip-server
$response=@file_get_contents('http://www.netip.de/search?query='.$ip);
if (empty($response))
{
throw new InvalidArgumentException("Error contacting Geo-IP-Server");
}
//Array containing all regex-patterns necessary to extract ip-geoinfo from page
$patterns=array();
$patterns["domain"] = '#Domain: (.*?) #i';
$patterns["country"] = '#Country: (.*?) #i';
$patterns["state"] = '#State/Region: (.*?)<br#i';
$patterns["town"] = '#City: (.*?)<br#i';
//Array where results will be stored
$ipInfo=array();
//check response from ipserver for above patterns
foreach ($patterns as $key => $pattern)
{
//store the result in array
$ipInfo[$key] = preg_match($pattern,$response,$value) && !empty($value[1]) ? $value[1] : 'not found';
}
return $ipInfo;
}
$ciudad = array_column($ipInfo, 'country');print_r($ciudad);
?>
gracias.
siREZ
Valora esta pregunta


0