PHP scan : essais du WE : Google Latitude,Google Profile, json, Zend_Debug, Zend_Date, ...
À 05:53 dans la rubrique PHP / PEAR
←
/ #1011
/ rss
/ →
Samedi matin j'ai voulu essayer le mini script proposé par Ludovic Toinel sur son blog dans son article PHP : Récupérer la position d'un téléphone utilisant Google Latitude
De fil en aiguille j'en suis arrivé à ce script (incomplet) qui en montre un peu plus.
Dans la première partie, j'affiche cote-à-cote 2 carte similaires.
- A gauche, une carte gmap, positionnée grâce aux informations récupérée sur l'api de latitude.
- A droite la carte latitude équivalente (rien à coder, juste le user id à connaitre).
Ce qui m'ennuyait dans le script c'est qu'il faille connaître le user id latitude de l'utilisateur à afficher.
Alors j'ai cherché dans google profile. Et ... je n'ai pas trouvé. Mais j'ai quand même affiché les infos qu'on y trouve.
Voilà. J'ai bon espoir d'améliorer ce script quand j'aurai du temps à perdre, et de republier une nouvelle version.
J'ai par exemple envie de refactoriser le var_export_intelligent pour qu'il soit passé sur un itérateur, qu'il soit récursif et de plus en plus adapté a divers contenus. quitte à étendre Zend_Debug::Dump puisque c'est un peu son utilité.
Le résultat et le script dans la suite.
[php]
<?php
// Pour obtenir l'id user et pour activer ou désactiver : visit http://www.google.com/latitude/apps/badge
$googleLatitudeUser = '1234567890123456789';
// J'ai Zend_Framework et PEAR dans mon include_path.
require_once 'Zend/Http/Client.php';
require_once 'Zend/Json.php';
require_once 'Zend/Debug.php';
require_once 'Zend/Date.php';
$client = new Zend_Http_Client();
$client->setUri('http://www.google.com/latitude/apps/badge/api?user=-' . $googleLatitudeUser . '&type=json');
$client->request('GET');
/*
* Le flux JSON adhère à la spécification GeoJSON 1.0.
* En plus du point identifié par la latitude et la longitude,
* les propriétés GeoJSON comprennent les id, accuracyInMeters, timeStamp, reverseGeocode,
* photoUrl, photWidth, photoHeight, placardUrl, placardWidth, placardHeight de l’utilisateur.
*
* Pour les flux de partage du nom de la ville, la latitude et la longitude
* représentent le centre-ville et la précision est de 0.
* */
$response = $client->request();
if ($response->isError()) {
die($response->getStatus() . ": " . $response->getMessage());
}
$googleLatitude = Zend_Json::decode($response->getBody());
// le 0 c'est parce qu'on peu mettre user=[user1],[user2], ... dans la requête à l'api
$coordinates = $googleLatitude['features'][0]['geometry']['coordinates'];
$client->setUri('http://socialgraph.apis.google.com/otherme?q=moosh.be@gmail.com');
$client->request('GET');
$response = $client->request();
if ($response->isError()) {
die($response->getStatus() . ": " . $response->getMessage());
}
$googleSocialgraph = Zend_Json::decode($response->getBody());
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<title>Test de google Latitude & google profil</title>
</head>
<body lang="fr">
| Il est à <?php
echo $googleLatitude['features']['0']['properties']['reverseGeocode']?>
| Latitude <?php
echo $coordinates[0];
?>
| Longitude <?php
echo $coordinates[1];
?>
| Précision <?php
echo $googleLatitude['features']['0']['properties']['accuracyInMeters']?> mètres
| TimeStamp <?php
echo $googleLatitude['features']['0']['properties']['timeStamp']?>
= <?php
Une utilisation toute simple de Zend_Date
$date = new Zend_Date($googleLatitude['features']['0']['properties']['timeStamp'], Zend_Date::TIMESTAMP);
print $date;
?>
| <br>Photo du user :
<img
src="<?php
echo $googleLatitude['features']['0']['properties']['photoUrl']?>"
width="<?php
echo $googleLatitude['features']['0']['properties']['photoWidth']?>"
height="<?php
echo $googleLatitude['features']['0']['properties']['photoHeight']?>" />
Phylactère du user :
<img
src="<?php
echo $googleLatitude['features']['0']['properties']['placardUrl']?>"
width="<?php
echo $googleLatitude['features']['0']['properties']['placardWidth']?>"
height="<?php
echo $googleLatitude['features']['0']['properties']['placardHeight']?>" />
<br />
<a
href="http://www.google.com/latitude/apps/badge/api?user=-<?php
echo $googleLatitudeUser;
?>&type=kml"
style="color: #0000FF; text-align: left">Localisation KML</a>
|
<a
href="http://maps.google.com/maps?f=q&hl=fr&ie=UTF8&t=h&z=14&iwloc=A&ll=<?php
echo $coordinates[1]?>,<?php
echo $coordinates[0]?>"
style="color: #0000FF; text-align: left">Agrandir le plan</a>
<h2>Les cartes</h2>
La première c'est avec googlemap, la suivante directement via latitude
<h3>Satélite</h3>
<!-- Google Public Location Badge via GoogleMaps -->
<iframe width="500" height="350" frameborder="0" scrolling="no"
marginheight="0" marginwidth="0"
src="http://maps.google.com/maps?f=q&hl=fr&ie=UTF8&t=k&z=15&ll=<?php
echo $coordinates[1]?>,<?php
echo $coordinates[0]?>&output=embed"></iframe>
<!-- Google Public Location Badge -->
<iframe width="500" height="350" frameborder="0" scrolling="no"
marginheight="0" marginwidth="0"
src="http://www.google.com/latitude/apps/badge/api?user=-<?php
echo $googleLatitudeUser;
?>&type=iframe&maptype=satellite&hl=fr&z=15"></iframe>
<h3>Hybride</h3>
<!-- Google Public Location Badge via GoogleMaps -->
<iframe width="500" height="350" frameborder="0" scrolling="no"
marginheight="0" marginwidth="0"
src="http://maps.google.com/maps?f=q&hl=fr&ie=UTF8&t=h&z=15&ll=<?php
echo $coordinates[1]?>,<?php
echo $coordinates[0]?>&output=embed"></iframe>
<!-- Google Public Location Badge -->
<iframe width="500" height="350" frameborder="0" scrolling="no"
marginheight="0" marginwidth="0"
src="http://www.google.com/latitude/apps/badge/api?user=-<?php
echo $googleLatitudeUser;
?>&type=iframe&maptype=hybrid&hl=fr&z=15"></iframe>
<h3>Terrain</h3>
<!-- Google Public Location Badge via GoogleMaps -->
<iframe width="500" height="350" frameborder="0" scrolling="no"
marginheight="0" marginwidth="0"
src="http://maps.google.com/maps?f=q&hl=fr&ie=UTF8&t=t&z=15&ll=<?php
echo $coordinates[1]?>,<?php
echo $coordinates[0]?>&output=embed"></iframe>
<!-- Google Public Location Badge -->
<iframe width="500" height="350" frameborder="0" scrolling="no"
marginheight="0" marginwidth="0"
src="http://www.google.com/latitude/apps/badge/api?user=-<?php
echo $googleLatitudeUser;
?>&type=iframe&maptype=terrain&hl=fr&z=15"></iframe>
<h3>Routes</h3>
<!-- Google Public Location Badge via GoogleMaps -->
<iframe width="500" height="350" frameborder="0" scrolling="no"
marginheight="0" marginwidth="0"
src="http://maps.google.com/maps?f=q&hl=fr&ie=UTF8&t=r&z=15&ll=<?php
echo $coordinates[1]?>,<?php
echo $coordinates[0]?>&output=embed"></iframe>
<!-- Google Public Location Badge -->
<iframe width="500" height="350" frameborder="0" scrolling="no"
marginheight="0" marginwidth="0"
src="http://www.google.com/latitude/apps/badge/api?user=-<?php
echo $googleLatitudeUser;
?>&type=iframe&maptype=roadmap&hl=fr&z=15"></iframe>
<h2>Google Profile</h2>
<?php
foreach ($googleSocialgraph as $url => $data) {
echo '<h3><a href="' . $url . '">' . $url . '</a></h3><dl>';
foreach ($data['attributes'] as $attrName => $attrValue) {
echo '<dt>
' . $attrName . '
</dt>
<dd>
' . var_export_intelligent($attrValue, 1) . '
' . (($attrName == 'photo') ? '<img src="' . $attrValue . '" />' : '') . '
</dd>';
;
}
echo '</dl>';
unset($data['attributes']);
if (! empty($data))
var_export($data);
}
?>
<h2>Infos brutes, retournées par l'api</h2>
<?php
Zend_Debug::dump($googleLatitude, 'info json', 1);
Zend_Debug::dump($googleSocialgraph, 'info social', 1);
?>
à voir : <a href="http://www.google.com/latitude/">Latitude</a>
&
<a href="http://www.google.com/latitude/apps/badge">Latitude Badge</a>
</body>
</html>
<?php
function var_export_intelligent ($attrValue, $return)
{
if (is_integer($attrValue)) {
$val = $attrValue;
} elseif (is_string($attrValue) && (substr($attrValue, 0, 7) == 'http://') && (in_array(substr($attrValue, - 3, 3), array('jpg' , 'gif' , 'png')))) {
$val = '<img src="' . $attrValue . '" /> (' . $attrValue . ')';
} elseif (is_string($attrValue) && (substr($attrValue, 0, 7) == 'http://')) {
$val = '<a href="' . $attrValue . '">' . $attrValue . '</a>';
} elseif (is_string($attrValue)) {
$val = $attrValue;
} else {
$val = var_export($attrValue, 1);
}
if ($return) {
return $val;
} else {
echo $val;
}
}
?>








Commentaires
Aucun commentaire pour le moment.
Ajouter un commentaire