SitePoint Sponsor |
|
User Tag List
Results 1 to 8 of 8
Thread: converting php5 to php4
-
Mar 3, 2009, 05:26 #1
- 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); }
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); }
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
-
Mar 3, 2009, 05:39 #2
- 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($fo, 4096);
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($rates, 4);
}
Mistakes are proof that you are trying.....
------------------------------------------------------------------------
PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5
-
Mar 3, 2009, 05:58 #3
- Join Date
- Jun 2004
- Location
- Copenhagen, Denmark
- Posts
- 6,157
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
-
Mar 3, 2009, 06:04 #4
- 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';
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
-
Mar 3, 2009, 06:19 #5
- 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');
HTML Code:var $YAHOO_URL;
HTML Code:function YahooFinanceConverter ()
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); }
-
Mar 3, 2009, 06:22 #6
- 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!
-
Mar 3, 2009, 06:42 #7
- 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.....
-
Mar 3, 2009, 17:35 #8
- 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 £6.80 though";
}
else {
echo number_format($rates, 4);
}
Bookmarks