Related question: PHP - execute web script as command line script?
So I've got a script, we'll call it MainScript.php, that does the following:
The reason I must do all this is because if I just had MainScript.php process the data, or run OtherScript.php as a child process to process the data, it would be unavailable on my server for other users to access; it would be busy processing data. I need it to process data but remain available for other users. To that end, I have it spawn a new process for each time a user accesses and enters data into MainScript.php, so that it remains available, and the user's data is processed swiftly.
However, the error I get is most odd; when MainScript.php uses shell_exec() to start ThirdScript.php, and then refreshes the page to say "your data is processing" or something to the user, MainScript.php throws the following error:
PHP Parse error: syntax error, unexpected T_STRING, expecting T_CONSTANT_ENCAPSED_STRING or '(' in /home7/fantatv1/public_html/MainScript.php on line 6
Line 6 is:
use Facebook\FacebookSession;
I'm not sure what the problem is, because when I run ThirdScript.php directly from the command line, and simply hardcode some arguments into it (to simulate the data gathered from the user from MainScript.php), it runs just fine. No errors.
However, I have a theory that since MainScript.php is "use"ing namespaces, and then OtherScript is also using them, and MainScript.php refreshes itself when the user enters data into the form on the page, there's some problem with calling "use" on a namespace that's already being "use"d. This is just an idea, but I have no idea what the actual problem is. Can anyone give me some advice or help?
MainScript.php:
<?php
session_start();
require_once ('otherevent.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;
$display_login = 0;
$display_logout = 0;
if ( isset( $_SESSION ) && isset( $_SESSION['fb_token'] )) {
// create new session from saved access_token
$session = new FacebookSession( $_SESSION['fb_token'] );
// validate the access_token to make sure it's still valid
try {
if ( !$session->validate() ) {
$session = null;
$display_login = 1;
}else{
$display_logout = 1;
}
} catch ( Exception $e ) {
// catch any exceptions
$session = null;
$display_login = 1;
}
} else {
// no session exists
try {
$session = $helper->getSessionFromRedirect();
if(isset($session)){
if ( !$session->validate() ) {
$session = null;
$display_login = 1;
}else{
$display_logout = 1;
}
}else{
$display_login = 1;
}
} catch( FacebookRequestException $ex ) {
echo'<p>There was a problem with your facebook session. Please re-login to Facebook, and then try loading the app again.</p>';
//echo $ex;
// When Facebook returns an error
} catch( Exception $ex ) {
echo'<p>There was a validation issue with your previous session. Please log in again.</p>';
// When validation fails or other local issues
//echo $ex;
}
}
if(isset($session)){
$_SESSION['fb_token'] = $session->getToken();
// create a session using saved token or the new one we generated at login
$session = new FacebookSession( $session->getToken() );
}
if($display_login == 1){
//Display the login URL for the app. To do this, we must first -obtain- the login URL for the app, since the app is not actually accessible to the user.
echo "<a href=\"";
loginurl(); //This is a function in OtherScript.php, which simply outputs the login URL.
echo "\">Login</a>";
}else if($display_logout == 1){
$request = new FacebookRequest( $session, 'GET', '/me' );
$response = $request->execute();
// get response
$GraphObject = $response->getGraphObject(GraphUser::className());
// print profile data
$name = $GraphObject->getName();
if($GraphObject->getID() == xxxxxxxxxxxx){ //Makes sure I'm the only one logged in (currently I'm the only person allowed to use the app, and I haven't started working on a database for authorized users just yet).
echo 'You are logged in, ' . $name . '.';
if(isset($_POST['thetextbox'])){
echo"<br>Your request is being evaluated. Please visit this page to see the progress on your request. *URL TO USER FILE GOES HERE*";
shell_exec('php ThirdScript.php token="' . $session->getToken() . '" text="' . $_POST['thetextbox']); //Clumsy way of handing the data off to a separate process/script. Ideally, this shell command will spawn a new process, that evaluates and thus writes to file all the data the user wants, using $session and all other data passed via $_GET through the shell.
}
echo'<p><form method="POST" action="">
Enter Random Crap Here: <input type="text" name="thetextbox"> <br>
<input type="submit" value="Submit">
</form>
</p>';
}else{
echo "You are not authorized to use this app, $name.";
}
}
echo '
</body>
</html>
';
?>
Otherscript.php:
<?php
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;
// start session
session_start();
// init app with app id and secret
FacebookSession::setDefaultApplication( 'xxxxxxxxxxxxxxx','xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' );
// login helper with redirect_uri
$helper = new FacebookRedirectLoginHelper( 'http://google.com' );
function loginurl() {
global $helper;
echo $helper->getLoginUrl( array( 'email' ) );
}
function evaluate($token, $text){
//Do stuff! The actual app does some stuff and then writes to a file. When I run ThirdScript.php from the command line, I see the file being written to, and it works -perfectly-. When I run it from a browser via MainScript.php (which is the only way to run these from a browser, so that MainScript.php is the front end), it has a parse error and stops running, and nothing is written to the file.
}
?>
Thirdscript.php:
<?php
session_start();
require_once ('otherevent.php');
if(isset($_GET["text"]) && isset($_GET['token'])){
evaluate($_GET['token'], $_GET['text']);
}
?>
PHP Parse error: syntax error, unexpected T_STRING, expecting T_CONSTANT_ENCAPSED_STRING or '(' in ...
This happens when the interpreter doesn't understand namespaces entirely, i.e. a version of PHP is used that's lower than 5.3.
See also this 3v4l which emits the same parse error for those versions.