Read data from object json with php - php

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

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.

There are two issues at least:
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.
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

Extract Host Name from "gethostbyaddr" string

I wish to extract the exact hostname from an ip address but what i am receiving is an entire string of the address.
I would just like to extract only the Host Name from the the string. Can any one point me in the right direction ?
Here is the snippet of the code being used:-
<?php
$ip = $_SERVER['REMOTE_ADDR'];
$fullhost = gethostbyname(gethostbyaddr($ip));
$host = preg_replace("/^[^.]+./", "*.", $fullhost);
?>
Host: <?=$host?>
The output received from it is :-
Host: *.36.64.182.airtelbroadband.in
I would just like to display Airtel Broadband and nothing else to the user
You can try this:
$host = gethostbyaddr($_SERVER['REMOTE_ADDR']); // gets the full hostname
$hostNames = explode(".", $host); // explodes into parts divided by DOT
echo $hostNames[count($hostNames)-2]; // take what you need
// $hostNames[count($hostNames)-1]; -> in
// $hostNames[count($hostNames)-2]; -> airtelbroadband
You might also want to look into geoip.
This is a basic PHP extension that can be installed using PECL.
string geoip_isp_by_name ( string $hostname )
The geoip_isp_by_name() function will return the name of the Internet Service Provider (ISP) that an IP is assigned to.
This function is currently only available to users who have bought a commercial GeoIP ISP Edition. A warning will be issued if the proper database cannot be located.
Consider this simple example:
<?php
$subject = 'Host: *.36.64.182.airtelbroadband.in';
$pattern = '/^Host:\s+((\*|\d+)\.){4}(.+)\..+$/';
preg_match($pattern, $subject, $tokens);
var_dump($tokens[3]);
The output obviously is:
string(15) "airtelbroadband"
That is all the information you can rip from your input. If you really want to somehow show the "publicly known company names" (as opposed to the technical network names), then you need to use some form of catalog. It is very hard to somehow retrieve such information directly from the network. You could give the whois database a try. But parsing the output of that will be a really tough task...

Getting Twitch followers using Twitch API in PHP

What I'm trying to do is make a twitch follower alert in PHP. The only stuff so far that I have gotten done is find out where to get the info from. I need assistance decoding the info, and then getting the user name and setting it to a string. The place for the recent followers is: https://api.twitch.tv/kraken/channels/trippednw/follows/?limit=1
The data Twitch's API gives you is in JSON format (JavaScript Object Notation). You can decode it using json_decode($data, true). This gives you an associative array with the required fields. For example, to get the name of the user who most recently followed:
json_decode($twitch_data, true)['follows'][0]['user']['name']
Update:
Here's a more detailed answer. First, you're going to have to get the data from the Twitch API via a get request using file_get_contents(). Then, you parse the resulting JSON using the method described above and echo that out onto the page. Here's the full code:
<?php
$api_url = 'https://api.twitch.tv/kraken/channels/trippednw/follows/?limit=1';
$api_data = file_get_contents($api_url);
$last_follow = json_decode($api_data, true)['follows'][0]['user']['name'];
echo "The last user to follow <strong>trippednw</strong> on Twitch is <strong>" . $last_follow . "</strong>.";
?>

How to get the IP address of the user and return the data in JSON

I am not a php expert. I develop android apps. In my app i am getting the user's ip address from this url http://ip2country.sourceforge.net/ip2c.php?format=JSON. As you can see when some open this url it returns some info including the IP address. I only want to get the IP and (if possible) country. Most of the time this url is busy and doesn't returns ip and gives max active user connections error. Can you please give me any php file which i can put in my own webhost and call the url to get ip. The data returned should be in json so i can parse it easily.
Thanks
<?php
$json = file_get_contents("http://ip2country.sourceforge.net/ip2c.php?format=JSON");
//this $json will have the response that the website sends.
echo json_encode($json);
?>
You can have this object wherever you call this php file and do the needful
Run this php file to check the output
Another way: EDIT
<?php
$visitor_ip = $_SERVER['REMOTE_ADDR'];
echo $visitor_ip;
$data1 = file_get_contents("http://api.hostip.info/?ip=$visitor_ip");
echo "<br> $data1";
?>
You can use PHP to get the users IP address via $_SERVER['REMOTE_ADDR']. You can then use an IP to Location lookup website to translate that into a country and merge the results:
$ip = $_SERVER['REMOTE_ADDR'];
$loc = json_decode(file_get_contents('http://ipinfo.io/'.$ip.'/json'), true);
$country = isset($loc['country']) ? $loc['country'] : 'Unknown';
$result = array('ip'=>$ip, 'country'=>$country);
header('Content-Type: application/json');
echo json_encode($result);
You get the IP address in PHP you have to use $_SERVER['REMOTE_ADDR'] then use http://ipinfo.io to pass that IP to this website. You can get the Location through JSON data, just follow the first answer on this question Getting the location from an IP address.
I have successfully implemented it by following the answer.

