Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Alternate to file_get_contents()

Status
Not open for further replies.

DonP

IS-IT--Management
Jul 20, 2000
684
US
I am using:

Code:
$PageCode = file_get_contents("[URL unfurl="true"]http://"[/URL] . $LinkDetails);

on one of my sites for a special feature that gets any page's contents, strips it of certain unneeded items, inserts the results into a temporary table, then presents it to the screen for printing. The variable simply builds a URL with any parameters needed for getting the appropriate database entry, which is then fetched by file_get_contents(). The purpose is that a single printpage.php script is needed for printing many dissimilar pages in various areas of the site.

It was working fine until the hosting company made a security change to the server a few weeks ago that causes it to crash with a 406 error message. The exact error is:

Code:
failed to open stream: HTTP request failed! HTTP/1.1 406 Not Acceptable

Is there some alternative that can fetch the contents if a page similarly to the way file_get_contents() does? Any help is greatly appreciated.

Don
Experienced in HTML, Perl, PHP, VBScript, PWS, IIS and Apache and MS-Access, MS-SQL, MySQL databases
 
try grabbing a file using this function
Code:
	function getFile($filename){
		$return = '';
		if ($fp = fopen($filename, 'rb')) {
			while (!feof($fp)) {
				$return .= fread($fp, 1024);
			}
			fclose($fp);
			return $return;
	
		} else {
			return false;
		}
	}
example:

$html=getfile(" . $LinkDetails);
//stores the file to the $html variable

you can write a script to strip out whatever you want
 
Thanks! The function works perfectly here as did file_get_contents() but it still crashes with the same error on the live server. I am wondering if there is some way to send a header, which the hosting company implied was missing.

Don
Experienced in HTML, Perl, PHP, VBScript, PWS, IIS and Apache and MS-Access, MS-SQL, MySQL databases
 
It sounds as though your hosting company has disabled the ability of PHP to access files that are not on the server. I believe it's called the fopen wrapper. I do not know of any ways around it other then using direct port connections, which, would be very difficult. Hopefully someone else around here does.
 
what you can try next is fsockopen()

blatant code rip from the manual page foillows:

Code:
<?php
$fp = fsockopen("[URL unfurl="true"]www.example.com",[/URL] 80, $errno, $errstr, 30);
if (!$fp) {
   echo "$errstr ($errno)<br />\n";
} else {
   $out = "GET / HTTP/1.1\r\n";
   $out .= "Host: [URL unfurl="true"]www.example.com\r\n";[/URL]
   $out .= "Connection: Close\r\n\r\n";

   fwrite($fp, $out);
   while (!feof($fp)) {
       echo fgets($fp, 128);
   }
   fclose($fp);
}
?>

______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
Thanks, I'll try it tonight when I get home! It looks promising and is unlike anything I've tried so far. I presume I can use a full URL to a specific file and pass along a paramater that it needs to get the appropriate database entry.

The hosting company suggested I use the full server path rather than the URL but, as the page is actually another script with dynamic content, that doesn't work. The problem itself is odd because everything is located on the same server so they must have gone somewhat overboard in their recent security changes.

Don
Experienced in HTML, Perl, PHP, VBScript, PWS, IIS and Apache and MS-Access, MS-SQL, MySQL databases
 
Knowing now that fsocketopens() exists but not quite able to get the sample above to work for my needs, I tried another example from the manual, which seems to have done the trick:

Code:
function getFile($host,$query,$others=''){
   $path=explode('/',$host);
   $host=$path[0];
   unset($path[0]);
   $path='/'.(implode('/',$path));
   $post="POST $path HTTP/1.1\r\nHost: $host\r\nContent-type: application/x-[URL unfurl="true"]www-form-urlencoded\r\n${others}User-Agent:[/URL] Mozilla 4.0\r\nContent-length: ".strlen($query)."\r\nConnection: close\r\n\r\n$query";
   $h=fsockopen($host,80);
   fwrite($h,$post);
   for($a=0,$r='';!$a;){
       $b=fread($h,8192);
       $r.=$b;
       $a=(($b=='')?1:0);
   }
   fclose($h);
   return $r;
}

$PageCode = getFile("[URL unfurl="true"]www.domainname.com/docs/script.php",[/URL] "ID=3");

It works perfectly! Thanks everyone for the help.

Don
Experienced in HTML, Perl, PHP, VBScript, PWS, IIS and Apache and MS-Access, MS-SQL, MySQL databases
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top