PHP - 执行web脚本作为命令行脚本? [英] PHP - execute web script as command line script?

查看:113
本文介绍了PHP - 执行web脚本作为命令行脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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




  • 我有一个Facebook应用程序脚本otherevent.php。任何用户都不应该公开看到它。这是一个后端应用程序,在网络服务器上,由于Facebook要求它有一个网址,该应用程序由其他脚本运行和管理。


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


  • Craziness的形式是意外的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');

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


//获取会话变量!
session_start();
$ output = array(
);
//使用应用程序的秘密和ID初始化应用程序。这些是私有价值。
FacebookSession :: setDefaultApplication('xxxxxxxxxxxx','xxxxxxxxxxxxxxxxxxxxxxxxxxxxx');

$ arg1 = $ _SESSION ['arg1']; //这是一个密码变量,用于告诉脚本由服务器上的授权脚本运行,而不是外部源。
$ arg2 = $ _SESSION ['arg2']; //这是一个参数,告诉脚本它应该是试图做什么。
if($ arg1!=whatever){
echo出现问题,密码变量无效。
//以下代码是什么是-supposed - 当这个代码块执行时会发生,但我们现在正在调试,所以我们提供输出来看看代码中会发生什么。
// header(Location:http://www.fantasycs.com/app.php);
} elseif($ arg2 ==loginurl){
echo它工作!登录URL将显示在这里。
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'); //运行test.php和$ output可以正确地捕获输出。别担心。

echo $ output;


$ output = shell_exec('php test.php Thisworks'); //这样可以无限期地运行,就我所知,我无法看到发生了什么,因为没有产生输出。它只是继续尝试加载页面永远。没有输出似乎被捕获。离奇。

echo $ output;


$ output = shell_exec('php otherevent.php'); //这与您尝试使用php命令运行test.php的问题相同。它停了

echo $ output;


$ output = shell_exec('php-cli otherevent.php Thisworks'); //这在otherevent.php中的使用行产生错误。它说这些行上有一个语法错误。这从来没有发生过或出现过;这些线,简单地说,他们没有任何错误。他们以前跑得很好,从未改变过。作为错误的结果,脚本被中止,并且没有捕获输出。

echo $ output;

?>

在这个例子中我想要的是app.php可以执行otherevent.php,get登录URL(或其输出的任何测试输出),并将登录URL打印到屏幕。一个人怎么做显然,它并不像看起来那么简单。

解决方案

你的错误使用php-cli围绕'use'关键字使我相信你有一个旧版本的PHP ..但是为了解决你的问题的一部分,我建议不要使用shell_exec来运行其他PHP代码。如果你需要捕获脚本的输出,你可以使用这样的输出缓冲区:

 <?php 

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

//开始捕获缓冲区中的输出
ob_start();
包含'otherevent.php';
//获取数据并关闭缓冲区
$ output = ob_get_clean();
echo $ output;

?>

如果要立即回送输出,输出缓冲区就没有意义。 p>

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


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.

解决方案

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+.

这篇关于PHP - 执行web脚本作为命令行脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