How to use the Foursquare API to get Venue Details

So I'm trying to get details of a specific venue using PHP. Here's my code that attempts to use a GET request to the Foursquare API to return results and then process them as JSON and display the name, address and city:
$curlhandle = curl_init();
curl_setopt($curlhandle, CURLOPT_URL, "https://api.foursquare.com/v2/venues/4b522afaf964a5200b6d27e3");
curl_setopt($curlhandle, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($curlhandle);
curl_close($curlhandle);
$json = json_decode($response);
foreach ($json->groups[0]->venues as $result)
{
echo $result->name.' - '.$result->address.' '.$result->city."<p />";
}
What am I doing wrong? I'm completely new to PHP and the Foursquare API so it could be something glaringly obvious.
You don't need to authenticate using the OAuth flow to get venue information, but you do need to add your Client ID and Client Secret to the API call.
So, the URL should be something like:
"https://api.foursquare.com/v2/venues/4b522afaf964a5200b6d27e3?client_id=CLIENT_ID&client_secret=CLIENT_SECRET
In JavaScript, the URL should be
`https://api.foursquare.com/v2/venues/${venue_id}?client_id=${CLIENT_ID}&client_secret=${CLIENT_SECRET}&v=20180323`
Note, I am using template literals and don't forget v=20180323 because the Foursquare API no longer supports requests that do not pass in a version parameter. Of course, you can modify the version number to keep updated.
You need to authenticate your request, if you go to the URL you get this.
{"meta":{"code":400,"errorType":"invalid_auth","errorDetail":"Missing access credentials. See https:\/\/developer.foursquare.com\/docs\/oauth.html for details."},"response":{}}
So I'd say you need to authenticate by following this: https://developer.foursquare.com/overview/auth.html
This worked for me: (on Python)
url = 'https://api.foursquare.com/v2/venues/{0}'.format(self.placeid)
params = dict(
client_id=self.clientid,
client_secret=self.clientsecret,
v='20170801'
)
r = requests.get(url=url, params=params)

Google maps - Facebook Places fake location using JSON

I am doing some research regarding location based social networking and I am trying to see whether I can fake a location by modifying the JSON that google returns to the Firefox browser.
Firstly, I have typed about:config in the firefox browser and got all the config settings up and changed the param of geo.wifi.uri to a page that returns JSON location below.
<?php
header('Content-type: application/json');
$longitude = "-73.98626";
$latitude = "40.75659";
$accuracy = "10";
$geoArray = array( 'location'=>array(
'latitude'=>$latitude,
'longitude'=>$longitude,
'accuracy'=>$accuracy ) ) ;
$geoJson = json_encode( $geoArray ) ;
echo $geoJson ;
?>
It has got to a stage where I can select a place and the check in button appears but when i press it, it just says loading..
I am using a firefox user agent iphone 3.0 setting to get the check-in option
Does your faked location work in something like this: http://html5demos.com/geo ? There is an access_token field in the expected response that your JSON is missing: try adding that. And you may want to add an address field as well. This is the structure you need:
{"location":
{"latitude": 40.75659,
"longitude": -73.98626,
"address":{"country":"United States","country_code":"US","region":"<statename>",
"city":"<city name>","street":"<street name>","street_number":"<number>","postal_code":"<zip>"},"accuracy":10.0},
"access_token":"<honestly not sure what this is or how it is interpreted>"}

Categories