note 31486 deleted from function.include by sniper
| From: | sniper@php.net | Date: | Tue, 04 Nov 2003 00:41:30 +0000 |
| Subject: | note 31486 deleted from function.include by sniper | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-59786@lists.php.net to get a copy of this message | ||
Note Submitter: mlewis@nospam.piqsoftware.com
----
If you're needing to include pieces of a page from another server (I do this to have a public
poll mirrored on two separate web sites) and the include() or require() is taking a long time to
process, consider using fsockopen() to grab the file instead of waiting for the include(). This only
works well if the include()/require() is slow but the remote script executes fast.
<?php
$fp = fsockopen ("remote.server.com", 80, $errno, $errstr, 30);
if (!$fp)
{
echo "Can't fetch remote data: $errstr ($errno)\n";
}
else
{
fputs ($fp, "GET /path/to/page.php\r\n");
$doit = 0;
while (!feof($fp))
{
$thing = fgets ($fp,128);
if(substr($thing, 0, 10) == "<!--tag-->")
$doit = 1;
if($doit == 1)
echo $thing;
}
fclose ($fp);
}
?>
At the top of page.php, add the HTML comment line <!--tag--> so that the script will work
correctly. The tag line is used to denote the end of the HTTP headers and the beginning of the
actual HTML output.
This will still process the script remotely, so it is ideal for sharing small pieces of data between
sites (such as a poll) without needing to share the database(s). Be careful when doing this with
large or complex scripts, as you will chew up CPU cycles quickly by waiting for the socket to
finish.
Hope someone finds this useful.