Forum Moderators: coopster

Message Too Old, No Replies

Problem connecting to feed source

         

neophyte

9:58 am on Nov 12, 2009 (gmt 0)

10+ Year Member



Hello All -

Boy, I've been pulling my hair on this one for days. Maybe someone here can help.

I've written a php script that will pull an rss feed once a day which will then - from the results of the feed - populate any number of db tables.

This works PERFECTLY on my local dev - not a problem!

But, when I put it on a live testing domain (previous to client deployment) I always get this error: "cannot open [domain.com...]

Even though I can see this error, my hosting company says that nothing is reported in their error logs and that they're not blocking/fire-walling anything which may be blocking an outgoing http request.

Then I went to the feed provider and they said they are not blocking the feed by IP or domain and it should pull without a problem - just like from my local dev.

The host suggested that I implement a more specific type of error handling for outgoing requests - which they're probably right - but I've never done that before and am not really sure how to go about that.

The issue that really nags at me is, if it works locally, why wouldn't it work in a live environment.

Has anyone else encountered this kind of issue before?

Would gratefully accept any guidance.

Neophyte

bkeep

11:06 am on Nov 12, 2009 (gmt 0)

10+ Year Member



How are you accessing the remote feed, curl, fsockopen, fopen? Also what version PHP on the testing box and the local dev box?

neophyte

12:43 pm on Nov 12, 2009 (gmt 0)

10+ Year Member



bkeep -

Thanks for your reply. Regarding how am I accessing the feed, I'm using fopen in a php parser script I found on the web and then modified.

The meat of this function is shown here:

++++++++++++++++++++++

if(!($xmlparser = xml_parser_create())) die('Cannot create parser');

xml_set_element_handler($xmlparser, 'start_tag_weather', 'end_tag_weather');

xml_set_character_data_handler($xmlparser, 'tag_contents');

if(!($fp = fopen($fileName, 'r'))) die('cannot open ' . $fileName);

while ($data = fread($fp, 4096)){
$data=eregi_replace('>'.'[[:space:]]+'.'<','><',$data);
if(!xml_parse($xmlparser, $data, feof($fp)))
{
$failure = xml_error_string(xml_get_error_code($xmlparser));
$failure .= xml_get_current_line_number($xmlparser);
die($failure);
}
}

xml_parser_free($xmlparser);

+++++++++++++++

The error is being generated by:
if(!($fp = fopen($fileName, 'r'))) die('cannot open ' . $fileName);

The PHP version on my local dev box (at home) is 5.2.0.

The version used on my live dev hosting account is 5.2.1.

Hope the above offers some clues as to why I can flawlessly access the feed from my local box at home, but not on a live domain.

Neophyte

TheMadScientist

1:44 pm on Nov 12, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



If allow_url_fopen is turned off by your host, that could be the issue...

If PHP has decided that filename specifies a local file, then it will try to open a stream on that file. The file must be accessible to PHP, so you need to ensure that the file access permissions allow this access. If you have enabled safe mode, or open_basedir further restrictions may apply.

If PHP has decided that filename specifies a registered protocol, and that protocol is registered as a network URL, PHP will check to make sure that allow_url_fopen is enabled. If it is switched off, PHP will emit a warning and the fopen call will fail.

[php.net...]

bkeep

9:53 pm on Nov 12, 2009 (gmt 0)

10+ Year Member



My guess is the same. I use curl and if it is available you can get the file and write it to a local file. I use this to grab a remote csv file but you could adjust it for any file.

//full remote and local paths
$full_remote_path = "$remote_server$csv_file_name";
$full_local_path = "$local_path$csv_file_name";

//initialize curl
$curl = curl_init();

//Open file and truncate or create new if not exists
$file = fopen($full_local_path, 'w');

//set curl options connect and download file using open file handle
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_URL, $full_remote_path);
curl_setopt($curl, CURLOPT_FILE, $file);

//execute then close
curl_exec($curl);
curl_close($curl);

//close file so we can do other stuff
fclose($file);

Also eregi_replace will be outdated and if you can use preg_replace instead I think that would be a good idea as well.