API Allociné
Démonstration:
Utilisation:
<?php
// taille_contenu (optionnel) = small/medium/large
$movie = new MovieInfo("nom_du_film/id_allocine", "taille_contenu");
//$array contiendra un tableau comprenant tout ce qu'il vous faut !
$array = $movie->getInfo();
print_r($array); //pour mieux comprendre.
?>
Télécharger l'API
Problèmes connus:
- Allociné ayant tendance à modifier son API, il se peut que les requêtes n'aboutissent plus. Pour y remédier, il faut modifier les variables $searchIdUrl et $getInfoUrl, peut-être du coté du paramètre partner.
- Les serveurs ne disposant pas de PHP 5.2.0+ ne possèdent pas la fonction json_decode(). Pour y remédier, téléchargez ce script, et insérez ce bout de code au début de votre page:
<?php
if ( !function_exists('json_decode') ){
function json_decode($content, $assoc=false){
require_once 'JSON.php';
if ( $assoc ){
$json = new Services_JSON(SERVICES_JSON_LOOSE_TYPE);
} else {
$json = new Services_JSON;
}
return $json->decode($content);
}
}
if ( !function_exists('json_encode') ){
function json_encode($content){
require_once 'JSON.php';
$json = new Services_JSON;
return $json->encode($content);
}
}
?>
Afficher le code:
<?php
/* API Allociné v5.0.1
* Créée par Etienne Gauvin
* Adaptée par Loïs Di Qual
*
* Ce code source est sous licence Creative Commons (CC-by)
* http://creativecommons.org/licenses/by/2.0/fr/
* Auteur original de la source: Etienne GAUVIN
*
* Pour plus d'informations: ldiqual [AT] free [DOT] fr
*/
/* Utilisation:
* $movie = new MovieInfo("nom_du_film/id_allocine", "taille_contenu");
* taille_contenu (optionnel) = small/medium/large
*
* $array = $movie->getInfo();
* $array contiendra un tableau comprenant tout ce qu'il vous faut !
* print_r($array) pour mieux comprendre.
*
* Sources de problème:
* - Allociné change régulièrement le nom de ses variables, attention !
* - Changez la constante FATAL_ERROR à false pour que les erreurs
* n'engendrent pas la fin de votre script
* - En cas de problème récurrent (venant ostensiblement d'Allociné),
* jettez un coup d'oeil du coté de la variable partner dans $searchIdUrl
* et $getInfoUrl, en tentant de la modifier (1,2,3,4,...).
*/
// PHP 5+ requis
if (version_compare(PHP_VERSION, '5.0.0', '<')){
echo "ERREUR: L'API Allociné nécessite PHP5 ou supérieur. Votre version (".PHP_VERSION.") n'est pas compatible.";
exit();
}
define("API_NAME", "API Allociné");
define("API_VERSION", "5.0");
define("FATAL_ERROR", true);
class MovieInfo
{
var $movieId;
var $searchIdUrl = "http://api.allocine.fr/xml/search?q=[movieTitle]&partner=3&json=1&count=10";
var $getInfoUrl = "http://api.allocine.fr/xml/movie?code=[movieId]&partner=3&json=1&profile=[infoSize]";
var $movieInfoArray = array();
// $infoSize = small, medium, large
function MovieInfo($movieTitle, $infoSize = "large")
{
if (!isset($movieTitle)
|| (!is_string($movieTitle) && !is_numeric($movieTitle)))
$this->error("movieTitle");
if ($infoSize != "small" && $infoSize != "medium"
&& $infoSize != "large")
$this->error("infoSize");
if (is_numeric($movieTitle))
$this->movieId = (int) $movieTitle;
else {
$movieTitle = $this->keywordEscape($movieTitle);
$this->searchIdUrl = str_replace("[movieTitle]", $movieTitle, $this->searchIdUrl);
$this->movieId = $this->getMovieId();
}
if (!isset($this->movieId))
$this->error("movieId");
$this->getInfoUrl = str_replace("[movieId]", $this->movieId, $this->getInfoUrl);
$this->getInfoUrl = str_replace("[infoSize]", $infoSize, $this->getInfoUrl);
$this->getMovieInfo();
}
function getMovieId()
{
$jsonResult = file_get_contents($this->searchIdUrl);
if (!$jsonResult)
$this->error("jsonResult");
$jsonArray = json_decode($jsonResult, true);
if (!$jsonArray)
$this->error("jsonArray");
if (empty($jsonArray["feed"]["movie"][0]))
$this->error("jsonArrayEmpty");
return $jsonArray["feed"]["movie"][0]["code"];
}
function getMovieInfo()
{
$jsonResult = @file_get_contents($this->getInfoUrl);
if (!$jsonResult)
$this->error("jsonResult");
$jsonResult = str_replace('"$"', '"value"', $jsonResult);
$jsonArray = @json_decode($jsonResult, true);
if (!$jsonArray)
$this->error("jsonArray");
if (!empty($jsonArray["error"]))
$this->error("jsonArrayError");
$this->movieInfoArray = $jsonArray;
}
function keywordEscape($keyword)
{
$search = utf8_decode('àáâãäçèéêëìíîïñòóôõöùúûüýÿÀÁÂÃÄÇÈÉÊËÌÍÎÏÑÒÓÔÕÖÙÚÛÜÝ ');
$replace = utf8_decode('aaaaaceeeeiiiinooooouuuuyyAAAAACEEEEIIIINOOOOOUUUUY+');
return utf8_encode(strtolower(strtr(utf8_decode($keyword),$search,$replace)));
}
function addData($name, $data)
{
$chem = explode('/', $name);
switch (count($chem)) {
case 1: $this->movieInfoArray[$chem[0]] = $data; break;
case 2: $this->movieInfoArray[$chem[0]][$chem[1]] = $data; break;
case 3: $this->movieInfoArray[$chem[0]][$chem[1]][$chem[2]] = $data; break;
}
}
function getInfo()
{
return $this->movieInfoArray;
}
function error($code)
{
echo API_NAME .": ";
switch ($code){
case "movieTitle":
echo "Erreur lors du passage du titre de film en paramètre. Le titre doit être une chaîne de caractères.";
break;
case "infoSize":
echo "Erreur lors du passage de la quantité d'informations en paramètre. La quantité doit être une chaîne
de caractères de valeur \"small\", \"large\", ou \"medium\".";
break;
case "jsonResult":
echo "Erreur lors de la récupération des informations sur le film demandé. Cela peut provenir des
paramètres de votre serveur, ou d'un changement sur le site Allociné.";
break;
case "jsonArray":
echo "Erreur lors de l'interprétation du JSON provenant d'Allociné. Ré-essayez à nouveau.";
break;
case "jsonArrayError":
echo "Erreur en provenance d'Allociné, après la récupération du JSON. Ré-essayez à nouveau.";
break;
case "jsonArrayEmpty":
echo "Erreur lors de la récupération des informations sur la film demandé. Aucun film portant
ce nom n'a été trouvé.";
break;
case "movieId":
echo "Erreur lors de la récupération des informations sur la film demandé. Impossible de récupérer
l'Id du film demandé.";
break;
default:
echo "Erreur inconnue.";
}
if (FATAL_ERROR == true)
exit();
}
}
?>