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;
Related
I'm trying to send this text Montréal-Pierre Elliott Trudeau International Airport, Montreal, QC, Canada through a Ajax post to a php script. I'm getting a 500 (Internal Server Error) when trying to send all the characters in the text.
When I tried 'Montréal-Pierre-Elliott-Trudeau-International' by handcoding, it worked.
Here is my ajax script
$("#nomination-name").focus(function(){
$.post(
'/getlocation/'+$("#newLatitude").val()+"/"+$("#newLongitude").val()+"/"+'ok', // location of your php script
{ place: 'Montréal-Pierre-Elliott-Trudeau-International'}, // any data you want to send to the script
function( data ){ // a function to deal with the returned information
$(".flickr-images").remove();
for(i=0;i<data.images.length;i++) {
$("#images").append(data.images[i]);
}
});
});
Here is the server script:
public function getLocationImage($latitude, $longitude, $cc){
//$place = 'Montreal, Pierre, Elliott, Trudeau, International, Airport';
//GET THE IMAGES BASED ON THE LOCATION FROM FLICKR.
$api_key = 'my_api_key';
$text = Input::get('place');
// $text = 'airport';
$lat = '&lat='.$latitude;
$lon = '&lon='.$longitude;
$perPage = 6;
$url = 'https://api.flickr.com/services/rest/?method=flickr.photos.search';
$url.= '&api_key='.$api_key;
$url.= '&text='.$text;
// $url.= $lat;
// $url.= $lon;
$url.= '&per_page='.$perPage;
$url.= '&format=json';
$url.= '&nojsoncallback=1';
$response = json_decode(file_get_contents($url));
$photo_array = $response->photos->photo;
$count=0;
foreach($photo_array as $single_photo){
$size = 's';
$size2='b';
$photo_url = 'https://farm'.$single_photo->farm.'.staticflickr.com/'.$single_photo->server.'/'.$single_photo->id.'_'.$single_photo->secret.'_'.$size.'.'.'jpg';
$photo_url_big = 'https://farm'.$single_photo->farm.'.staticflickr.com/'.$single_photo->server.'/'.$single_photo->id.'_'.$single_photo->secret.'_'.$size2.'.'.'jpg';
$img[$count] = "<a href='$photo_url_big' target='_blank'><img class='flickr-images' title='' src='$photo_url' /></a>";
$count++;
}
return Response::json(["success"=>"true", "images"=>$img]);
}
I'm trying to remove the space and commas in a string and limit it's count to 20. I tried the following method but is not right.
$('#geocomplete').val().replace(/[, ]+/g, "-").substr(20, $('#geocomplete').val().replace(/[, ]+/g, "-").length)
Is this the right way?
You need to encode the parameter:
$url.= '&text='.urlencode($text);
var data = $('#geocomplete').val().substr(0,20); //get the first 20 characters
data.replace(/\s+/g, '-'); //replace all spaces with '-'
$youtube = simplexml_load_file('http://gdata.youtube.com/feeds/api/videos/wGG543FeHOE?v=2');
$title = $youtube->title;
This gets the title. But how could I get the viewcount and description? tried $youtube->description; and $youtube->views;
I suggest you to use the JSON output instead of the XML one.
You can get it by adding the alt=json parameter to your URL:
http://gdata.youtube.com/feeds/api/videos/wGG543FeHOE?v=2&alt=json
Then you have to load the json and parse it:
<?php
$json_output = file_get_contents("http://gdata.youtube.com/feeds/api/videos/wGG543FeHOE?v=2&alt=json");
$json = json_decode($json_output, true);
//This gives you the video description
$video_description = $json['entry']['media$group']['media$description']['$t'];
//This gives you the video views count
$view_count = $json['entry']['yt$statistics']['viewCount'];
//This gives you the video title
$video_title = $json['entry']['title']['$t'];
?>
Hope this helps.
UPDATE
To see what variables does the JSON output have, add the prettyprint=true parameter to the URL and open it in your browser, it will beautify the JSON output to make it more comprehensible:
http://gdata.youtube.com/feeds/api/videos/wGG543FeHOE?v=2&alt=json&prettyprint=true
Instead of browsing the URL you can just write
echo "<pre>";
print_r($json);
echo "</pre>";
after
$json = json_decode($json_output, true);
and it will print the formatted JSON output
My code:
$vId = "Jer8XjMrUB4";
$gkey = "AIzaSyCO5lIc_Jlrey0aroqf1cHXVF1MUXLNuR0";
$dur = file_get_contents("https://www.googleapis.com/youtube/v3/videos?part=snippet,contentDetails&id=".$vId."&key=".$gkey."");
$data = json_decode($dur, true);
foreach ($data['items'] as $rowdata) {
$vTime = $rowdata['contentDetails']['duration'];
$desc = $rowdata['snippet']['description'];
}
$interval = new DateInterval($vTime);
$vsec = $interval->h * 3600 + $interval->i * 60 + $interval->s;
if($vsec > 3600)
$vsec = gmdate("H:i:s", $vsec);
else
$vsec = gmdate("i:s", $vsec);
echo $vsec."--".$desc;
Result:
02:47--Following the critically acclaimed global smash hit X-Men:
Youtube API V2.0 has been deprecated.You must have to switch to V3 API.Which need API key.
So prefer to use but In this way so can not get video description
http://www.youtube.com/oembed?url=https://www.youtube.com/watch?v=ktt3C7nQbbA&format=json
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;
?>
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'];
I have a small problem with my tweets script. But for some reason, I don't know what the error could be. This is the error it gives me:
$url = "http://www.twitter.com/statuses/user_timeline/{$username}.xml?count={$number}";
$tweets = file_get_contents($url);
$feed = new SimpleXMLElement($tweets);
function time_stamp($date){
if (empty($date)){
return "No date provided";
}
and on the index.php page, it'll show this code:
<?php
$username = "user";//your twitter username
$number = 3;//number of tweets
include ("{$dir}/php/tweets.php");
?>
Do you guys know what it is that I'm doing wrong?
You don't need file_get_contents()
Try:
$url = "http://www.twitter.com/statuses/user_timeline/{$username}.xml?count={$number}";
$feed = simplexml_load_file($url);
Also, twitter made some changes not too long ago so your URL needs to look like this:
$url = "http://api.twitter.com/1/statuses/user_timeline/{$username}.xml?count={$number}";
Check this discussion.
You can use JSON easy and faster than XML
And to get content you can use
Curl => Faster
or
File_get_contents
Url
https://api.twitter.com/1/statuses/user_timeline.json?include_entities=true&include_rts=true&screen_name={screenname}&count={count}
like this
<?php
$url = 'https://api.twitter.com/1/statuses/user_timeline.json?include_entities=true&include_rts=true&screen_name=abdullaheid&count=3'
$x = file_get_contents( $url ) ; // Using file get contents
$object = json_decode( $x ) ;
$array = (array) $object ;
print_r( $array ) ;
?>