Skip to content

Instantly share code, notes, and snippets.

@pablomoretti
Created October 17, 2011 03:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pablomoretti/1291848 to your computer and use it in GitHub Desktop.
Save pablomoretti/1291848 to your computer and use it in GitHub Desktop.
MercadoLibre - GET ITEM - EXAMPLE PHP
<?php
header('Content-Type: text/html; charset=utf-8');
// we recommend use most robust http client.
// http://www.google.com/search?q=http+client+php
function http_client_get($url) {
//busco el contenido un archivo por si lo habia guardado anteriormente
$reponse = unserialize(@file_get_contents(sys_get_temp_dir() . "/cache_http_client_get-" . str_replace("/", ".",$url)));
//consulto si el contenido que tengo no expiro
if(time() > $reponse['expire']){
// realizo un get a la api
$body = @file_get_contents($url);
// obtengo informacion de los header recibido por el request anterior
foreach ($http_response_header as $headerValue) {
//obtengo el status code de http
if(preg_match('/HTTP.1.1 (...)/',$headerValue, $matches)){
$statusCode = $matches[1];
}
//obtengo el tiempo de cache
if(preg_match('/Cache-Control:.*?max-age=(.*)/',$headerValue, $matches)){
$cache = intval($matches[1]);
}
}
// construyo un array con el contenido del request
$reponse = array("body" => json_decode($body,true), "statusCode" => $statusCode, "expire" => time() + $cache);
// consulto si el contenido puede ser cacheado
if($cache){
// guardo el contenido en
$fp = fopen(sys_get_temp_dir() . "/cache_http_client_get-" . str_replace("/", ".",$url), 'w');
fwrite($fp,serialize($reponse));
fclose($fp);
}
}
return $reponse;
}
// creo el modelo para la vista
$model = array();
// consulto se envio el parametro item_id en el request
if($_REQUEST["item_id"]){
$model["requestItemID"] = $_REQUEST["item_id"];
// realizo la llamada a la API de items
$itemResponse = http_client_get('https://api.mercadolibre.com/items/' . $_REQUEST["item_id"]);
// si el status code es 200
if($itemResponse["statusCode"] == "200"){
// obtengo el body del response devuelvo por la API de items
$model["item"] = $itemResponse["body"];
// realizo la llamada a la API de currencies
$currenciesResponse = http_client_get('https://api.mercadolibre.com/currencies/' . $itemResponse["body"]["currency_id"]);
$model["item"]["currency_symbol"] = $currenciesResponse["body"]["symbol"];
$model["item"]["decimal_places"] = $currenciesResponse["body"]["decimal_places"];
// realizo la llamada a la API de sites
$siteResponse = http_client_get('https://api.mercadolibre.com/sites/' . $itemResponse["body"]["site_id"] );
// realizo la llamada a la API de countries
$countryResponse = http_client_get('https://api.mercadolibre.com/countries/' . $itemResponse["body"]["country_id"] );
$model["country"] = array();
$model["country"]["decimal_separator"] = $countryResponse['body']["decimal_separator"];
$model["country"]["thousands_separator"] = $countryResponse['body']["thousands_separator"];
}
$model["statusCode"] = $itemResponse["statusCode"];
}
?>
<!DOCTYPE html>
<html>
<body>
<h1> Get item </h1>
<form>
Itemd ID <input tabindex="1" type="text" name="item_id" >
<input tabindex="2" type="submit" value="Get">
</form>
<?php if($model["requestItemID"]): ?>
<h2> ItemId <?=$model["requestItemID"]?> </h2>
<h2> Http status : <a target="_blank" href=http://en.wikipedia.org/wiki/HTTP_<?=$model["statusCode"]?> > <?=$model["statusCode"]?> </a> </h2>
<?php endif; ?>
<?php if($model["statusCode"] == "200"): ?>
<dl>
<dt>Title</dt>
<dd><?=$model["item"]["title"] ?></dd>
<dt>Permalink</dt>
<dd><a href="<?=$model["item"]["permalink"]?>" > <?=$model["item"]["permalink"]?> </a> </dd>
<dt>Price</dt>
<dd><?=$model["item"]["currency_symbol"] ?> <?=number_format($model["item"]["price"], $model["item"]["decimal_places"], $model["country"]["decimal_places"], $model["country"]["thousands_separator"])?> </dd>
</dl>
<?php endif; ?>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment