PHP - execute web script as command line script?

风格不统一 提交于 2020-01-16 18:48:47

问题


Using PHP version 5.4.28, what I'm trying to accomplish is:

  • A facebook app I have is script "otherevent.php". It should not be publicly visible to any user. It is a backend app that is on the web server, due to Facebook requiring it to have a web address, and the app is run and managed by other scripts.

  • The first script I have to run and manage the app is called "app.php". Ideally all it's supposed to do is execute "otherevent.php", get the output, and echo the output to the screen.

  • Craziness ensues in the form of "unexpected T_STRING, expecting T_CONSTANT_ENCAPSED_STRING" errors, amongst other things.

I have comments and code here for three test files, "otherevent.php", "app.php", and "test.php" which is a test file used for debugging the errors. Here goes:

test.php:

echo $argv[1] . " TADA!";
exit();

?>

otherevent.php (trimmed down and censored, for sensitive information):

require_once( 'Facebook/FacebookSession.php' );
require_once( 'Facebook/FacebookRedirectLoginHelper.php' );
require_once( 'Facebook/FacebookRequest.php' );
require_once( 'Facebook/FacebookResponse.php' );
require_once( 'Facebook/FacebookSDKException.php' );
require_once( 'Facebook/FacebookRequestException.php' );
require_once( 'Facebook/FacebookServerException.php' );
require_once( 'Facebook/FacebookOtherException.php' );
require_once( 'Facebook/FacebookAuthorizationException.php' );
require_once( 'Facebook/GraphObject.php' );
require_once( 'Facebook/GraphSessionInfo.php' );
require_once( 'Facebook/GraphUser.php' );

use Facebook\FacebookSession;
use Facebook\FacebookRedirectLoginHelper;
use Facebook\FacebookRequest;
use Facebook\FacebookResponse;
use Facebook\FacebookSDKException;
use Facebook\FacebookRequestException;
use Facebook\FacebookServerException;
use Facebook\FacebookOtherException;
use Facebook\FacebookAuthorizationException;
use Facebook\GraphObject;
use Facebook\GraphSessionInfo;
use Facebook\GraphUser;


  // Get session variable!
session_start();
$output = array(
);
// Initialize the app with the app's "secret" and ID. These are private values.
FacebookSession::setDefaultApplication( 'xxxxxxxxxxxx','xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' );

$arg1 = $_SESSION['arg1']; //This is a password variable that tells the script it's being run by an authorized script on the server, not by an outside source.
$arg2 = $_SESSION['arg2']; //This is an argument that tells the script what it's supposed to be trying to do.
if($arg1 != "whatever"){
    echo"Something went wrong; the password variable didn't work.";
    //The following line is what is -supposed- to happen when this codeblock executes, but we're debugging right now, so we're providing output instead to see what happens in the code.
    //header("Location: http://www.fantasycs.com/app.php");
}elseif($arg2 == "loginurl"){
    echo "It worked! Login URL will be displayed here.";
    unset($_SESSION['arg2']);
    unset($_SESSION['arg1']);
    exit();
}
?>

And finally, app.php:

<?php

session_start();
$_SESSION['arg1'] = "whatever";
$_SESSION['arg2'] = "loginurl";

$output = shell_exec('php-cli test.php Thisworks'); //This runs test.php and $output captures the output properly just fine. No worries.

echo $output;


$output = shell_exec('php test.php Thisworks'); //This keeps running indefinitely so far as I can tell, and I can't see what's happening because no output is produced. It simply keeps trying to load the page forever. No output is seemingly capture. Bizarre.

echo $output;


$output = shell_exec('php otherevent.php'); //This has the same problem as when you try to run test.php with the "php" command. It stalls.

echo $output;


$output = shell_exec('php-cli otherevent.php Thisworks'); //This produces an error on the "use" lines in otherevent.php. It says there's a syntax error on those lines. This has never happened or appeared before; these lines, simply put, do NOT have any errors in them. They've run fine before and have never been changed. As a result of the error, the script is aborted, and no output is captured.

echo $output;

?>

All I want in this instance is for app.php to be able to execute otherevent.php, get the login URL (or whatever test output it outputs), and print the login URL to the screen. How does one do that? Apparently it's not as simple as it seems.


回答1:


Your error using php-cli around the 'use' keywords leads me to believe you have an old version of PHP.. but to address one part of your question, I suggest not using shell_exec for running other PHP code.. If you need to capture the output of the script, you can use an output buffer like this:

<?php

session_start();
$_SESSION['arg1'] = "whatever";
$_SESSION['arg2'] = "loginurl";

// Start capturing the output in a buffer
ob_start();
include 'otherevent.php';    
// Get the data and close the buffer
$output = ob_get_clean();
echo $output;

?>

There's no point to the output buffer if you're going to echo the output immediately afterwards.

Check your version of PHP. That SDK requires 5.4+.



来源:https://stackoverflow.com/questions/24337132/php-execute-web-script-as-command-line-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!