Link to home
Start Free TrialLog in
Avatar of FORege
FORege

asked on

problem with fopen (PHP)

I cannot get the fopen command to work for external URLs on a unix server.  I am able to open files in the local directory, but fopen("http://yadayada", "r") does not work.  I get:

Warning: fopen("http://p.moreover.com/cgi-local/page?c=Africa%20news&o=xml", "r") - No such file or directory in /usr/...path to my content.../public_html/news.php on line 70
Could not open your file for parsing!

Its as though its looking for the external reference in the local directory.  The server is Unix.  I can get the exact same file (news.php) to parse and display the xml on my local machine (Windows IIS running php4.3).

Please help.  Here's the code:

<?
$xml_file = "http://p.moreover.com/cgi-local/page?c=Africa%20news&o=xml";//change this to whatever news feed you want to use
$open_tags = array(
"ARTICLE" => "<ARTICLE>",
"HEADLINE_TEXT" => "<HEADLINE_TEXT>",
"URL" => "<URL>",
"HARVEST_TIME" => "<HARVEST_TIME>",
"SOURCE" => "<SOURCE>"); $close_tags = array(
"ARTICLE" => "</ARTICLE>",
"HEADLINE_TEXT" => "</HEADLINE_TEXT>",
"HARVEST_TIME" => "<HARVEST_TIME>",
"URL" => "</URL>",
"SOURCE" => "</SOURCE>"); $count=0;
function startElement($parser, $name, $attrs=""){
global $open_tags, $temp, $current_tag;
$current_tag = $name;
if ($format = $open_tags[$name]){
switch($name){
default:
break;
}
}
} function endElement($parser, $name, $attrs=""){
global $close_tags, $temp, $current_tag, $count, $numstors;
if ($format = $close_tags[$name]){
switch($name){
case "ARTICLE":
return_page($temp);
$temp = "";
break;
default:
break;
}
}
} function characterData($parser, $data){
global $current_tag, $temp, $catID;
switch($current_tag){
case "HEADLINE_TEXT":
$temp["head"] = $data;
$current_tag = "";
break;
case "URL":
$temp["url"] = $data;
$current_tag = "";
break;
case "SOURCE":
$temp["source"] = $data;
$current_tag = "";
break;
case "HARVEST_TIME":
$temp["time"] = $data;
$current_tag = "";
break;
default:
break;
}
} function return_page(){
global $temp, $count;
$temp["time"] = preg_replace ("/AM/", " a.m.", $temp["time"]);
$temp["time"] = preg_replace ("/PM/", " p.m.", $temp["time"]);
echo "<a href=\"".$temp["url"]."\" target=\"_blank\">".$temp["head"]."</a>\n<br>\n";
echo "[".$temp["source"]."] - ".$temp["time"]."\n<br>\n<br>\n\n";
} $xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startElement","endElement");
xml_set_character_data_handler($xml_parser, "characterData"); if (!($fp = fopen($xml_file, "r"))) {
die("Could not open your file for parsing!\n");
}
while ($data = fread($fp, 4096)) {
if (!($data = utf8_encode($data))) {
echo "ERROR"."\n";
}
if (!xml_parse($xml_parser, $data, feof($fp))) {
die(sprintf( "XML error: %s at line %d\n\n",
xml_error_string(xml_get_error_code($xml_parser)),
xml_get_current_line_number($xml_parser)));
}
} xml_parser_free($xml_parser);
?>

New to PHP so thanks in advance, Fred.  
Avatar of axis_img
axis_img

Can you do me a favor... Copy this simple script, open the page in a browser, and paste what it says here. I would like to first make sure you have "allow_url_fopen" enabled in the php configuration. If not, you will need to enable it before you can use fopen() (or any other file-stream functions that use the fopen() wrappers) to open a URL.

<?
if(! ini_get("allow_url_fopen")) {
        print "Attempting to enable fopen() wrappers...<br>";
        ini_set("allow_url_fopen", 1);
       
        if(ini_get("allow_url_fopen")) {
                print "External URL's are now enabled for fopen() wrappers.";
        }
        else {
                print "Unable to enable fopen() wrappers.";
        }
}
else {
        print "allow_url_fopen is already enabled...";
}
?>


Also, please give what OS, server and php version you are using.

Regards,
Barry
hu hu hu good guess, fopen wrappers could be off its server ;-)
Avatar of FORege

ASKER

Barry,

your php script works fine.  I get:

"External URL's are now enabled for fopen() wrappers."

I think I found the problem though... please confirm.  Here's the info my hosting company sent me regarding my server version:

***************************************************
Our servers are equipped with Dual 1Ghz processors and 2GB of RAM to
ensure a fast and reliable system for our users. Our operating system is
FreeBSD v4.4-stable providing our users with access to a secure UNIX
based system. Our server software is Apache v1.3.19 Other employed
software includes Tomcat v3.3 with JDK v1.2.2.

We support MySQL Ver 11.15 Distrib 3.23.42, for -freebsd4.4 (i386)

All the accounts are setup with Perl 5.6.1, mod_perl 1.27, mod_fastcgi
2.2.12.

The PHP version that we support is 4.3.
*****************************************************

I'm trying to load MySQL ver 3.23.55.  Could that simple little difference in db versions cause this?

Thanks again, FORege
Avatar of FORege

ASKER

please ignore the MySQL reference above.  I'm trying to multitask and things are getting all mixed up:)

FORege
ASKER CERTIFIED SOLUTION
Avatar of axis_img
axis_img

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of FORege

ASKER

Barry,

adding "ini_set("allow_url_fopen", 1);" to my script removed the error is was getting.  Thanks for that!

Now I am getting the dreaded "page cannot be displayed" error from my browser (ie 6.0).

I don't believe that this is coming from my server because I have custom 400 and 404 error pages defined.  I think that there is something was with the script below the $xml_file declaration.

I tested this mini script to check this theory:

<?
ini_set("allow_url_fopen", 1);
$xml_file = "http://p.moreover.com/cgi-local/page?c=Africa%20news&o=xml";
/*if (!($fp = fopen($xml_file, "r"))) {
die("Could not open your file for parsing!\n");*/
echo ("XML file is $xml_file");
print "<br>this works";
?>

No problems with the above at all.  I also verified that the URL i'm pointing to was valid.  

What would cause "page cannot be displayed" in the remaining script?

Thanks again,

FORege