Reputation: 1100
I'm using the yahoo currency converter API to convert the currency for each product listed on the database when it's being displayed on the page.
Function One
function currencyConverter($currency_from,$currency_to,$currency_input){
$yql_base_url = "http://query.yahooapis.com/v1/public/yql";
$yql_query = 'select * from yahoo.finance.xchange where pair in ("'.$currency_from.$currency_to.'")';
$yql_query_url = $yql_base_url . "?q=" . urlencode($yql_query);
$yql_query_url .= "&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys";
$yql_session = curl_init($yql_query_url);
curl_setopt($yql_session, CURLOPT_RETURNTRANSFER,true);
$yqlexec = curl_exec($yql_session);
$yql_json = json_decode($yqlexec,true);
$currency_output = (float) $currency_input*$yql_json['query']['results']['rate']['Rate'];
return $currency_output;
}
Function two
function getProducts($currency)
{
if ($result = $connection->query("SELECT `name`, `price` FROM `products`")) {
$product_count = mysqli_num_rows($result);
if ($product_count >0) {
$output = "";
while ($row = mysqli_fetch_assoc($result)) {
$price = currencyConverter("USD", $currency, $row['item_price']);
$output = $output."<div class='item'><h3 class='product-name'>".$row['item_name']."</h3><h3 class='product-price'>".$currency." ".$price."</h3></div></div>";
}
echo $output;
mysqli_free_result($result);
} else {
echo "nope";
}
} else {
echo "failed";
}
}
The price of a product is saved in USD on the database. The problem is that it takes a very long time for it to convert each and every price. Is there a better way of doing this?
Upvotes: 1
Views: 422
Reputation: 21
try these:
// google API - Load time: 558 ms
function google_money_convert($from, $to, $amount)
{
$url = "https://www.google.com/search?q=".$from.$to;
$request = curl_init();
$timeOut = 0;
curl_setopt($request, CURLOPT_URL, $url);
curl_setopt($request, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($request, CURLOPT_USERAGENT, "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36");
curl_setopt($request, CURLOPT_CONNECTTIMEOUT, $timeOut);
$response = curl_exec($request);
curl_close($request);
preg_match('~<span [^>]* id="knowledge-currency__tgt-amount"[^>]*>(.*?)</span>~si', $response, $finalData);
$finalData=str_replace(',', '.', $finalData);
return (float)$finalData[1]*$amount;
}
// free.currencyconverter API - Load time: 95ms
function money_convert($from, $to, $amount)
{
$url = "http://free.currencyconverterapi.com/api/v5/convert?q=$query&compact=ultra";
$request = curl_init();
$timeOut = 0;
curl_setopt($request, CURLOPT_URL, $url);
curl_setopt($request, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($request, CURLOPT_USERAGENT, "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36");
curl_setopt($request, CURLOPT_CONNECTTIMEOUT, $timeOut);
$response = curl_exec($request);
curl_close($request);
$response = json_decode($response, true);
$responseOld=$response;
// print_r($response);
return $response[$query]*$amount;
}
Upvotes: 1
Reputation: 1100
Got it working a lot faster now.
if ($result = $connection->query("SELECT `name`, `price` FROM `products`")) {
$product_count = mysqli_num_rows($result);
if ($product_count >0) {
$output = "";
// get current currency rate
$exchange_rate = currencyConverter("USD", $currency, "1");
while ($row = mysqli_fetch_assoc($result)) {
$price = $row['item_price'] * $exchange_rate;
$output = $output."<div class='item'><h3 class='product-name'>".$row['item_name']."</h3><h3 class='product-price'>".$currency." ".$price."</h3></div></div>";
}
echo $output;
mysqli_free_result($result);
} else {
echo "nope";
}
} else {
echo "failed";
}
Upvotes: 0