Copying file... failed to open stream

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
cggreer
Forum Newbie
Posts: 14
Joined: Mon Jul 21, 2003 6:39 am

Copying file... failed to open stream

Post by cggreer »

G'day everyone!

My current project involves grabbing news headlines from a local news website and slotting them into the client's website. There's no copyright issues involved - all that has been cleared up (thank goodness :? )

Here's the problem:

Code: Select all

<?php
$GrabURL = urlencode("http://www.news.com.au/common/storylib/0,4810,BreakingNews%255EBREAKING%2520NEWS%255ETEXT%255Enews,00.html");
// 1. Grab the HTML
$RetrieveFile = implode ('', file ($GrabURL));
?>
When this code executes it generates the following errors:

Code: Select all

Warning: file(http%3A%2F%2Fwww.news.com.au%2Fcommon%2Fstorylib%2F0%2C4810%2CBreakingNews%25255EBREAKING%252520NEWS%25255ETEXT%25255Enews%2C00.html): failed to open stream: No such file or directory in D:\Program Files\Apache Group\Apache2\htdocs\MyHSCAdmin\TMPsb0s1idfog.php on line 94

Warning: implode(): Bad arguments. in D:\Program Files\Apache Group\Apache2\htdocs\MyHSCAdmin\TMPsb0s1idfog.php on line 94
When I replace the nasty looking $GrabURL with a local copy of the same file everything works no worries. The fopen_wrappers are turned on. I get the same errors running on two different servers!
Any help is v.v.appreciated!

Cheers,
Clinton Greer
cggreer@it.uts.edu.au
choppsta
Forum Contributor
Posts: 114
Joined: Thu Jul 03, 2003 11:11 am

Post by choppsta »

At a glance it looks like you're urlencoding something that is already URL encoded...
cggreer
Forum Newbie
Posts: 14
Joined: Mon Jul 21, 2003 6:39 am

Post by cggreer »

Even without the urlencode() I still get the same errors on both servers.
I thought it might have been a problem with all the comments and arguments in the URL, however I get the same errors trying to load a plain .html page from another server.

I know both files exist because I can view them in the browser. Is there some setting in PHP.ini or in httpd.conf that needs to be changed to make this work?

Even trying to do a simple include("http://...") fails with similar errors.

eeek! Please help! :s

Clinton Greer
cggreer@it.uts.edu.au
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

...

Post by kettle_drum »

If your getting errors that way, then why not try a different way of doing it. PHP has several ways to do every task - so just take a different route. Maybe something like:

Code: Select all

<?php

$file = "http://blah.com";
$fp = fopen($file, r);
while (!feof($fp)) {
   $data = fgets($fp, 1024);
   #do what you want with the data
}
?>
cggreer
Forum Newbie
Posts: 14
Joined: Mon Jul 21, 2003 6:39 am

Post by cggreer »

Thanks, but I tried that too. It seems that any kind of function that involves resolving a remote address ie. "http://..." causes it to come up with this error.

Like I said, I thought it might have been the complex URL I was trying to grab, but it doesn't work using simple http://server.com/page1.html either.

As the page I intend to copy from is updated hourly, manually copying the file to the local system to reference isn't an option.

Any thoughts?

Ciao!
Clinton Greer
cggreer@it.uts.edu.au
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

Have you had a look at the snoopy-class?
I find it quite useful in cases like yours.
cggreer
Forum Newbie
Posts: 14
Joined: Mon Jul 21, 2003 6:39 am

Post by cggreer »

Snoopy looks promising, with lots of functionality. I'll try it out tomorrow morning and let you know! Fingers-crossed!!

Thanks mate!
Clinton Greer
cggreer@it.uts.edu.au
cggreer
Forum Newbie
Posts: 14
Joined: Mon Jul 21, 2003 6:39 am

Post by cggreer »

ARGH! Snoopy does not work either - I end up with the same errors!

Here's the code I'm running (Snoopy sample):

Code: Select all

<?php
	include "Snoopy.class.inc";
	$snoopy = new Snoopy;
	
	$snoopy->user = "joe";
	$snoopy->pass = "bloe";
	
	if($snoopy->fetch("http://www.slashdot.org/"))
	{
		echo "response code: ".$snoopy->response_code."<br>\n";
		while(list($key,$val) = each($snoopy->headers))
			echo $key.": ".$val."<br>\n";
		echo "<p>\n";
		
		echo "<PRE>".htmlspecialchars($snoopy->results)."</PRE>\n";
	}
	else
		echo "error fetching document: ".$snoopy->error."\n";?>
Here's the errors I get (Apache 2.0.45 and PHP 4.2.3):

Code: Select all

Warning: fsockopen(): php_network_getaddresses: gethostbyname failed in D:\Program Files\Apache Group\Apache2\htdocs\MyHSCAdmin\Snoopy.class.inc on line 1061

Warning: fsockopen(): unable to connect to www.slashdot.org:80 in D:\Program Files\Apache Group\Apache2\htdocs\MyHSCAdmin\Snoopy.class.inc on line 1061
error fetching document: connection failed (0)
Anyone suggest anything?? PLEASE??!

Thanks,
Clinton Greer
cggreer@it.uts.edu.au
Post Reply