Skip to content

Instantly share code, notes, and snippets.

@pablomoretti
Created October 17, 2011 03:03
Show Gist options
  • Select an option

  • Save pablomoretti/1291848 to your computer and use it in GitHub Desktop.

Select an option

Save pablomoretti/1291848 to your computer and use it in GitHub Desktop.

Revisions

  1. pablomoretti revised this gist Oct 31, 2011. 1 changed file with 18 additions and 4 deletions.
    22 changes: 18 additions & 4 deletions get-item.php
    Original file line number Diff line number Diff line change
    @@ -5,24 +5,34 @@
    // 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);
    @@ -34,30 +44,34 @@ function http_client_get($url) {

    }

    // 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"];

    //get currency
    // 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"];

    //get site
    // realizo la llamada a la API de sites
    $siteResponse = http_client_get('https://api.mercadolibre.com/sites/' . $itemResponse["body"]["site_id"] );

    //get country

    // realizo la llamada a la API de countries
    $countryResponse = http_client_get('https://api.mercadolibre.com/countries/' . $itemResponse["body"]["country_id"] );

    $model["country"] = array();
  2. pablomoretti revised this gist Oct 18, 2011. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions get-item.php
    Original file line number Diff line number Diff line change
    @@ -1,8 +1,6 @@
    <?php
    header('Content-Type: text/html; charset=utf-8');

    $model = array();

    // we recommend use most robust http client.
    // http://www.google.com/search?q=http+client+php
    function http_client_get($url) {
    @@ -36,6 +34,8 @@ function http_client_get($url) {

    }

    $model = array();

    if($_REQUEST["item_id"]){

    $model["requestItemID"] = $_REQUEST["item_id"];
    @@ -46,7 +46,7 @@ function http_client_get($url) {

    $model["item"] = $itemResponse["body"];

    //get currency symbol
    //get currency
    $currenciesResponse = http_client_get('https://api.mercadolibre.com/currencies/' . $itemResponse["body"]["currency_id"]);

    $model["item"]["currency_symbol"] = $currenciesResponse["body"]["symbol"];
  3. pablomoretti created this gist Oct 17, 2011.
    104 changes: 104 additions & 0 deletions get-item.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,104 @@
    <?php
    header('Content-Type: text/html; charset=utf-8');

    $model = array();

    // we recommend use most robust http client.
    // http://www.google.com/search?q=http+client+php
    function http_client_get($url) {

    $reponse = unserialize(@file_get_contents(sys_get_temp_dir() . "/cache_http_client_get-" . str_replace("/", ".",$url)));

    if(time() > $reponse['expire']){

    $body = @file_get_contents($url);

    foreach ($http_response_header as $headerValue) {
    if(preg_match('/HTTP.1.1 (...)/',$headerValue, $matches)){
    $statusCode = $matches[1];
    }
    if(preg_match('/Cache-Control:.*?max-age=(.*)/',$headerValue, $matches)){
    $cache = intval($matches[1]);
    }
    }

    $reponse = array("body" => json_decode($body,true), "statusCode" => $statusCode, "expire" => time() + $cache);

    if($cache){
    $fp = fopen(sys_get_temp_dir() . "/cache_http_client_get-" . str_replace("/", ".",$url), 'w');
    fwrite($fp,serialize($reponse));
    fclose($fp);
    }

    }

    return $reponse;

    }

    if($_REQUEST["item_id"]){

    $model["requestItemID"] = $_REQUEST["item_id"];

    $itemResponse = http_client_get('https://api.mercadolibre.com/items/' . $_REQUEST["item_id"]);

    if($itemResponse["statusCode"] == "200"){

    $model["item"] = $itemResponse["body"];

    //get currency symbol
    $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"];

    //get site
    $siteResponse = http_client_get('https://api.mercadolibre.com/sites/' . $itemResponse["body"]["site_id"] );

    //get country

    $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>