PHP & JSON Parsing - php

I have read a few Q&As on here and additional articles, but cannot get this script to display the relevant values from the JSON return (which are long and lat from this Google Maps call).
<?php
// Address for Google to search
$address = 'London,UK';
// Get the map json data from Google Maps using the $address variable
$googleCall = 'http://maps.googleapis.com/maps/api/geocode/json?address=' . $address .'&sensor=false';
$json = file_get_contents($googleCall);
//header("Content-type: application/json");
//echo $json;
echo $json->results->geometry->location->lat;
echo $json->results->geometry->location->lng;
?>
I am sure I am 99% there, just cannot see where the error is.

You can decode your json as an associative array and the access to all data with scopes
$address = 'London,UK';
$googleCall = 'http://maps.googleapis.com/maps/api/geocode/json?address=' . $address .'&sensor=false';
$json = file_get_contents($googleCall);
$array = json_decode($json, true); //note second parameter on true as we need associative array
echo $array['results'][0]['geometry']['location']['lat'] . '<br>';
echo $array['results'][0]['geometry']['location']['lng'];
This will output
51.5112139
-0.1198244

try this out:
<?php
// Address for Google to search
$address = 'London,UK';
// Get the map json data from Google Maps using the $address variable
$googleCall = 'http://maps.googleapis.com/maps/api/geocode/json?address=' . $address .'&sensor=false';
$json = file_get_contents($googleCall);
$json = json_decode($json);
//header("Content-type: application/json");
//echo $json;
echo $json->results->geometry->location->lat;
echo $json->results->geometry->location->lng;
?>

use json_decode here documentation
http://php.net/manual/en/function.json-decode.php
<?php
// Address for Google to search
$address = 'London,UK';
// Get the map json data from Google Maps using the $address variable
$googleCall = 'http://maps.googleapis.com/maps/api/geocode/json?address=' . $address .'&sensor=false';
$json = file_get_contents($googleCall);
$json = json_decode($json);
echo $json->results[0]->geometry->location->lat;
echo $json->results[0]->geometry->location->lng;
?>

Related

JSON to PHP Array using file_get_contents not working

I'm trying to get video urls from dailymotion.
i got JSON results and its valid tested with online tools but when I use js_decode & print_r it shows warning like
<?php
$content = file_get_contents("http://www.dailymotion.com/embed/video/x49oyt5");
$content = explode(',"qualities":', $content);
$json = explode(',"reporting":', $content[1]);
$json = $json[0];
$mycontent = file_get_contents($json);
$response = json_decode($mycontent, true);
print_r($response);
?>
I want to get video quality and video url from JSON.
You're using file_get_contents on what is actually already a JSON.
updated code, tested ;)
<?php
$content = file_get_contents("http://www.dailymotion.com/embed/video/x49oyt5");
$content = explode(',"qualities":', $content);
$json = explode(',"reporting":', $content[1]);
$json = $json[0];
$videos = json_decode($json,true);
//Cycle through the 1080 videos and print the video urls
foreach($videos[1080] as $video){
printf("Video type:%s URL:%s\n", $video['type'], $video['url']);
}
//Cycle through the 720 videos and print the video urls
foreach($videos[720] as $video){
printf("Video type:%s URL:%s\n", $video['type'], $video['url']);
}
?>
With array_keys($array) you can get all the keys from array, it will return an array with the keys.
<?php
$content = file_get_contents("http://www.dailymotion.com/embed/video/x49oyt5");
$content = explode(',"qualities":', $content);
$json = explode(',"reporting":', $content[1]);
$json = $json[0];
$mycontent = file_get_contents($json);
$response = json_decode($mycontent, true);
$qualities = array_keys($response)
print_r($qualities);
?>

decoding json with php - get url from Facebook profile pic

I need to decode this json value using PHP. I need the value 'url'.
Here is what I have but does not work.
$json = file_get_contents('http://graph.facebook.com/{fb-ID}/picture?type=large&redirect=false');
$decoded = json_decode($json,true);
$url = $decoded[???]; // NEED THIS VAR.
Since $decoded is a PHP array you can access it like any other array:
$url = $decoded['data']['url'];
$decoded=(object)json_decode( $json['data'], true );
echo $decoded->url
This worked:
$json = file_get_contents('http://graph.facebook.com/{fb-ID}/picture?type=large&redirect=false');
$decoded = json_decode($json,true);
$url = $decoded['data']['url'];

PHP google geocoding service returns zero results

I have a problem when I try to convert an address into latitude and longitude via google service. My address is 1456 sok. no: 10/1 kat:8 Alsancak. The problem is when I write this address to url, correct result is returned, however when I use the php code below, I get zero results.
No problem with result :
http://maps.google.com/maps/api/geocode/json?address=1456%20sok.%20no:%2010/1%20kat:8%20Alsancak&sensor=true
Problem with php :
<?php
header('Content-Type: text/html; charset=utf-8');
getGoogleAddressCoordinates("1456 sok. no: 10/1 kat:8 Alsancak");
function getGoogleAddressCoordinates($address)
{
//$address = urlencode($address);
$address = str_replace(" ", "%20", $address);
$request = file_get_contents('http://maps.google.com/maps/api/geocode/json?address=' . $address . '&sensor=true');
$json = json_decode($request, true);
print_r ($json);
}
?>
The address 1456 sok. no: 10/1 kat:8 Alsancak is not valid. If you try it in google maps you get zero results, too.
Try it with: 1456 sok. 10/1 Alsancak and uncomment the line $address = urlencode($address); and clear the line $address = str_replace(" ", "%20", $address);.
The url should look like:
http://maps.google.com/maps/api/geocode/json?address=1456%20sok.%2010%2F1%20Alsancak&sensor=true

Can get JSON from Google Maps call

I have managed to get the longitude and latitude of postcodes from Google Maps but I am unable to then get the distance between the two. The following url gives me some JSON:
https://maps.googleapis.com/maps/api/distancematrix/json?origins=51.6896118,-0.7846495&destinations=51.7651382,-3.7914676&units=imperial&sensor=false
But I can't strip it out using PHP:
<?php
$du = "https://maps.googleapis.com/maps/api/distancematrix/json?origins=51.6896118,-0.7846495&destinations=51.7651382,-3.7914676&units=imperial&sensor=false";
$djd = json_decode(utf8_encode($du),true);
print("-".$djd."-");
?>
Anybody know why this is?
EDIT
The following worked just fine:
<?php
$pc1 = 'SA92NH';
$pc2 = 'HP270SW';
$url1 = "https://maps.googleapis.com/maps/api/geocode/json?address=".$pc1."&sensor=false";
$url2 = "https://maps.googleapis.com/maps/api/geocode/json?address=".$pc2."&sensor=false";
$url1_data = file_get_contents($url1);
$url2_data = file_get_contents($url2);
$json1_data = json_decode(utf8_encode($url1_data),true);
$json2_data = json_decode(utf8_encode($url2_data),true);
$longlat1 = $json1_data['results'][0]['geometry']['location']['lat'].",".$json1_data['results'][0]['geometry']['location']['lng'];
$longlat2 = $json2_data['results'][0]['geometry']['location']['lat'].",".$json2_data['results'][0]['geometry']['location']['lng'];
print($longlat1."<br>\n");
print($longlat2."<br>\n");
?>
Is this what you are looking for?
You need to get the data, you cannot only type the url.
In my example i use get file_get_contents.
<?php
$du = file_get_contents("https://maps.googleapis.com/maps/api/distancematrix/json?origins=51.6896118,-0.7846495&destinations=51.7651382,-3.7914676&units=imperial&sensor=false");
$djd = json_decode(utf8_encode($du),true);
print_r($djd);
?>
http://codepad.viper-7.com/LqxpJW
$url = 'https://maps.googleapis.com/maps/api/distancematrix/json?origins=51.6896118,-0.7846495&destinations=51.7651382,-3.7914676&units=imperial&sensor=false';
$content = file_get_contents($url);
$json = json_decode($content, true);
And after that access $json like an array !
Something like that:
echo $json['rows']['elements']['distance']['text'];

retrieve latitude and longitude from google latitude

I am working on a project and i want to retrieve latitude and longitude using this code but it is not
returning me...please check this code
<?PHP
header('Content-Type: text/plain');
//$userkey='AIzaSyB2hw658V7RSzEYgwSHwYkaLm_505gqLhs';
//$url='https://www.googleapis.com/latitude/v1/currentLocation?key='.$userkey.'&type=json';
$userId = '30811844108.apps.googleusercontent.com';
$url = 'http://www.google.com/latitude/apps/badge/api?user='.$userId.'&type=json';
$content = file_get_contents( $url );
$json = json_decode( $content );
$coord = $json->features[0]->geometry->coordinates;
$lat = $coord[1];
$lon = $coord[0];
echo $lat.' x '.$lon;
//$newurl="http://jecrcgps.betu.tk/data.php?lat=$lat&lon=$lon";
//header("location: $newurl");exit;
?>
are you sure that is the correct $userid?
i was testing for that user $iserid and i just got "Format error for user identifier."
perhaps you should try $userid = 30811844108;

Categories