|
-
May 10th, 2007, 09:37 PM
#1
Thread Starter
New Member
Help convert this code from php5 to php4
My current host refuses to upgrade to php 5 so I am stuck (for the time being) on php 4.
My test server is php 5 but my live server is php 4 so the code I got will not work as it delivers error after error.
Please help. Thank you
The original code is:
rss_feeds.php
PHP Code:
<?php
// rss_feeds.php written by Static & Penagate @ http://staticfx.com
// this file goes in the /bb3portal/block/ directory
class OnlyHellRssFeed {
private $nr_news=10;
private $rss_channel = array();
private $currently_writing = "";
private $main = "";
private $item_counter = 0;
private $template;
private $feedname;
private $url;
function __construct($feedname, $url) {
$this->feedname = $feedname;
$this->url = $url;
}
function startElement($parser, $name, $attrs) {
switch($name) {
case "RSS":
case "RDF:RDF":
case "ITEMS":
$this->currently_writing = "";
break;
case "CHANNEL":
$this->main = "CHANNEL";
break;
case "IMAGE":
$this->main = "IMAGE";
$this->rss_channel["IMAGE"] = array();
break;
case "ITEM":
$this->main = "ITEMS";
break;
default:
$this->currently_writing = $name;
break;
}
}
function endElement($parser, $name) {
$this->currently_writing = "";
if ($name == "ITEM") {
$this->item_counter++;
}
}
function characterData($parser, $data) {
if ($this->currently_writing != "") {
switch($this->main) {
case "ITEMS":
if (isset($this->rss_channel[$this->main][$this->item_counter][$this->currently_writing])) {
$this->rss_channel[$this->main][$this->item_counter][$this->currently_writing] .= $data;
} else {
//print ("rss_channel[$main][$item_counter][$currently_writing] = $data<br>");
$this->rss_channel[$this->main][$this->item_counter][$this->currently_writing] = $data;
}
break;
}
}
}
function get_data(&$template) {
$xml_parser = xml_parser_create();
xml_set_element_handler(
$xml_parser,
array($this, 'startElement'),
array($this, 'endElement')
);
xml_set_character_data_handler(
$xml_parser,
array($this, 'characterData')
);
$data = self::curl_string($this->url);
xml_parse($xml_parser,$data);
xml_parser_free($xml_parser);
// putting in array
$news=array();
if (isset($this->rss_channel["ITEMS"]))
{
if (count($this->rss_channel["ITEMS"]) > 0)
for($i = 0;$i < count($this->rss_channel["ITEMS"]);$i++) $news[]=$this->rss_channel["ITEMS"][$i];
}
$c=0;
foreach($news as $key=>$val)
{
if($c<$this->nr_news)
{
$template->assign_block_vars($this->feedname, array(
'LINK' => $val['LINK'],
'TITLE' => $val['TITLE'],
'DESC' => $val['DESCRIPTION'])
);
}
$c++;
}
}
private static function curl_string ($url,$user_agent='Mozilla 4.0'){
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_USERAGENT, $user_agent);
curl_setopt ($ch, CURLOPT_HEADER, 0);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt ($ch, CURLOPT_TIMEOUT, 120);
$result = curl_exec ($ch);
curl_close($ch);
return $result;
}
}
?>
This gave the error:
Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /home/admin/public_html/bb3portal/block/rss_feeds.php on line 9
When I change:
Code:
private $nr_news=10;
private $rss_channel = array();
private $currently_writing = "";
private $main = "";
private $item_counter = 0;
private $template;
private $feedname;
private $url;
private static function curl_string ($url,$user_agent='Mozilla 4.0'){
to: (removing the "private" code)
Code:
var $nr_news=10;
var $rss_channel = array();
var $currently_writing = "";
var $main = "";
var $item_counter = 0;
var $template;
var $feedname;
var $url;
var static function curl_string ($url,$user_agent='Mozilla 4.0'){
Parse error: syntax error, unexpected T_STATIC, expecting T_VARIABLE in /home/admin/public_html/bb3portal/block/rss_feeds.php on line 106
It was then said to remove "var static" and leave only function curl_string
the new error:
Fatal error: Undefined class name 'self' in /home/admin/public_html/bb3portal/block/rss_feeds.php on line 80
so I searched and found only 1 reference to self:: and changed it to $this->
now I get no errors but the list is not showing up as it should in my block.
Current code:
PHP Code:
<?php
// rss_feeds.php written by Static & Penagate @ http://staticfx.com
// this file goes in the /bb3portal/block/ directory
class OnlyHellRssFeed {
var $nr_news=10;
var $rss_channel = array();
var $currently_writing = "";
var $main = "";
var $item_counter = 0;
var $template;
var $feedname;
var $url;
function OnlyHellRssFeed ($feedname, $url) {
$this->feedname = $feedname;
$this->url = $url;
}
function startElement($parser, $name, $attrs) {
switch($name) {
case "RSS":
case "RDF:RDF":
case "ITEMS":
$this->currently_writing = "";
break;
case "CHANNEL":
$this->main = "CHANNEL";
break;
case "IMAGE":
$this->main = "IMAGE";
$this->rss_channel["IMAGE"] = array();
break;
case "ITEM":
$this->main = "ITEMS";
break;
default:
$this->currently_writing = $name;
break;
}
}
function endElement($parser, $name) {
$this->currently_writing = "";
if ($name == "ITEM") {
$this->item_counter++;
}
}
function characterData($parser, $data) {
if ($this->currently_writing != "") {
switch($this->main) {
case "ITEMS":
if (isset($this->rss_channel[$this->main][$this->item_counter][$this->currently_writing])) {
$this->rss_channel[$this->main][$this->item_counter][$this->currently_writing] .= $data;
} else {
//print ("rss_channel[$main][$item_counter][$currently_writing] = $data<br>");
$this->rss_channel[$this->main][$this->item_counter][$this->currently_writing] = $data;
}
break;
}
}
}
function get_data(&$template) {
$xml_parser = xml_parser_create();
xml_set_element_handler(
$xml_parser,
array($this, 'startElement'),
array($this, 'endElement')
);
xml_set_character_data_handler(
$xml_parser,
array($this, 'characterData')
);
$data = $this->curl_string($this->url);
xml_parse($xml_parser,$data);
xml_parser_free($xml_parser);
// putting in array
$news=array();
if (isset($this->rss_channel["ITEMS"]))
{
if (count($this->rss_channel["ITEMS"]) > 0)
for($i = 0;$i < count($this->rss_channel["ITEMS"]);$i++) $news[]=$this->rss_channel["ITEMS"][$i];
}
$c=0;
foreach($news as $key=>$val)
{
if($c<$this->nr_news)
{
$template->assign_block_vars($this->feedname, array(
'LINK' => $val['LINK'],
'TITLE' => $val['TITLE'],
'DESC' => $val['DESCRIPTION'])
);
}
$c++;
}
}
function curl_string ($url,$user_agent='Mozilla 4.0'){
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_USERAGENT, $user_agent);
curl_setopt ($ch, CURLOPT_HEADER, 0);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt ($ch, CURLOPT_TIMEOUT, 120);
$result = curl_exec ($ch);
curl_close($ch);
return $result;
}
}
?>
there are 2 more files that go with this:
rss_feedlist.php
PHP Code:
<?php
$feeds = array(
'mtgbks' => 'http://www.wizards.com/rss.asp?x=books',
'mtgdnd' => 'http://www.wizards.com/rss.asp?x=dnd',
'mtgd20' => 'http://www.wizards.com/rss.asp?x=d20modern',
'mtgebr' => 'http://www.wizards.com/rss.asp?x=eberron',
'mtgfr' => 'http://www.wizards.com/rss.asp?x=forgottenrealms',
'mtgmtg' => 'http://www.wizards.com/rss.asp?x=magic',
'mtgswrpg' => 'http://www.wizards.com/rss.asp?x=starwars-rpg',
);
foreach ($feeds as $feed_name => $url) {
$feed = new OnlyHellRssFeed($feed_name, $url);
$feed->get_data($template);
}
?>
and rss_feeds.html which is the output page.
if you could please look closley at this code and help with a rewrite for php 4 I would be gratly appreciative.
Thank you
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|