SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Enthusiast
    Join Date
    Jan 2007
    Location
    Shropshire, UK
    Posts
    90
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    converting php5 to php4

    I've got a currency converter script that works perfectly in php5, however, when I went to add it to a clients site. That site sits on a server that runs php4. The client doesn't want to upgrade at the moment so I have to find a way to get this to work.

    This is the original script {

    HTML Code:
    class YahooFinanceConverter
    {
    	CONST YAHOO_URL = 'http://finance.yahoo.com/d/quotes.csv?e=.csv&f=sl1d1t1&s=%s%s=X';
    
    	public static function yahoo_convert($a,$from,$to)
    	{
    		$fo = @fopen(sprintf(self::YAHOO_URL,$from,$to), 'r');
    		if ($fo)
    		{
    			$response = fgets($fo, 4096);
    			fclose($fo);
    			$array = explode(',',$response);
    			if(strval($array[1]) > 0) { return strval($a)*strval($array[1]); }
    		}
    		return false;
    	}
    
    }
    
    $rates = YahooFinanceConverter::yahoo_convert(10,'USD','GBP');
    if($rates===false) {
    	echo "Can't process the conversion";
    } else {
    	echo number_format($rates,4);
    }
    and this is what I have changed it to so far:

    HTML Code:
    define('YAHOO_URL','http://finance.yahoo.com/d/quotes.csv?e=.csv&f=sl1d1t1&s=%s%s=X');
    
    class YahooFinanceConverter
    {
    	
    	
    	function YahooFinanceConverter () 
    	{
    	$this -> YAHOO_URL = 'http://finance.yahoo.com/d/quotes.csv?e=.csv&f=sl1d1t1&s=%s%s=X';
    	}
    	
    	function yahoo_convert($a,$from,$to)
    	{
    		$fo = @fopen(sprintf(YAHOO_URL,$from,$to), 'r');
    		if ($fo)
    		{
    			$response = fgets($fo, 4096);
    			fclose($fo);
    			$array = explode(',',$response);
    			if(strval($array[1]) > 0) { return strval($a)*strval($array[1]); }
    		}
    		return false;
    	}
    
    }
    
    $rates = new YahooFinanceConverter::yahoo_convert(10,'USD','GBP');
    if($rates==false) {
    	echo "Can't process the conversion";
    } else {
    	echo number_format($rates,4);
    }
    Unfortunately this gives me the error:
    Parse error: parse error, unexpected T_PAAMAYIM_NEKUDOTAYIM in /home/httpd/vhosts/robertharrop.com/httpdocs/CurrencyConverter.php on line 29

    I know the error is because of :: in
    $rates = new YahooFinanceConverter::yahoo_convert(10,'USD','GBP');

    but, as I know nothing about classes I have no real idea how to fix it. Can anyone help?

    Thanks
    Kirsty

  2. #2
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I guess something like this should work:
    PHP Code:
    class YahooFinanceConverter
    {
        var 
    $_yahoo_url 'http://finance.yahoo.com/d/quotes.csv?e=.csv&f=sl1d1t1&s=%s%s=X';

        function 
    yahoo_convert($a$from$to)
        {
            
    $fo = @fopen(sprintf($this->_yahoo_url$from$to), 'r');
            if (
    $fo)
            {
                
    $response fgets($fo4096);
                
    fclose($fo);
                
    $array explode(',',$response);
                if(
    strval($array[1]) > 0) { return strval($a)*strval($array[1]);}
            }
            return 
    false;
        }

    }
    $obj = new YahooFinanceConverter;
    $rates $obj->yahoo_convert(10,'USD','GBP');
    if(
    $rates === false){
        echo 
    "Can't process the conversion";
    }else{
        echo 
    number_format($rates4);

    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  3. #3
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by KirstyBurgoine View Post
    PHP Code:
    $rates = new YahooFinanceConverter::yahoo_convert(10,'USD','GBP'); 
    Should be:

    PHP Code:
    $rates YahooFinanceConverter::yahoo_convert(10,'USD','GBP'); 

  4. #4
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    And also this line in the constructor?
    PHP Code:
    $this -> YAHOO_URL 'http://finance.yahoo.com/d/quotes.csv?e=.csv&f=sl1d1t1&s=%s%s=X'
    When you fix the problem suggested by kyberfabrikken, I think next error will be in this line since the YAHOO_URL is not defined as the member variable of the class.


    And AFAIK you dont need the constructor to assign the Yahoo URL because you have already defined the constant YAHOO_URL.

    Edit:
    Ahh..
    That actually did not give any error when I checked. I am in PHP 5.2.8. Is the the right behavior kyberfabrikken?? I am not certain on this .
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  5. #5
    SitePoint Enthusiast
    Join Date
    Jan 2007
    Location
    Shropshire, UK
    Posts
    90
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,
    Thanks for your help so far guys.

    I tried the top piece of code but it echoed 'Can't process the conversion'.

    So I went back to my code and changed the $rates line back to:
    HTML Code:
    $rates = YahooFinanceConverter::yahoo_convert(10,'USD','GBP'); 
    I then added:
    HTML Code:
    var $YAHOO_URL;
    above this line
    HTML Code:
    function YahooFinanceConverter ()
    But this still echoes 'Can't process the conversion'

    My entire code looks like:
    HTML Code:
    class YahooFinanceConverter
    {
    	
    	var $YAHOO_URL;
    	function YahooFinanceConverter () 
    	{
    	$this -> YAHOO_URL = 'http://finance.yahoo.com/d/quotes.csv?e=.csv&f=sl1d1t1&s=%s%s=X'; 
    	}
    	
    	function yahoo_convert($a,$from,$to)
    	{
    		$fo = @fopen(sprintf(YAHOO_URL,$from,$to), 'r');
    		if ($fo)
    		{
    			$response = fgets($fo, 4096);
    			fclose($fo);
    			$array = explode(',',$response);
    			if(strval($array[1]) > 0) { return strval($a)*strval($array[1]); }
    		}
    		return false;
    	}
    
    }
    
    $rates = YahooFinanceConverter::yahoo_convert(10,'USD','GBP'); 
    if($rates==false) {
    	echo "Can't process the conversion";
    } else {
    	echo number_format($rates,4);
    }

  6. #6
    SitePoint Guru
    Join Date
    Jan 2005
    Location
    heaven
    Posts
    953
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $fo = @fopen(sprintf(>>>>>>YAHOO_URL<<<<<<,$from,$to), 'r');
    PHP Code:
    $fo = @fopen(sprintf($this->YAHOO_URL,$from,$to), 'r'); 
    Creativity knows no other restraint than the
    confines of a small mind.
    - Me
    Geekly Humor
    Oh baby! Check out the design patterns on that framework!

  7. #7
    SitePoint Enthusiast
    Join Date
    Jan 2007
    Location
    Shropshire, UK
    Posts
    90
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just realised I need to change my php.ini to 'allow_url_fopen'

    Will let you know.....

  8. #8
    SitePoint Wizard cranial-bore's Avatar
    Join Date
    Jan 2002
    Location
    Australia
    Posts
    2,634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You are calling YahooFinanceConverter as a static class, which means the constructor won't run, which means $YAHOO_URL won't be defined.

    Try this:
    PHP Code:
    $yfc = new YahooFinanceConverter();
    $rates $yfc -> yahoo_convert(10'USD''GBP');
    if(
    $rates === false) {
        echo 
    "Can't convert. I reckon it's probably about &pound;6.80 though";
    }
    else {
        echo 
    number_format($rates4);



Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •