LibreY

- privacy respecting meta search engine
git clone git://git.acid.vegas/LibreY.git
Log | Files | Refs | Archive | README | LICENSE

currency.php (1412B)

      1 <?php
      2     class CurrencyRequest extends EngineRequest {
      3         public function get_request_url() {
      4             return "https://cdn.moneyconvert.net/api/latest.json";
      5         }
      6         
      7         public function parse_results($response) {
      8             $split_query = explode(" ", $this->query);
      9 
     10             $base_currency = strtoupper($split_query[1]);
     11             $currency_to_convert = strtoupper($split_query[3]);
     12             $amount_to_convert = floatval($split_query[0]);   
     13             
     14             $json_response = json_decode($response, true);
     15                     
     16             $rates =  $json_response["rates"];
     17 
     18             if (!array_key_exists($base_currency, $rates) || !array_key_exists($currency_to_convert, $rates))
     19                 return array();
     20             $base_currency_response = $rates[$base_currency];
     21             $currency_to_convert_response = $rates[$currency_to_convert];
     22 
     23             $conversion_result = ($currency_to_convert_response / $base_currency_response) * $amount_to_convert;
     24 
     25             $formatted_response = "$amount_to_convert $base_currency = $conversion_result $currency_to_convert";
     26             $source = "https://moneyconvert.net/";
     27             return array(
     28                 "special_response" => array(
     29                     "response" => htmlspecialchars($formatted_response),
     30                     "source" => $source
     31                 )
     32             );
     33         }
     34     }
     35 ?>