2017-10-20 219 views
0

嘿,IP长字符串过滤

我试图只显示一长串我从API我得到了FRAM邮递​​员获得的1位,我需要证明的唯一的事情是城市。我如何需要这样做?

我试图找到与PHP的一个方式,但我不知道什么(使用谷歌的IP IM只是这个问题)做

a:14:{s:10:"regionName";s:10:"California";s:6:"status";s:7:"success";s:4:"city";s:13:"Mountain View";s:8:"timezone";s:19:"America/Los_Angeles";s:7:"country";s:13:"United States";s:11:"countryCode";s:2:"US";s:3:"zip";s:0:"";s:3:"lon";d:-122.08499908447266;s:3:"isp";s:6:"Google";s:2:"as";s:19:"AS15169 Google Inc.";s:5:"query";s:7:"8.8.8.8";s:6:"region";s:2:"CA";s:3:"lat";d:37.42290115356445;s:3:"org";s:6:"Google";} 

这样的长度城市名称变更!

在那里我得到它FRM的http://ip-api.com/php/8.8.8.8 的网站,我使用的代码:

$curl = curl_init(); 

curl_setopt_array($curl, array(
    CURLOPT_URL => "http://ip-api.com/php/8.8.8.8", 
    CURLOPT_RETURNTRANSFER => true, 
    CURLOPT_ENCODING => "", 
    CURLOPT_MAXREDIRS => 10, 
    CURLOPT_TIMEOUT => 30, 
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, 
    CURLOPT_CUSTOMREQUEST => "GET", 
    CURLOPT_HTTPHEADER => array(
     "cache-control: no-cache", 
     "postman-token: 2e83e542-a6fb-5bb6-94e0-c1908282a2a2" 
    ), 
)); 

$response = curl_exec($curl); 
$err = curl_error($curl); 

curl_close($curl); 

if ($err) { 
    echo "cURL Error #:" . $err; 
} else { 
    echo $response; 
} 
+0

这是什么?一个字符串,json,..? – Naruto

+0

这是一个字符串,它是'serialize'调用的结果。你可以通过'unserialize'来获得原始数据 – apokryfos

+0

的卷曲,这是我从http://ip-api.com/php/8.8.8.8获得的数据。 – Bram

回答

0

这是运行在一个PHP PHP变量serialize你可以扭转它的产品unserialize

$curl = curl_init(); 

curl_setopt_array($curl, array(
    CURLOPT_URL => "http://ip-api.com/php/8.8.8.8", 
    CURLOPT_RETURNTRANSFER => true, 
    CURLOPT_ENCODING => "", 
    CURLOPT_MAXREDIRS => 10, 
    CURLOPT_TIMEOUT => 30, 
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, 
    CURLOPT_CUSTOMREQUEST => "GET", 
    CURLOPT_HTTPHEADER => array(
     "cache-control: no-cache", 
     "postman-token: 2e83e542-a6fb-5bb6-94e0-c1908282a2a2" 
    ), 
)); 

$response = curl_exec($curl); 
$err = curl_error($curl); 
$responseArray = unserialize($response); //You probably need some error trapping here 

curl_close($curl); 

if ($err) { 
    echo "cURL Error #:" . $err; 
} else { 
    echo $responseArray["country"]; 
}