Zfeeder / fopen/cURL issues

apps

#1

Boy do I need help…

zfeeder is an RSS parser/aggregator that I use on my site (argn.com) to display the last 5 entries at three different bogs/forums I check regularly. It’s super easy to set up and maintain, provided the fopen command is available to get URLs from outside sources. Because of security issues, it is not here at DH. I’m fine with that.

However, after reading multiple sources of documentation about cURL and how to work around the fact that I cannot use fopen for URLs, I’m at a loss as to why my feeds still aren’t streaming in. One of the only places where fopen attempts to read from an external URL (and not a filename on my own site) is in a function PHP page, and I’ve changed this code:

[code]function fetchFeed($from, $to)
{
@ini_set(“user_agent”,“zfeeder/”.ZF_VER." (http://zvonnews.sf.net)");
@$fp = fopen($from, “r”);
$data = ‘’;
while (true) {
@$datas = fread($fp, 4096);
if (strlen($datas) == 0) {
break;
}
$data .= $datas;
}
@fclose($fp);

if ($data != ‘’) {
$fp = fopen($to, “w”);
fwrite($fp, $data);
fclose($fp);
@chmod($to, 0766);
return($from . " - cached
\n");
} else {
return($from . " - NOT cached; check connection
\n");
}
}
[/code]with this updated version (using the cURL commands):

[code]function fetchFeed($from, $to)
{
@ini_set(“user_agent”,“zfeeder/”.ZF_VER." (http://zvonnews.sf.net)");
$curl_handle = curl_init();
curl_setopt ($curl_handle, CURLOPT_URL, $from);
curl_setopt ($curl_handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($curl_handle, CURLOPT_CONNECTTIMEOUT, 1);
$fp = curl_exec($curl_handle);
curl_close($curl_handle);
$data = ‘’;
while (true) {
@$datas = fread($fp, 4096);
if (strlen($datas) == 0){
break;
}
$data .= $datas;
}

if ($data != ‘’) {
$fp = fopen($to, “w”);
fwrite($fp, $data);
fclose($fp);
@chmod($to, 0766);
return($from . " - cached
\n");
} else {
return($from . " - NOT cached; check connection
\n");
}
}
[/code]Now, from where I stand, that shoudl work. However, like I said, I am still experiencing connection problems. Can anyone help me with this problem?

jamesi


#2

Hi jamesi,

While fopen return a filepointer that you have to fread from to get the data, curl_exec returns the data in one go. Try to change your script from:

$fp = curl_exec($curl_handle); curl_close($curl_handle); $data = ''; while (true) { @$datas = fread($fp, 4096); if (strlen($datas) == 0) { break; } $data .= $datas; } to:

$data = curl_exec($curl_handle); curl_close($curl_handle); /* $data = ''; while (true) { @$datas = fread($fp, 4096); if (strlen($datas) == 0) { break; } $data .= $datas; } */
Best of luck and Happy New Year


[color=#000000]DreamHost Promo Code[/color]: [color=#CC0000]Save $50[/color] with the promo code DREAMTEAM


#3

Yoou are a life saver! This worked perfectly! I thank you so very much!!!

The new code (which I will send to the publishers of the software):

[code]// fetchFeed code modified for curl by Jonathan Waite for argn.com
// fetchFeed code finalized by doeleman @ Dreamhost forums – what a life saver!

function fetchFeed($from, $to)
{
@ini_set(“user_agent”,“zfeeder/”.ZF_VER." (http://zvonnews.sf.net)");
$curl_handle = curl_init();
curl_setopt ($curl_handle, CURLOPT_URL, $from);
curl_setopt ($curl_handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($curl_handle, CURLOPT_CONNECTTIMEOUT, 1);
$data = curl_exec($curl_handle);
curl_close($curl_handle);
/*
$data = ‘’;
while (true) {
@$datas = fread($fp, 4096);
if (strlen($datas) == 0){
break;
}
$data .= $datas;
}

*/
[/code]