来自php顶级php脚本的返回值被吞下

时间:2013-09-28 07:33:39

标签: php return-value router

我正在搜索应用程序中的一个错误,我可以缩小到一个必需的返回值,而不会因为对http服务器的http请求而返回。该请求是一个上传文件的POST,返回值应该是一个字符串形式的确认,以后可以用来访问该文件。

服务器正在运行一个php脚本来处理上传的文件,一切正常,但我没有在返回有效负载中获得所需的字符串。相反,有效载荷是空的。

问题是由我在现有应用程序之上实现的额外请求路由器引起的。路由器是一个简单的顶级脚本(index.php),由于服务器级别的一些基于重写的路由,http服务器(apache2)直接调用它。

这是(简化的和简化的)路由器脚本。对于测试用例,使用以下值调用它:service=put&method=logo。正如预期的那样,它包含文件...../service/pi.php,它执行一些处理并正确终止返回所需的字符串:

<?php

require_once \CONFIG\PATH\BACKEND.'/include/exception.php';

try {

  $service = isset($_GET['service']) ? $_GET['service'] : (isset($_POST['service']) ? $_POST['service'] : NULL);
  $method  = isset($_GET['method'])  ? $_GET['method']  : (isset($_POST['method'])  ? $_POST['method']  : NULL);

  switch ($service) {
    case 'amf':         return include \CONFIG\PATH\BACKEND.'/service/amf.php';
    case 'bof':         return include \CONFIG\PATH\BACKEND.'/service/bof.php';
    case 'put':
      switch ($method) {
        case 'deco':
        case 'img':
        case 'logo':
        case 'plan':    $ret = include \CONFIG\PATH\BACKEND.'/service/pi.php';
                        error_log('+++ '.print_r($ret,true));
                        error_log('*** '.print_r(debug_backtrace(),true));
                        echo "honkitonk";
                        return $ret;
        default:        throw new \PDExceptionLogicalError('Invalid method');
      } // switch method
    case 'get':
      switch ($method) {
        case 'creport': return include \CONFIG\PATH\BACKEND.'/service/gcr.php';
        case 'deco':    return include \CONFIG\PATH\BACKEND.'/service/gdi.php';
        case 'img':     return include \CONFIG\PATH\BACKEND.'/service/gi.php';
        case 'report':  return include \CONFIG\PATH\BACKEND.'/service/gr.php';
        default:        throw new \PDExceptionLogicalError('Invalid method');
      } // switch method
    default:            throw new \PDExceptionLogicalError('Invalid service');
  } // switch service

} catch (PDException $e) { return $e->getPDError(); }

?>

生成日志文件中的输出仅用于测试目的。通常,两个switch语句的分支只会返回包含的脚本jsut的结果,就像在其他分支中完成一样。

发送回客户端的结果有效负载是有效的,包含测试字符串“honkitonk”,但不是 $ret中的返回值。脚本执行继续在switch语句后面或任何东西,脚本以可疑内部交换机分支内的return $ret;终止。

日志文件中的输出显示:

[Sat Sep 28 09:17:39 2013] [error] [client 127.0.0.1] +++ /backend/get?method=img&id=26
[Sat Sep 28 09:17:39 2013] [error] [client 127.0.0.1] *** Array\n(\n)\n

日志文件中的空数组(debug_backtrace()调用的结果证明脚本位于顶层。顶级脚本中的返回应该导致返回值被移交给服务器回复但是这不是这种情况。我根本就不明白为什么!

我还使用脚本周围的全局输出缓冲进行检查并检查:结果相同:“honkitonk”,但不是$ret中所需的字符串值。

我在这里缺少什么?

1 个答案:

答案 0 :(得分:-1)

只需快速猜测,在你的包含语句中添加(和):

返回(包括'file');

包含的文件是否包含return语句,还是只打印您想要的内容?