Forum Moderators: coopster

Message Too Old, No Replies

Change fopen to CURL?

My site doesn't support fopen, can you convert this script to Curl?

         

Garibaldi3489

2:59 am on Aug 12, 2006 (gmt 0)

10+ Year Member



I have installed a script for my phpBB forum that will automatically take a rss feed and insert the news items as new topics. However, it uses fopen, which my host doesn't support. My host does, however, support CURL. Could you convert this fopen script to CURL for me? Here is part of the file:

// close the data source
if ($this->_fp)
{
fclose($this->_fp);
}

// destroy our xml parser
xml_parser_free($this->_xml_parser);
}

// parse the xml
function parse($RSS_URL)
{
$prev_error_reporting = error_reporting();
error_reporting(E_ERROR);

$result = false;
$data = '';

// retrieve the data from the given url
if ($this->_fp = @fopen($RSS_URL,'r'))
{
// read the file into our data in 4k chunks
while ($data = fread($this->_fp, 4096))
{
// parse our xml data until we find the end of our file
xml_parse($this->_xml_parser, $data, feof($this->_fp))
or die(sprintf('XML error: %s at line %d',
xml_error_string(xml_get_error_code($this->_xml_parser)),
xml_get_current_line_number($this->_xml_parser)));

$result = true;
}
}
error_reporting($prev_error_reporting);

return $result;
}

Thanks

whoisgregg

11:29 pm on Aug 16, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



WebmasterWorld isn't really a "volunteer code writing squad." If you are doing the conversion yourself and run into trouble, you should definitely feel welcome to post the bit of relevant code that is throwing an error and folks here will be happy to take a look at it. :)

Garibaldi3489

12:31 am on Aug 17, 2006 (gmt 0)

10+ Year Member



I'm not asking for you to write a whole script for me, I just want to know how to correctly replace the fopen command in that snippet of code with the correct curl command. That's it. The reason I included more than just that code was to put the variables in context

jatar_k

5:36 pm on Aug 17, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



using curl would be a little different

you would read the whole page into a variable and then instead of using fread you could possibly use chunk_split to make your 4k pieces, not sure what would be the best method though.

Garibaldi3489

6:43 pm on Aug 17, 2006 (gmt 0)

10+ Year Member



Thanks for the reply. I can post the whole file here if that would help?

jatar_k

6:54 pm on Aug 17, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



not really, plus we don't actually allow code dumps

as I mentioned above that would be the loguc to replicate what you are doing

give it a try, there are many examples of using curl in the manual
[php.net...]

just remember you will be pulling the page as a whole and then working with that.

If you really wanted to keep your code as is you could use curl to grab the whole page and then save it to your server and use your existing code on that.

Garibaldi3489

8:40 pm on Aug 17, 2006 (gmt 0)

10+ Year Member



How could I use Curl to grab the page and put it on my local server. I had tried a php include, however this script didn't like the file because of the <?> tags, it refused to use the xml file. However, if I could just get it to automatically copy over the xml file with curl then that would work! How can that be done?