file_get_contents() will not open remote file.
Moderator: General Moderators
-
kjcornwell
- Forum Newbie
- Posts: 8
- Joined: Thu May 25, 2006 8:28 am
file_get_contents() will not open remote file.
I can't get file_get_contents('http://www.google.com') to retrieve a remote page. It works fine for local files, file_get_contents("c:\file.txt"). I have all errors and warning turned on in php.ini (error_reporting = E_ALL) and yet all I get in my browser is "The connection was reset" in firefox and "The page cannot be displayed" in IE (btw, error reporting IS sent to the browser and does work in other situations). I also get the same "connection to browser was reset" message for fopen("http://www.google.com", "r"). allow_url_fopen is set to "on" and verified in phpinfo(). According to godaddy port 80 is open and the problem is in my script (btw, my 2003 firewall is disabled).
What the heck???
Here is my rig...
Server 2003
IIS 6.0
PHP Version 4.4.2
I am using the php4isapi.dll as suggested in php manual install zip.
Virtual Dedicated Account at godaddy.com.
Any suggestions/ideas?
Thanks,
Kevin C
What the heck???
Here is my rig...
Server 2003
IIS 6.0
PHP Version 4.4.2
I am using the php4isapi.dll as suggested in php manual install zip.
Virtual Dedicated Account at godaddy.com.
Any suggestions/ideas?
Thanks,
Kevin C
Last edited by kjcornwell on Thu May 25, 2006 10:36 am, edited 2 times in total.
- xpgeek
- Forum Contributor
- Posts: 146
- Joined: Mon May 22, 2006 1:45 am
- Location: Kyiv, Ukraine
- Contact:
Check option - "fopen wrappers" is on?
You code is wrong.
Try it.
read about wrappers here http://ua2.php.net/manual/en/wrappers.http.php
You code is wrong.
Try it.
Code: Select all
echo file_get_contents('http://www.google.com');-
kjcornwell
- Forum Newbie
- Posts: 8
- Joined: Thu May 25, 2006 8:28 am
Thanks but no help. 
here is the Fopen Section from php.ini
This was a message board typo...
I used the proper url in my actual page.
Any other ideas?
here is the Fopen Section from php.ini
Code: Select all
;;;;;;;;;;;;;;;;;;
; Fopen wrappers ;
;;;;;;;;;;;;;;;;;;
; Whether to allow the treatment of URLs (like http:// or ftp://) as files.
allow_url_fopen = 1
; Define the anonymous ftp password (your email address)
;from="john@doe.com"
; Define the User-Agent string
; user_agent="PHP"
; Default timeout for socket based streams (seconds)
default_socket_timeout = 60
; If your scripts have to deal with files from Macintosh systems,
; or you are running on a Mac and need to deal with files from
; unix or win32 systems, setting this flag will cause PHP to
; automatically detect the EOL character in those files so that
; fgets() and file() will work regardless of the source of the file.
; auto_detect_line_endings = OffCode: Select all
file_get_contents('http:www.google.com');Any other ideas?
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
I was running into the same issue, here is what I came up with: viewtopic.php?p=266317&highlight=#266317
-
kjcornwell
- Forum Newbie
- Posts: 8
- Joined: Thu May 25, 2006 8:28 am
-
kjcornwell
- Forum Newbie
- Posts: 8
- Joined: Thu May 25, 2006 8:28 am
THanks neogeek.neogeek wrote:I was running into the same issue, here is what I came up with: viewtopic.php?p=266317&highlight=#266317
It works! But why?????????
Any PHP experts out there know what the heck is going on? I'd much rather use file_get_contents(), it's much more elegant.
Last edited by kjcornwell on Thu May 25, 2006 4:05 pm, edited 1 time in total.
-
kjcornwell
- Forum Newbie
- Posts: 8
- Joined: Thu May 25, 2006 8:28 am
Well what happened was this. I was looking to download this file:
And when you go to it manually it brings you to this file:
But when you use the function that I wrote, you get this in the file that it returns:
I'm sure that it would be easy to find a way around this, like looking for 302 Found and doing a regex for a url in the page, but I'm going to see if there is any other more solid way of doing it than that. I see that as a last option.
Code: Select all
http://startupguide.typepad.com/favicon.icoCode: Select all
http://6a.typepad.com/favicon.icoCode: Select all
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>302 Found</title>
</head><body>
<h1>Found</h1>
<p>The document has moved <a href="http://6a.typepad.com/favicon.ico">here</a>.</p>
</body></html>Actually, all you need is the 'header', no need to parse the 'body'neogeek wrote: I'm sure that it would be easy to find a way around this, like looking for 302 Found and doing a regex for a url in the page,
timvw@madoka:~$ telnet startupguide.typepad.com 80
Trying 204.9.178.60...
Connected to typepad.com.
Escape character is '^]'.
GET /favicon.ico HTTP/1.0
Host: startupguide.typepad.com
HTTP/1.0 302 Moved Temporarily
Date: Thu, 25 May 2006 22:15:34 GMT
Server: Apache
Location: http://6a.typepad.com/favicon.ico
Content-Length: 217
Content-Type: text/html; charset=iso-8859-1
X-Cache: MISS from http://www.sixapart.com
Connection: close
[snipped body]
Thank timvw, I didn't know that the url it was redirecting to was in the header. Thus I updated the function to include a check to see if its getting the correct file.
Edit: Replaced my lnbr constant with its actual value.
Code: Select all
function fetch_remote_file($file) {
$path = parse_url($file);
$fs = @fsockopen($path['host'], 80);
if ($fs) {
$header = 'GET ' . $path['path'] . ' HTTP/1.0' . "\n";
$header .= 'Host: ' . $path['host'] . str_repeat("\n", 2);
fwrite($fs, $header);
$buffer = '';
while ($tmp = fread($fs, 1024)) { $buffer .= $tmp; }
preg_match('/Location: (.*+)/', $buffer, $matches);
if ($matches[1] && $file != trim($matches[1])) { return fetch_remote_file(trim($matches[1])); }
preg_match('/Content-Length: ([0-9]+)/', $buffer, $matches);
if ($matches[1] > 0) { return substr($buffer, -$matches[1]); } else { return false; }
} else { return false; }
}Actually, http/1.0 and the Host header don't go hand in hand very well... Better use the absolute URL for the request...
(But there is no need to reinvent the wheel, since http://www.php.net/curl already does this for us...)
And the first thing i would do when i recieve a http response is look at the status codeGET http://example.com/favicon.ico HTTP/1.0
Connection: close
If it's 200, everything is fine.. otherwise you may need to do something extra...http/$version $status $reason
(But there is no need to reinvent the wheel, since http://www.php.net/curl already does this for us...)
Very true. Only I wanted a surefire way to fetch files especially if the server this script was running on didn't have that library installed. (I now know that it is a common PHP library on many server, but it was still fun trying to find a solution to a problem that I though to be unsolvable)timvw wrote:(But there is no need to reinvent the wheel, since http://www.php.net/curl already does this for us...)