【问题标题】:PHP currency converterPHP 货币转换器
【发布时间】:2015-10-27 20:30:07
【问题描述】:

我正在使用 yahoo 货币转换器 API 来转换数据库中列出的每个产品在页面上显示时的货币。

功能一

        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 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";
    }   
  }

产品的价格以美元保存在数据库中。问题是它需要很长时间才能转换每个价格。有更好的方法吗?

【问题讨论】:

  • 哪位花费的时间最多?
  • 我刚刚意识到我可以使用 currencyConverter() 函数只获得一次转换率,然后在 while 循环中进行数学运算,而不是在每次迭代中调用 currencyConverter() 函数。
  • @nadz 我敢打赌,你最慢的线路是拨打curl_exec,因为你正在雅虎的服务器上等待。所以是的,如果你可以只调用一次并缓存结果,其他一切都会显着加快。
  • 每天调用一次

标签: php curl


【解决方案1】:

试试这些

// 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;
}

【讨论】:

    【解决方案2】:

    现在它的工作速度快了很多。

        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";
        } 
    

    【讨论】:

    • 更快,每天调用并存储结果
    猜你喜欢
    • 1970-01-01
    • 2014-04-08
    • 2014-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多