簡體   English   中英

用於貨幣轉換的php腳本

[英]php script for currency conversion

我正在尋找一個進行貨幣轉換的 php 腳本。

我在哪里可以找到一個適用於 codeigniter 的產品?

您可以使用以下代碼輕松地通過谷歌轉換來轉換貨幣。 在此處使用谷歌閱讀有關 PHP 貨幣轉換庫的更多信息http://mydons.com/currency-conversion-library-in-codeigniter/

public function getResult(){           
$result = file_get_contents($this->googleUrl);/* Convert the above result into Array */                           
$result = explode('"', $result);/* Right side text*/           
$convertedAmount = explode(' ', $result[3]);           
$conversion = $convertedAmount[0];           
$conversion = $conversion * $this->amount;          
$conversion = round($conversion, 2);//Get text for converted currency               
$rightText = ucwords(str_replace($convertedAmount[0],"",$result[3]));//Make right hand side string           

$rightText = $conversion.$rightText;/* Left side text*/           
$googleLeft = explode(' ', $result[1]);           
$fromAmount = $googleLeft[0];//Get text for converted from currency              

$fromText = ucwords(str_replace($fromAmount,"",$result[1])); //Make left hand side string           
$leftText = $this->amount." ".$fromText;             
return $leftText." = ".$rightText; 
}

取自http://www.ecb.europa.eu/stats/exchange/eurofxref/html/index.en.html#dev

How to parse the data
This is just an example

<?php
//This is a PHP (4/5) script example on how eurofxref-daily.xml can be parsed 

//Read eurofxref-daily.xml file in memory 
$XMLContent= file("http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml");
//the file is updated daily between 2.15 p.m. and 3.00 p.m. CET

foreach ($XMLContent as $line) {
        if (ereg("currency='([[:alpha:]]+)'",$line,$currencyCode)) {
            if (ereg("rate='([[:graph:]]+)'",$line,$rate)) {
                    //Output the value of 1 EUR for a currency code 
                    echo '1 &euro; = '.$rate[1].' '.$currencyCode[1].'<br />';
                    //--------------------------------------------------
                    // Here you can add your code for inserting
                    // $rate[1] and $currencyCode[1] into your database
                    //--------------------------------------------------
            }
        }
}
?> 

不是最好的腳本,但話說回來,你只是要求 gimme-teh-codez。

您可以非常簡單地計算匯率,例如:

$from = "GBP";
$to = "USD";
$url = 'http://www.webservicex.net/CurrencyConvertor.asmx/ConversionRate?FromCurrency='.$from.'&ToCurrency='.$to;
$rate = simplexml_load_file($url);
echo 'Rate from '.$from.' to '.$to.' is: '.$rate[0];

我從https://currencyfreaks.com/documentation.html#Conversion獲取了用於貨幣轉換的 PHP 代碼。 它提供全球 179 種貨幣的最新貨幣兌換率,包括加密貨幣和貴金屬。

setUrl('https://api.currencyfreaks.com/latest/convert
    ?apikey=YOUR_APIKEY
    &from=USD&to=PKR
    &amount=500');
$request->setMethod(HTTP_Request2::METHOD_GET);
$request->setConfig(array(
  'follow_redirects' => TRUE
));
try {
  $response = $request->send();
  if ($response->getStatus() == 200) {
    echo $response->getBody();
  }
  else {
    echo 'Unexpected HTTP status: ' . $response->getStatus() . ' ' .
    $response->getReasonPhrase();
  }
}
catch(HTTP_Request2_Exception $e) {
  echo 'Error: ' . $e->getMessage();
}

我希望它會對你有所幫助。

    <?php
    defined('BASEPATH') OR exit('No direct script access allowed');

    class CurrencyCon extends CI_Controller {
                                                public function index()
                                                {
                                                $dollarValue=$this->convert_currency('USD', 'INR',1890);//parameter3 give the amount to be converted.
                                                echo 'Actual Rate '.$dollarValue."";
                                                echo 'Round Figure '.$con_dollor = round($dollarValue,2);
                                                }
                                            function convert_currency($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 = file_get_contents($yql_query_url);
                                            $yql_json =  json_decode($yql_session,true);
                                            $currency_output = (float) $currency_input*$yql_json['query']['results']['rate']['Rate'];
                                            return $currency_output;

                                            }

                                                    }
? >

我在這里使用谷歌金融 Api。 使用這個:

 function currencyConverter($from_Currency, $to_Currency, $amount) { 
            $from_Currency = urlencode($from_Currency);
            $to_Currency = urlencode($to_Currency);
            $get = file_get_contents("https://finance.google.com/finance/converter?a=$amount&from=$from_Currency&to=$to_Currency");
            $get = explode("", $get);
            $get = explode("", $get[1]);
            $converted_currency = preg_replace("/[^0-9\.]/", null, $get[0]); 
            return $converted_currency; 
        }

    
 Core php ======== $converted_currency=currencyConverter('USD', 'INR', 1); echo $converted_currency; Codeigniter =========== $converted_currency=$this->currencyConverter('USD', 'INR', 1); echo $converted_currency;

我上周寫了一個 CodeIgniter 貨幣轉換模型。 你可以從這里下載。

它使用歐洲中央銀行的 XML 提要,該提要每周更新一次。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM