currency.php 1.2 KB

12345678910111213141516171819202122232425262728293031
  1. <?php
  2. function currency_results($query, $response)
  3. {
  4. $split_query = explode(" ", $query);
  5. $base_currency = strtoupper($split_query[1]);
  6. $currency_to_convert = strtoupper($split_query[3]);
  7. $amount_to_convert = floatval($split_query[0]);
  8. $json_response = json_decode($response, true);
  9. $rates = $json_response["rates"];
  10. if (array_key_exists($base_currency, $rates) && array_key_exists($currency_to_convert, $rates))
  11. {
  12. $base_currency_response = $rates[$base_currency];
  13. $currency_to_convert_response = $rates[$currency_to_convert];
  14. $conversion_result = ($currency_to_convert_response / $base_currency_response) * $amount_to_convert;
  15. $formatted_response = "$amount_to_convert $base_currency = $conversion_result $currency_to_convert";
  16. $source = "https://moneyconvert.net/";
  17. return array(
  18. "special_response" => array(
  19. "response" => htmlspecialchars($formatted_response),
  20. "source" => $source
  21. )
  22. );
  23. }
  24. }
  25. ?>