i am using http://api.easyjquery.com/ips/?ip=115.119.253.90&full=true this api for getting current location but when this site goes down i am not able to get current location.
can google provide any api to find current location using ip.
i am using http://api.easyjquery.com/ips/?ip=115.119.253.90&full=true this api for getting current location but when this site goes down i am not able to get current location.
can google provide any api to find current location using ip.
the solution of this question is
<?php
$url = str_replace(' ','-',$sport['title']);
$url = str_replace(":",'',$url);
$url = str_replace("'",'',$url);
?>
<a href="<?php echo base_url().'news/readmore/'.$sport['id']."/".urlencode($url);?>"><?php echo $sport_top['title'];?></a>
There is no way to get the user location using PHP since it's running on the server side. You can get the user location using javascript through the browser.
Here is an example. In this example I separated the code in two files. One for processing and storing the information using PHP (geocoordinates.php) and another one (HTML) for collecting the geocoding informantion (index.html), index.html.
You could combine both files into index.php but I'll keep them separated for simplicity.
geocoordinates.php
<?php
if(isset($_POST['lat'], $_POST['lng'])) {
$lat = $_POST['lat'];
$lng = $_POST['lng'];
$url = sprintf("https://maps.googleapis.com/maps/api/geocode/json?latlng=%s,%s", $lat, $lng);
$content = file_get_contents($url); // get json content
$metadata = json_decode($content, true); //json decoder
if(count($metadata['results']) > 0) {
// for format example look at url
// https://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452
$result = $metadata['results'][0];
// save it in db for further use
echo $result['formatted_address'];
}
else {
// no results returned
}
}
?>
index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Geocoding Page</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script>
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(savePosition, positionError, {timeout:10000});
} else {
//Geolocation is not supported by this browser
}
}
// handle the error here
function positionError(error) {
var errorCode = error.code;
var message = error.message;
alert(message);
}
function savePosition(position) {
$.post("geocoordinates.php", {lat: position.coords.latitude, lng: position.coords.longitude});
}
</script>
</head>
<body>
<button onclick="getLocation();">Get My Location</button>
</body>
</html>
Keep in mind that in this example the once the user clicks "Get My Location" the browser will prompt the user to allow the geolocation. You could also call the getLocation function once the page loads, but the browser will always ask for the user's permission
You can learn more about geolocation at http://www.w3schools.com/htmL/html5_geolocation.asp
You should be using a service account for this. Accessing the Google APIs using a service account is quite useful. Some times you just want to access your own data and not data owned by other users. In this instance there is no reason to use OAuth2 and prompt a user to give you access to there information, its your information you already have access. This is why we use a service account.
Google Service Account Example with in the PHP client lib can be found here: Example
You don't say exactly which API you are trying to access, and I dislike link only answers. So here is a basic example using the Google Analytics API.
<?php
session_start();
require_once 'Google/Client.php';
require_once 'Google/Service/Analytics.php';
/************************************************
The following 3 values an befound in the setting
for the application you created on Google
Developers console.
The Key file should be placed in a location
that is not accessable from the web. outside of
web root.
In order to access your GA account you must
Add the Email address as a user at the
ACCOUNT Level in the GA admin.
************************************************/
$client_id = '1046123799103-nk421gjc2v8mlr2qnmmqaak04ntb1dbp.apps.googleusercontent.com';
$Email_address = '1046123799103-nk421gjc2v8mlr2qnmmqaak04ntb1dbp@developer.gserviceaccount.com';
$key_file_location = '629751513db09cd21a941399389f33e5abd633c9-privatekey.p12';
$client = new Google_Client();
$client->setApplicationName("Client_Library_Examples");
$key = file_get_contents($key_file_location);
// seproate additional scopes with a comma
$scopes ="https://www.googleapis.com/auth/analytics.readonly";
$cred = new Google_Auth_AssertionCredentials(
$Email_address,
array($scopes),
$key
);
$client->setAssertionCredentials($cred);
if($client->getAuth()->isAccessTokenExpired()) {
$client->getAuth()->refreshTokenWithAssertion($cred);
}
$service = new Google_Service_Analytics($client);
I have a tutorial that goes along with that code it can be found here. Google Service Account PHP
What you are looking to do is get the position using the LocationManager.NETWORK_PROVIDER
instead of LocationManager.GPS_PROVIDER
. The NETWORK_PROVIDER
will resolve on the GSM or wifi, which ever available. Obviously with wifi off, GSM will be used. Keep in mind that using the cell network is accurate to basically 500m.
http://developer.android.com/guide/topics/location/obtaining-user-location.html has some really great information and sample code.
After you get done with most of the code in OnCreate()
, add this:
// Acquire a reference to the system Location Manager
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
// Define a listener that responds to location updates
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
// Called when a new location is found by the network location provider.
makeUseOfNewLocation(location);
}
public void onStatusChanged(String provider, int status, Bundle extras) {}
public void onProviderEnabled(String provider) {}
public void onProviderDisabled(String provider) {}
};
// Register the listener with the Location Manager to receive location updates
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
You could also have your activity implement the LocationListener
class and thus implement onLocationChanged()
in your activity.
If you don't want to use the HTML5 Geolocoation API, you can detect a bit from IP.
Or try other other solution
The Google Geocoding API
Geolocator-PHP
Maxmind http://www.maxmind.com/app/php
ipinfodb