Page 1 of 1
using file* to initiate a separate http request
Posted: Wed Aug 22, 2007 9:25 pm
by s.dot
I've the need to capture a php script's output into a variable. Using shell_exec() or other system functions isn't an option to me, although shell_exec() is the behavior I'm after.
I was told to use file_get_contents() or fsockopen() to initiate a separate HTTP request. I've played around with fsockopen() for quite a bit now, and can't seem to get it.
what I would like to do
Code: Select all
$contents = file_get_contents('myscript.php');
Only, not have the php code returned, but the php code to be executed and the result returned.
Posted: Wed Aug 22, 2007 9:43 pm
by Christopher
If your server is configured to do it, you would need to specify the protocol. It would be:
Code: Select all
$contents = file_get_contents('http://www.mydomain.com/myscript.php');
Did you look at the doc and comments?
http://www.php.net/function.file_get_contents
Posted: Wed Aug 22, 2007 9:48 pm
by feyd
You have to actually make it an HTTP request to do it with
file_get_contents(); http:// and all.
You're basically doing an
ob_start()+
include()+
ob_get_clean()... but since this is likely apart of your cron solution won't shunt to a separate thread/process.
Posted: Wed Aug 22, 2007 9:54 pm
by s.dot
Yes, I've read it many times.
What server configurations does it depend on?
@feyd, yes, you're right. I'm thinking I stand better to do the ob_start(), include, ob_get_clean(), since many shared server configurations are very nitpicky about what they let you use with system calls and file calls (found this out today).
I wouldn't imagine they'd have much control over ob_* and include.
I'm not worrying about forking the process right now, just trying to get what I have working.
Posted: Wed Aug 22, 2007 10:08 pm
by s.dot
I like that, it works nicely.

Thanks.
Is there a way to not have php save the variables with the include() call, but disregard them?
Posted: Wed Aug 22, 2007 10:09 pm
by feyd
The other thing to worry about is possible collisions that may occur as a result of including these files.. specifically redefinition of functions and classes. Provided they are engineered carefully, you won't run into problems, but it's quite possible to hit errors in many cases. That's why file_get_contents() and fsockopen() are generally better solutions.
Posted: Wed Aug 22, 2007 10:13 pm
by s.dot
file_get_contents() would require me knowing the domain, and as aborint said, server configuration
Posted: Wed Aug 22, 2007 10:17 pm
by feyd
That's all possible to determine via $_SERVER and some logic. The only problem is it has to be accessible via a HTTP request. Granted, an .htaccess could limit the exposure that would have to only accept requests from the server itself...
Posted: Wed Aug 22, 2007 10:29 pm
by s.dot
Looks like allow_url_fopen is off in the one shared hosting server I tested. Looks like I'll be going the include() route.
My only concern is memory hogging in an infite loop. Granted, the same variables will be overwritten, when necessary, but they will never be discarded from memory either.
Posted: Wed Aug 22, 2007 10:37 pm
by feyd
You can snapshot the variables prior to the call to
include() and after, then
unset() as needed.
Posted: Wed Aug 22, 2007 10:44 pm
by s.dot
Snapshot.. as in create an array of variables that are defined, and then unset ones that aren't in that array?
From the user comments on get_defined_vars(), I was thinking something like this would be effective.
Code: Select all
function getDefinedVars($varList, $excludeList)
{
$temp1 = array_values(array_diff(array_keys($varList), $excludeList));
$temp2 = array();
while (list($key, $value) = each($temp1)) {
global $$value;
$temp2[$value] = $$value;
}
return $temp2;
}
$excludeList = array('GLOBALS', '_FILES', '_COOKIE', '_POST', '_GET', 'http_response_header', 'excludeList');
$extraVars = getDefinedVars(get_defined_vars(), $excludeList);
foreach($extraVars AS $extraVar)
{
unset($extraVar);
}
Posted: Wed Aug 22, 2007 11:08 pm
by Benjamin
Maybe I missed something here, but why not..
Code: Select all
ob_start();
// do stuff
$captured_stuff = ob_get_contents();
ob_end_clean();