using file* to initiate a separate http request

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
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

using file* to initiate a separate http request

Post 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.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post 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
(#10850)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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?
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

file_get_contents() would require me knowing the domain, and as aborint said, server configuration
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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...
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

You can snapshot the variables prior to the call to include() and after, then unset() as needed.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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);
}
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post 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();
Post Reply