Home > Enterprise >  Read data from object json with php
Read data from object json with php

Time:09-16

I am building my log system, for my software in php.

Data collection via: https://ipinfo.io/

As shown in the screenshot, just make a json_decode to read them.

Only problem for the privacy object that I can't show:

example working with for example the city parameter:

//Gets the IP Address from the visitor
$PublicIP = $_SERVER['REMOTE_ADDR'];
//Uses ipinfo.io to get the location of the IP Address, you can use another site but it will probably have a different implementation
$json = file_get_contents("http://ipinfo.io/$PublicIP/geo");
//Breaks down the JSON object into an array
$json = json_decode($json, true);
$city = $json['city'];
echo $city;

instead when I have to go into privacy, it doesn't give me anything back, what am I doing wrong?

$PublicIP = $_SERVER['REMOTE_ADDR'];
$json = file_get_contents("http://ipinfo.io/$PublicIP/geo");
$json = json_decode($json, true);
$vpn = $json["privacy"]["vpn"];
echo $vpn

CodePudding user response:

If vpn is false, then you won't see anything because false shows up as blank when echoed.

Try something like echo ($vpn == true ? "Yes" : "No"); instead. Or use var_dump($vpn);

See also PHP - Get bool to echo false when false.

CodePudding user response:

There are two issues at least:

  1. Privacy data is only available if you have a token (i.e. you signed up), and that too only if you purchased the privacy data - see their pricing page and the addons page. Then you'll have to include the token in your request.
  2. You are requesting for /geo, which will only give back geographical data. Don't put that suffix in the request URL.

I suggest you carefully read https://ipinfo.io/developers and https://ipinfo.io/developers/data-types#privacy-data in particular for your use case.

  • Related