简体   繁体   中英

Separating all of the results from: 'http://ip-api.io/json/$ipaddress' geo location data. PHP

I'm not able to get the Suspicious Factors, all others ok. I'm trying to format all of the elements of http://ip-api.io/json/$ipaddress to display in a contact us email message.

My code as follows:

$ipad = getenv("REMOTE_ADDR"); 
$data = json_decode(file_get_contents("http://ip-api.io/json/$ipad"));  

$loc = "IP Address: " . $data->ip
     . "\r\n"
     . "Country Code: " . $data->country_code
     . "\r\n"
     . "Country Name: " . $data->country_name
     . "\r\n"
     . "City: " . $data->city
     . "\r\n"
     . "Region Code: " . $data->region_code
     . "\r\n"
     . "Region Name: " . $data->region_name
     . "\r\n"
     . "Zip Code: " . $data->zip_code
     . "\r\n"
     . "Time Zone: " . $data->time_zone
     . "\r\n"
     . "Latitude: " . $data->latitude
     . "\r\n"
     . "Longitude: " . $data->longitude
     . "\r\n"
     . "Metro Code: " . $data->metro_code
     . "\r\n"
     . "Suspicious factors: " ## nothing below here displays
     . "\r\n"
     . "Is Proxy: " . $data->is_proxy
     . "\r\n"
     . "Is Tor Node: " . $data->is_tor_node
     . "\r\n"
     . "Is Spam: " . $data->is_spam
     . "\r\n"
     . "Is suspicious: " . $data->is_suspicious
     . "\r\n\r\n"; 

Inspired by this past post: geo location discussion

The raw results are complete with Suspicious Factors:

{"ip":"75.132.132.182",
 "country_code":"US",
 "country_name":"United States",
 "region_code":"IL",
 "region_name":"Illinois",
 "city":"Belleville",
 "zip_code":"62221",
 "time_zone":"America/Chicago",
 "latitude":38.5121,
 "longitude":-89.8998,
 "metro_code":609,
 "suspicious_factors":
 {"is_proxy":false,
  "is_tor_node":false,
  "is_spam":false,
  "is_suspicious":false
  }
}

What handling is necessary to display each Suspicious factor?

Edit: modified as follows, result values still missing.

     . "Suspicious factors: " 
     . "\r\n"
     . "Is Proxy: " . $data->suspicious_factors->is_proxy
     . "\r\n"
     . "Is Tor Node: " . $data->suspicious_factors->is_tor_node
     . "\r\n"
     . "Is Spam: " . $data->suspicious_factors->is_spam
     . "\r\n"
     . "Is suspicious: " . $data->suspicious_factors->is_suspicious
     . "\r\n\r\n";

Try this code

    $ipad = getenv("REMOTE_ADDR"); 
    $data = json_decode(file_get_contents("http://ip-api.io/json/$ipad"));  
    $suspicious = $data->suspicious_factors;
    $loc = "IP Address: " . $data->ip
         . "\r\n"
         . "Country Code: " . $data->country_code
         . "\r\n"
         . "Country Name: " . $data->country_name
         . "\r\n"
         . "City: " . $data->city
         . "\r\n"
         . "Region Code: " . $data->region_code
         . "\r\n"
         . "Region Name: " . $data->region_name
         . "\r\n"
         . "Zip Code: " . $data->zip_code
         . "\r\n"
         . "Time Zone: " . $data->time_zone
         . "\r\n"
         . "Latitude: " . $data->latitude
         . "\r\n"
         . "Longitude: " . $data->longitude
         . "\r\n"
         . "Metro Code: " . $data->metro_code
         . "\r\n"
         . "Suspicious factors: " 
         . "\r\n"
         . "Is Proxy: " . ($data->suspicious_factors->is_proxy ? 'true' : 'false')
         . "\r\n"
         . "Is Tor Node: " . ($data->suspicious_factors->is_tor_node ? 'true' : 'false')
         . "\r\n"
         . "Is Spam: " . ($data->suspicious_factors->is_spam ? 'true' : 'false')
         . "\r\n"
         . "Is suspicious: " . ($data->suspicious_factors->is_suspicious ? 'true' : 'false')
         . "\r\n\r\n"; 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM