Friday, April 19, 2013

Forex Class for PHP 5

4/19/2013


This class can convert money amounts between currencies using iGoogle calculator API.

It can send a HTTP request to the iGoogle calculator API to convert a given money amount between to given currencies.

The class parses the response and returns an array with the original and converted values and currency symbols, the current time and the API URL.





class.forex.php




<?php
/* 
Online PHP Examples with Source Code
website: http://4evertutorials.blogspot.in/
*/


/** 
 *  Foreign Exchange Class | PHP 5 
 *  Build 2013.01.14  
 * 
 *    Copyright (c) 2013 
 *    Jonathan Discipulo  
 *    http://jondiscipulo.com/ 
 *  
 *  This library is free software; you can redistribute it and/or 
 *  modify it under the terms of the GNU Lesser General Public 
 *  License as published by the Free Software Foundation; either 
 *  version 2.1 of the License, or (at your option) any later version. 
 *  
 *  This library is distributed in the hope that it will be useful, 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
 *  Lesser General Public License for more details. 
 *  
 *  You should have received a copy of the GNU Lesser General Public 
 *  License along with this library; if not, write to the Free Software 
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA 
 *  
 *  http://www.gnu.org/copyleft/lesser.html 
 *  
**/ 

// }} 
// {{ Forex 
class Forex { 

    private $amount, $from, $to; 
    protected $api, $query; 
    public $result; 
     
    // }} 
    // {{ Constructor 
    public function __construct() { 
         
        $this->api = 'http://www.google.com/ig/calculator'; 
         
    } 

    // }} 
    // {{ Convert 
    public function convert( $amount, $from='USD', $to='USD' ) { 
     
        $this->amount = $amount; 
        $this->from = $from; 
        $this->to = $to; 
        $this->query = '?hl=en&q=' . $this->amount . $this->from . '%3D%3F' . $this->to; 
        $content = file_get_contents( $this->api . $this->query ); 
        $this->result = $this->parse( $content ); 
         
    } 

    // }} 
    // {{ Get Result 
    public function get( $string='converted' ) { 
         
        $array = $this->result; 
        return $array[$string]; 
         
    } 

    // }} 
    // {{ Get Result Array 
    public function getResultArray() { 
         
        return $this->result; 
         
    } 
     
    // }} 
    // {{ Parse 
    private function parse( $string ) { 
        if (!empty($string)) { 
            $data = explode( '"', $string ); 
            $object = explode( ' ', $data[3]); 
            $result = array( 
                'from' => $this->from, 
                'to' => $this->to, 
                'amount' => $this->amount, 
                'converted' => $object[0], 
                'stamp' => gmdate('Y/m/d H:i:s') . ' UTC', 
                'api' => $this->api 
            ); 
        } else { 
            $result = null; 
        } 
        return $result; 
    } 

    // }} 
    // {{ Magic Method: Sleep 
    public function __sleep() { 
        // reserved for codes to run when in sleep 
    } 
     
    // }} 
    // {{ Magic Method: Wake Up 
    public function __wakeup() { 
        // reserved for codes to run when after sleep 
    } 

    // }} 
    // {{ Destructor 
    public function __destruct() { 
         
        // reserved for codes to run when this object is destructed 
        if (isset($this->amount)) unset($this->amount); 
        if (isset($this->from)) unset($this->from); 
        if (isset($this->to)) unset($this->to); 
        if (isset($this->result)) unset($this->result); 
         
    } 

} 

?>








sample.php



<?php
/* 
Online PHP Examples with Source Code
website: http://4evertutorials.blogspot.in/
*/

require_once('class.forex.php'); 

$forex = new Forex(); 
$forex->convert( '1', 'USD', 'PHP' ); 

echo $forex->get('from') . ' ' . $forex->get('amount') . ' is equal to ' . $forex->get('to') . ' ' . $forex->get('converted') . ' as of ' . $forex->get('stamp') . '
'; echo ""; print_r( $forex->getResultArray() ); echo ""; ?>

helpful? Share this

The Editorial Team of 4everTutorials consists of a group of PHP Professionals.

0 comments:

 

© 2014 4everTutorials. All rights resevered.

Back To Top