Click to See Complete Forum and Search --> : failed to open stream: HTTP request failed


sickofitall_uk
07-14-2008, 05:20 PM
Hi, hope somebody can help me with this problem,

I am trying to open a remote xml file and write the contents to a file. If I copy the xml info to a local file everything works perfectly. However when I replace the location of the xml file with the current file thats held on a remote server, I get the following error message:

Warning: fopen(http://www.ipoker.com/summercashdash/SummerCashDashLB0.5-1_1-2.xml) [function.fopen]: failed to open stream: HTTP request failed!

I'm basically pretty much stumped now. Is it some kind of permissions problem?


<?php
// Sets the files we'll be using

$srcurl = 'http://www.ipoker.com/summercashdash/SummerCashDashLB0.5-1_1-2.xml'; // This must be a URL!
$tempfilename = 'tempindex.xml'; // This must be a filename!
$targetfilename = 'temp.xml'; // Also a filename!
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Generating <?php echo $targetfilename; ?></title>
<meta http-equiv="content-type"
content="text/html; charset=iso-8859-1" />
</head>
<body>
<p>Generating <?php echo $targetfilename; ?>...</p>
<?php

error_reporting (E_ALL);

// Begin by deleting the temporary file, in case
// it was left lying around. This might spit out an
// error message if it were to fail, so we use
// @ to suppress it.
@unlink($tempfilename);

// Load the dynamic page by requesting it with a
// URL. The PHP will be processed by the Web server
// before we receive it (since we're basically
// masquerading as a Web browser), so what we'll get
// is a static HTML page. The 'r' indicates that we
// only intend to read from this "file".
$dynpage = fopen($srcurl, 'r');

// Check for errors
if (!$dynpage) {
exit("<p>Unable to load $srcurl. Static page update aborted!</p>");
}

// Read the contents of the URL into a PHP variable.
// Specify that we're willing to read up to 1MB of
// data (just in case something goes wrong).
$htmldata = fread($dynpage, 1024*1024);

// Close the connection to the source "file", now
// that we're done with it.
fclose($dynpage);

// Open the temporary file (creating it in the
// process) in preparation to write to it (note
// the 'w').
$tempfile = fopen($tempfilename, 'w');

// Check for errors
if (!$tempfile) {
exit("<p>Unable to open temporary file ($tempfilename) for writing. Static page update aborted!</p>");
}

// Write the data for the static page into the
// temporary file
fwrite($tempfile, $htmldata);

// Close the temporary file, now that we're done
// writing to it.
fclose($tempfile);

// If we got this far, then the temporary file
// was successfully written, and we can now copy
// it on top of the static page.
$ok = copy($tempfilename, $targetfilename);

// Finally, delete the temporary file.
unlink($tempfilename);

?>
<p>Static page successfully updated!</p>
</body>
</html>


allow_url_fopen is set to on, I'm running this script on a localhost (xampp install).

Thanks for reading, hope somebody can help!

SyCo
07-14-2008, 07:50 PM
All I can think is they are preventing hot-linking to the file? Would seem odd for hot link protection to be on an RSS file unless it's for internal use or to be shared with certain other sites?

Contact the site owners to find out if you can be allowed to access it. It's a poker site, they'll probably take all the publicity they can!

chazzy
07-14-2008, 08:02 PM
this might be a stupid question, but do you possibly have a firewall/antivirus software running that's blocking PHP from talking to the outside world?

SyCo
07-14-2008, 08:04 PM
I got the same error with fopen() and file() and I'm pretty sure I should be able to access it. Could you open with fopen() chazzy?

sickofitall_uk
07-15-2008, 02:58 AM
Hi,
thanks very much for the replies. The xml file I'm trying to access is simply poker tournament information which is updated daily, so I wouldn't imagine they would disallow me linking to it especially as we are running this tournament with them, but I guess it's possible.

I have tried accessing this both from my work machine and at home in case it was the work firewall blocking me.

Hmm I'm pretty much stumped now, the only other thing I can do is manually save the source of the file to a local file on a daily basis and use it this way, unless anybody has any better ideas?

I was basically trying to capture the file and insert a link to an xsl stylesheet so I can format it (this works fine when the source xml is local).

Thanks

Znupi
07-15-2008, 06:41 PM
They're most probably filtering the User-Agent http header. If I use my custom http class which uses the header:
User-Agent: Mozilla 5.0 (compatible; HttpAnalyzer/0.5; +xxxx@gmail.com) it works. If I use file_get_contents it doesn't work, because PHP sends its own header, which they are probably filtering. I can post the class, if you want. You can also use cURL.
You can try my class here to see it works: http://znupi.ath.cx/work2/http-analyzer/

sickofitall_uk
07-16-2008, 03:11 AM
Hi Znupi,
thanks I've tried that link and it does indeed work when I enter the url. Would you mind posting the class? I tried adding the following line

ini_set('user_agent', 'Mozilla/5.0 (compatible; HttpAnalyzer/0.5; +xxxx@gmail.com');

but I'm guessing there must be more to it than this as it doesn't work?

I did also try using cURL, again this worked with a local file but not when i tried linking to the actual remote file

<?php

$url = "http://www.ipoker.com/summercashdash/SummerCashDashLB0.5-1_1-2.xml";

//grab the XML and save it to a temp XML file
$ch = curl_init($url);
$fp = fopen("temp.xml", "w");
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);

//Load the local XML that was created in the above CURL call
$xml = simplexml_load_file("temp.xml")
or Die ('ERROR: Could not load file');

print_r($xml);
?>


This causes the following error (amongst others) temp.xml:1: parser error : Document is empty in C:\xampp\htdocs\xmltest\curl.php on line 15

I'm guessing this is because the file appears empty?

Thanks alot!

Znupi
07-16-2008, 05:44 AM
Try this:
<?php

$url = "http://www.ipoker.com/summercashdash/SummerCashDashLB0.5-1_1-2.xml";

//grab the XML and save it to a temp XML file
$ch = curl_init($url);
$fp = fopen("temp.xml", "w");
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, Array("User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.15) Gecko/20080623 Firefox/2.0.0.15") ); // makes our request look like it was made by Firefox
curl_exec($ch);
curl_close($ch);
fclose($fp);

//Load the local XML that was created in the above CURL call
$xml = simplexml_load_file("temp.xml")
or Die ('ERROR: Could not load file');

print_r($xml);
?>

sickofitall_uk
07-16-2008, 06:40 AM
Znupi thanks so much, it works! I really didn't think this would be so much trouble but it was difficult to find anything even after several hours of searching. Seems to work great!