PHP-将Web脚本作为命令行脚本执行?

Codefun64

使用PHP 5.4.28版本,我要完成的工作是:

  • 我拥有的一个Facebook应用是脚本“ otherevent.php”。它不应对任何用户公开可见。由于Facebook要求它具有网址,因此它是Web服务器上的后端应用程序,并且该应用程序由其他脚本运行和管理。

  • 我必须运行和管理应用程序的第一个脚本称为“ app.php”。理想情况下,它要做的就是执行“ otherevent.php”,获取输出,并将输出回显到屏幕上。

  • 除其他外,疯狂还会以“意外的T_STRING,预期为T_CONSTANT_ENCAPSED_STRING”错误的形式出现。

我在这里为三个测试文件“ otherevent.php”,“ app.php”和“ test.php”提供了注释和代码,这是用于调试错误的测试文件。开始:

test.php:

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

?>

otherevent.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;


  // 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();
}
?>

最后,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;

?>

在此实例中,我想要做的就是使app.php能够执行otherevent.php,获取登录URL(或它输出的任何测试输出),并将登录URL打印到屏幕上。如何做到这一点?显然,它并不像看起来那样简单。

磨砂

您在'use'关键字周围使用php-cli导致的错误使我相信您使用的是旧版本的PHP ..但为解决您的问题的一部分,建议您不要使用shell_exec来运行其他PHP代码。捕获脚本的输出,可以使用如下所示的输出缓冲区:

<?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;

?>

如果您之后要立即回显输出,则指向输出缓冲区没有意义。

检查您的PHP版本。该SDK需要5.4+。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

PHP执行后台命令行脚本

PHP脚本命令行执行问题

命令行PHP脚本无法正确执行

如何找到启动PHP命令行脚本的文件夹?

从Windows 7中的php命令行脚本复制到剪贴板

如何在PHP中处理命令行脚本的路径(Windows)?

ffmpeg命令通过命令行执行,但不通过PHP脚本执行

确定PHP脚本的命令行执行与HTTP执行的规范方法是什么?

从命令行运行PHP脚本作为后台进程

使用PHP脚本在命令行中运行PHP文件

从php文件执行脚本

将参数从命令行传递到PHP类和函数脚本

从Windows命令行CMD触发php脚本

获取命令行脚本的输出作为模板变量

命令行脚本 - 作为新手使用什么

Matlab从Linux命令行执行脚本

如何将命令行参数传递给从可执行脚本启动的NodeJS

Laravel中的命令行脚本?

在Linux命令行中同时运行PHP脚本多次

在win10上全局通过命令行运行PHP脚本

如何为命令行PHP脚本触发XDebug分析器?

如何在Heroku App上运行命令行PHP脚本

从 PHP 调用的 Shell 脚本不起作用,但可以在命令行中运行

尝试从命令行运行 PHP 脚本(可能是路径问题)

“ps -ef”在php脚本与命令行中运行时返回不同的结果

重试bash脚本中的命令行,直到成功,然后继续执行脚本

如何在执行脚本中使用Apple脚本的命令行参数?

如果将目录路径作为命令行参数传递给python,则执行哪个python脚本?

如何将字符串从php传递到tcl并执行脚本