I have user Ip address. i want country name of that user. I don't want to use geoIp(plugin) so is there any way to get country name from google maps without JS support(send country name through ajax is not option) in php ?
Without JS yes you can be able,but still you need to use geo-plugin. You can use one from this site http://www.geoplugin.com/
<?php
$ip = "41.164.194.81";
$geo = unserialize(file_get_contents("http://www.geoplugin.net/php.gp?ip=$ip"));
$country = $geo["geoplugin_countryName"];
// $city = $geo["geoplugin_city"]; if you need the city as well
echo $country;
?>
Results :
South Africa
Google API doesn't have any API related to IP reverse or IP location (as far as I know of)
You can use other free website if you want to.
You can use https://freegeoip.net/json/78.194.1.99, it's free and the limit is set to 10000 calls / hours.
Code:
<?php
$ip = '78.194.1.99';
$data = #json_decode(file_get_contents('https://freegeoip.net/json/' . $ip));
$country = $data->country_name;
JSON Result:
{
"ip": "78.194.1.99",
"country_code": "FR",
"country_name": "France",
"region_code": "IDF",
"region_name": "Île-de-France",
"city": "Paris",
"zip_code": "75009",
"time_zone": "Europe/Paris",
"latitude": 48.8718,
"longitude": 2.3399,
"metro_code": 0
}
By the way, why you don't want to use a geo plugin ?
Related
I'm trying to scrap information directly from the maersk website.
Exemple, i'm trying scraping the information from this URL https://www.maersk.com/tracking/221242675
I Have a lot of tracking nunbers to update every day on database, so I dicided automate a little bit.
But, if have the following code, but its saying that need JS to work. I alredy even tryed with curl, etc.
But nothing work. Any one know another way?
I tryed the following code:
<?php
// ------------ teste 14 ------------
$html = file_get_contents('https://www.maersk.com/tracking/#tracking/221242675'); //get the html returned from the following url
echo $html;
$ETAupdate = new DOMDocument();
libxml_use_internal_errors(TRUE); //disable libxml errors
if(!empty($html)){ //if any html is actually returned
$ETAupdate->loadHTML($html);
libxml_clear_errors(); //remove errors for yucky html
$ETA_xpath = new DOMXPath($ETAupdate);
//get all the h2's with an id
$ETA_row = $ETA_xpath->query('//strong');
if($ETA_row->length > 0){
foreach($ETA_row as $row){
echo $row->nodeValue . "<br/>";
}
}
}
?>
You need to scrape the data directly from their API requests, rather than trying to scrape the page URL directly (Unless you're using something like puppeteer, but I really don't recommend that for this simple task)
I took a look at the site and the API endpoint is:
https://api.maersk.com/track/221242675?operator=MAEU
This will return a JSON-formatted response which you can parse and use to extract the details. It'll also give you a much easier method to access the data rather than parsing the HTML. Example below.
{
"tpdoc_num": "221242675",
"isContainerSearch": false,
"origin": {
"terminal": "YanTian Intl. Container Terminal",
"geo_site": "1PVA2R05ZGGHQ",
"city": "Yantian",
"state": "Guangdong",
"country": "China",
"country_code": "CN",
"geoid_city": "0L3DBFFJ3KZ9A",
"site_type": "TERMINAL"
},
"destination": {
"terminal": "DCT Gdansk sa",
"geo_site": "02RB4MMG6P32M",
"city": "Gdansk",
"state": "",
"country": "Poland",
"country_code": "PL",
"geoid_city": "3RIGHAIZMGKN3",
"site_type": "TERMINAL"
},
"containers": [ ... ]
}
im trying to do this json decoding my professor suggested i try, but i cant seem to pass the address part. Any ideas?
{"fname": "Nick", "mname": "F.", "lname": "Delos Reyes", "birthday": "1995-04-01", "address": { "Country": "America", "state": "New York" }}
Expected output:
Name : Nick F. Delos Reyes
Birthday : 1995-04-01
Address :Caramoran, New York
There are many ways to it. Following is an example to parse the name.
$array = json_decode('{"fname": "Nick", "mname": "F.", "lname": "Delos Reyes", "birthday": "1995-04-01", "address": { "Country": "America", "state": "New York" }}', true);
$name = [$array['fname'], $array['mname'], $array['lname']];
$outputName = 'Name:' . implode(' ', $name);
It uses json_decode() to convert the JSON to an associative array. It then put the name parts in an array. It uses implode() to join the name parts into a string.
This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 6 years ago.
I'm looking to create a basic IP tracker, I'm trying to figure out how to echo certain aspects of this variable, cheers:
{"ip":"MY IP","country_code":"GB","country_name":"United Kingdom","region_code":"ENG","region_name":"England","city":"MY CITY","zip_code":"MY CODE","time_zone":"Europe/London","latitude":----,"longitude":-----,"metro_code":-}
This is the code which I was reading up on:
$location = file_get_contents('http://freegeoip.net/json/'.$_SERVER['REMOTE_ADDR']);
print_r($location);
{
"ip": "77.99.179.98",
"country_code": "GB",
"country_name": "United Kingdom",
"region_code": "H9",
"region_name": "London, City of",
"city": "London",
"zipcode": "",
"latitude": 51.5142,
"longitude": -0.0931,
"metro_code": "",
"areacode": ""
}
Cheers!
To echo something do
//Example
$locationObject = json_decode($location);
echo $locationObject->ip;
//Prints out: 77.99.179.98
More info
http://www.dyn-web.com/tutorials/php-js/json/decode.php
I want to find the location (country, city,..) of my site visitor by their IP.
I'm coding php.
who can help me?
something like this:
$url = json_decode(file_get_contents("http://api.ipinfodb.com/v3/ip-city/?key=/*userapikey*/
ip=".$_SERVER['REMOTE_ADDR']."&format=json"));
$country=$url->countryName; // user country
$city=$url->cityName; // city
$region=$url->regionName; // regoin
$latitude=$url->latitude; //lat and lon
$longitude=$url->longitude;
Seems there is a & missing in the URL:
$url = json_decode(file_get_contents("http://api.ipinfodb.com/v3/ip-city/?key=/*userapikey*/&ip=".$_SERVER['REMOTE_ADDR']."&format=json"));
Probably, you should encode the IP:
$url = json_decode(file_get_contents("http://api.ipinfodb.com/v3/ip-city/?key=/*userapikey*/&ip=".urlencode($_SERVER['REMOTE_ADDR'])."&format=json"));
Then you can do this to get information about the result:
var_dump($url);
If you have another problem, please write it to the question.
Have a look at freegeoip.net. It's a webservice that gives you exactly the data you need for a specific IP.
E.g.:
https://freegeoip.net/json/1.2.3.4
will give you this JSON data:
{
ip: "1.2.3.4",
country_code: "US",
country_name: "USA",
region_code: "WA",
region_name: "Washington",
city: "Mukilteo",
zip_code: "98275",
time_zone: "America/Los_Angeles",
latitude: 47.945,
longitude: -122.305,
metro_code: 819
}
Check out https://freegeoip.net/. They offer a free API for up to 10,000 searches per hour.
Example:
$res = json_decode(file_get_contents("https://freegeoip.net/json/109.80.75.20"));
$countryCode = $res->country_code;
$country = $res->country_name;
Is there a way to do a PHP If statement based on the persons location?
My problem comes down to Amazon Affiliate links. The link to buy a DVD on Amazon.co.uk is different to the one to buy from Amazon.com and I want a way to only show the correct one.
At the same time, If they aren't based in either country, then I don't want the link to show in the first place.
Example:
If Location = UK; print "amazon-UK-link"
If Location = US; print "amazon-US-link"
If location = None of the above; print nothing
You can use: string geoip_country_code_by_name ( string $hostname )
Example:
<?php
$country = geoip_country_code_by_name('www.example.com');
if ($country) {
echo 'This host is located in: ' . $country;
}
?>
Output:
This host is located in: US
For your case you can use: geoip_country_code_by_name($_SERVER['REMOTE_ADDR']); to get the country code for the current user.
You're going to have to use the visitor's IP address to lookup their physical location. Apart from using the GeoIP extension for PHP (as stewe pointed out), there are two ways of doing this:
The easy way
Use an external service like http://www.hostip.info
With your own MySQL data
1.) Retrieve the visitors' IP address:
if (getenv('HTTP_X_FORWARDED_FOR'))
{
$ip_address = getenv('HTTP_X_FORWARDED_FOR');
}
else
{
$ip_address = getenv('REMOTE_ADDR');
}
2.) Convert the visitor's IP address to an IP Number:
$ips = explode(".",$ip_address);
return ($ips[3] + $ips[2] * 256 + $ips[1] * 256 * 256 + $ips[0] * 256 * 256 * 256);
3.) Locate the IP Number from your database which you can download here.
For example: the IP Address 202.186.13.4 converts to IP Number 3401190660. It is between the beginning and the ending of the following IP numbers:
Beginning_IP | End_IP | Country | ISO
-------------+-------------+----------+----
3401056256 | 3401400319 | MALAYSIA | MY
You need to use geoip for that by maxmind http://www.maxmind.com/app/ip-location or there's another option, search for an IP to Country API, there some on the web.
You can use a simple API from http://www.geoplugin.net/
$xml = simplexml_load_file("http://www.geoplugin.net/xml.gp?ip=".getRealIpAddr());
echo $xml->geoplugin_countryName ;
echo "<pre>" ;
foreach ($xml as $key => $value)
{
echo $key , "= " , $value , " \n" ;
}
function getRealIpAddr()
{
if (!empty($_SERVER['HTTP_CLIENT_IP'])) //check ip from share internet
{
$ip=$_SERVER['HTTP_CLIENT_IP'];
}
elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) //to check ip is pass from proxy
{
$ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
}
else
{
$ip=$_SERVER['REMOTE_ADDR'];
}
return $ip;
}
Output
United States
geoplugin_city= San Antonio
geoplugin_region= TX
geoplugin_areaCode= 210
geoplugin_dmaCode= 641
geoplugin_countryCode= US
geoplugin_countryName= United States
geoplugin_continentCode= NA
geoplugin_latitude= 29.488899230957
geoplugin_longitude= -98.398696899414
geoplugin_regionCode= TX
geoplugin_regionName= Texas
geoplugin_currencyCode= USD
geoplugin_currencySymbol= $
geoplugin_currencyConverter= 1
It makes you have so many options you can play around with
Thanks
:)