php display information from string - php

I have this code that gets the IP address and Latitude and Longitude of the user. However I am wanting ti display it on a map if it finds the lat and long.
$theirip = $_SERVER['REMOTE_ADDR'];
$fileinfo = file_get_contents("http://api.easyjquery.com/ips/?ip=".$theirip."&full=true");
This outputs a string like this.
{"IP":"000.000.00.000","continentCode":"Unknown","continentName":"Unknown","countryCode2":"Unknown","COUNTRY":"Unknown","countryCode3":"Unknown","countryName":"Unknown","regionName":"Unknown","cityName":"Unknown","cityLatitude":0,"cityLongitude":0,"countryLatitude":0,"countryLongitude":0,"localTimeZone":"Unknown","localTime":"0"}
I am wanting to retrieve just the cityLatitude and cityLongitude from this. I have researched some and tried some different code.
$lat = $fileinfo -> {"cityLatitude"};
but i'm not quite sure how to go about this. Any help would be appreciated!

What you have there is a JSON string, you can use json_decode to pasre it
$obj = json_decode($fileinfo);
$lat = $obj -> {"cityLatitude"};

its a JSONObject... just use the json_decode() to get data from it

no need to decode the JSON, it should work fine.
trying from here its working just fine...
try using your IP instead of $theirip = $_SERVER['REMOTE_ADDR'];

You're using PHP right? It should be
$info = json_decode($fileinfo);
$citylatitude = $info->cityLatitude;

Related

Displaying information from JSON request

For the sake of having you guys being able to read what I'm doing, I will post this with the api key and then edit it after.
How do I display the information from this url?
https://www.googleapis.com/youtube/v3/videos?part=snippet&id=UItqDZuHOsM&key=AIzaSyAQ26GN-removedapikey
I am using this code, but I don't understand the correct way to grab the json stuff. I've tried tons of different ways.
$vidkey = $vid_row['youtube_id'];
$apikey = "removed";
$json_output = file_get_contents("https://www.googleapis.com/youtube/v3/videos?part=snippet&id=$vidkey&key=$apikey");
$json = json_decode($json_output, true);
//video title
$you_title = $json['snippet']['title'];
I guess I don't understand the hierarchy of it all.
Use Below code to get Title :
$vidkey = $vid_row['youtube_id'];
$apikey = "**********";
$json_output = file_get_contents("https://www.googleapis.com/youtube/v3/videos?part=snippet&id=$vidkey&key=$apikey");
$json = json_decode($json_output, true);
echo $json['items'][0]['snippet']['title'];
Try do to a print_r($json) to understand what you're exactly doing. But just navigate in it as an object. In the current case just do:
$json['items'][0]['snippet']['title'];
But if you want to do it with a more easy way to do it (object way), just remove the true from your json_decode function
$json->items[0]->snippet->title;

Bidimensional array in PHP cant acces nominatim geocoding data

I'm accessing lat and lon via nominatim json and converting it to php array,all the stuff works until i try to acces lon into one array. If i acces to the array int its ok but if i try to get what's inside it dont.
$url = "http://nominatim.openstreetmap.org/search/".$_GET['places']."?format=json&email=gotia.ts#gmai.com";
$placeJSON = file_get_contents($url);
$arrPlace = json_decode($placeJSON);
//$lati = $arrPlace[0]; works
//That does not
$lati = $arrPlace[0]['lat'];
You Try this format
echo $lati = $arrPlace['0']->lat;
Working fine

Converting file_get_contents to an array for IP details

I have this simple code on my site:
$info = file_get_contents('http://freegeoip.net/json/'.$_SERVER['REMOTE_ADDR']);
which displays like this:
{"ip":"123.123.12.1","country_code":"GB","country_name":"United Kingdom","region_code":"ENG","region_name":"England","city":"London","zip_code":"LN1","time_zone":"Europe/London","latitude":30.302,"longitude":-1.123,"metro_code":0}
I am trying to turn this in to an array so that I can import this data to the database using something similar to:
$ip_address = $info['ip'];
$city = $info['city'];
And so on... I have attempted to use $info[1], $info[2] and it just displays { " each character of text. Is there a simple way to do this?
That is a JSON format and you can use the json_decode() function to convert it to either object or array.
So you have:
$info = file_get_contents('http://freegeoip.net/json/'.$_SERVER['REMOTE_ADDR']);
$info = json_decode($info, true);
Then you can use, as you said, the $info['ip'] and $info['city']
Use json_decode
$info_obj = json_decode($info);
Now $info_obj has the info you want, and you can access it this way:
$ip_address = $info_obj->ip;
$city = $info_obj->city;

Steam url api inserting variables

I have been attempting to use steam api to display a persons name. When I insert there steamid in directly it works however, when I try to use a variable it doesn't work. The thing that confuses me is that I have this exact same code for my queue that works however this does not work. I have looked around and I believe I am doing this right but for some reason it is not working. Any help here would be great. Thanks.
<?
$name = 76xxxxxxxxxxx;
$response = "http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/? key=xxx&steamids={$name}&format=json";
$middle = (file_get_contents($response));
$json = json_decode($middle, true);
$b = $json['response']['players'][0]['personaname'];
print_r($b);
?>
Your $name variable needs to be wrapped in quotes.
$name = "76xxxxxxxxxxx";
This will properly create your response variable

How find Distance between two addresses through php

i am looking for some help to find the distance between two address through php
as i can not use the google maps api. so get a way from this post
Distance between two addresses
but i need to know how can send the request using URL pattern
and grab the repose to save them in database.
http://maps.google.com/maps/api/directions/xml?origin=550+Madison+Avenue,+New+York,+NY,+United+States&destination=881+7th+Avenue,+New+York,+NY,+United+States&sensor=false
thanks for any suggestion.
///////////////
After these answers i am there
$customer_address_url = urlencode($customer_address);
$merchant_address_url = urlencode($merchant_address);
$map_url = "http://maps.google.com/maps/api/directions/xml?origin=".$merchant_address_url."&destination=".$customer_address_url."&sensor=false";
$response_xml_data = file_get_contents($map_url);
$data = simplexml_load_string($response_xml_data);
XML response is visible when i put this url : http://maps.google.com/maps/api/directions/xml?origin=Quentin+Road+Brooklyn%2C+New+York%2C+11234+United+States&destination=550+Madison+Avenue+New+York%2C+New+York%2C+10001+United+States&sensor=false
but can not printing through
echo "<pre>"; print_r($data); exit;
You can try this..
$url = "http://maps.google.com/maps/api/directions/xml?origin=550+Madison+Avenue,+New+York,+NY,+United+States&destination=881+7th+Avenue,+New+York,+NY,+United+States&sensor=false";
$data = file_get_contents($url);
Now $data contains resulting xml data. You can parse this xml data using..
http://php.net/manual/simplexml.examples.php
use the haversine formula
Also check https://developers.google.com/maps/documentation/distancematrix/

Categories