相关问题:PHP - execute web script as command line script?
所以我有一个脚本,我们称它为MainScript.php,它执行以下操作:
我必须这么做的原因是因为如果我只是让MainScript.php处理数据,或者将OtherScript.php作为子进程运行来处理数据,那么在我的服务器上它将无法供其他用户访问;它会忙于处理数据。我需要它来处理数据,但仍可供其他用户使用。为此,我每次用户访问并向MainScript.php输入数据时都会生成一个新进程,以便它保持可用状态,并迅速处理用户的数据。
然而,我得到的错误是最奇怪的;当MainScript.php使用shell_exec()启动ThirdScript.php,然后刷新页面以说明"您的数据正在处理"或者用户的东西,MainScript.php抛出以下错误:
PHP解析错误:语法错误,意外T_STRING,期待T_CONSTANT_ENCAPSED_STRING或'('在第6行的/home7/fantatv1/public_html/MainScript.php
第6行是:
使用Facebook \ FacebookSession;
我不确定问题是什么,因为当我直接从命令行运行ThirdScript.php时,只需将一些参数硬编码到其中(以模拟从MainScript.php用户收集的数据),它运行得很好。没有错误。
但是,我有一个理论认为,由于MainScript.php是"使用" ing命名空间,然后OtherScript也使用它们,当用户将数据输入到表单上时,MainScript.php会刷新自身。页面,调用"使用"有一些问题在已经存在的名称空间上"使用" d。这只是一个想法,但我不知道实际问题是什么。任何人都可以给我一些建议或帮助吗?
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']);
}
?>
答案 0 :(得分:0)
PHP解析错误:语法错误,意外T_STRING,期待T_CONSTANT_ENCAPSED_STRING或&#39;(&#39; in ...
当解释器完全不了解命名空间时,就会发生这种情况,即使用的PHP版本低于5.3。
另请参阅this 3v4l,它会为这些版本发出相同的解析错误。