/var/www/hkosl.com/littleark/webadmin/models/CurrencyRate.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<?php

use Illuminate\Database\Capsule\Manager as DB;
use 
Carbon\Carbon as Carbon;

class 
CurrencyRate extends BaseModel{
    protected 
$table "currency_rate";

    public function 
currency(){
        return 
$this->belongsTo('Currency''currency_code''iso_code');
    }

    public function 
supplier(){
        return 
$this->belongsTo('Supplier');
    }

    
// /**
    //  * http://fixer.io/
    //  * @return float exchange rate
    //  */
    // public static function getRate($from_currency_code, $to_currency_code){
    //     $result = file_get_contents("http://api.fixer.io/latest?base={$from_currency_code}");
    //     vdump($result['rates']);
    //     if (array_key_exists($to_currency_code, $result['rates'])) {
    //         return $result['rates'][$to_currency_code];
    //     }
    //     // TODO: convert in another way
    //     // $result = file_get_contents("http://api.fixer.io/latest?base={$to_currency_code}");
    //     throw new Exception("Currency not found", 1);

    // }

    /**
     * http://free.currencyconverterapi.com/
     * @return float exchange rate
     */
    
public static function getRate($from_currency_code$to_currency_code){
        
$result file_get_contents("http://free.currencyconverterapi.com/api/v3/convert?q={$from_currency_code}_{$to_currency_code}&compact=y");
        
vdump($result["{$from_currency_code}_{$to_currency_code}"]);
        if (
array_key_exists('val'$result['rates'])) {
            return 
$result['rates']['val'];
        }
        
// TODO: convert in another way
        // $result = file_get_contents("http://api.fixer.io/latest?base={$to_currency_code}");
        
throw new Exception("Currency not found"1);

    }
}