代码之家  ›  专栏  ›  技术社区  ›  Ajnabi

接收请求中的xml文件。代码有什么问题?

  •  1
  • Ajnabi  · 技术社区  · 7 年前

    有谁能帮我检测以下代码错误吗?它在上显示错误 $headerList = []; (new DumpHTTPRequestToFile)->execute('./dumprequest.txt'); 请帮助我接收xml请求。

        <?php
    
    class DumpHTTPRequestToFile {
    
        public function execute($targetFile) {
    $data="";
            foreach ($this->getHeaderList() as $name => $value) {
                //$data .= $value . "\n";
            }
    
            $data .= "";
    
            file_put_contents(
                $targetFile,
                $data . file_get_contents('php://input') . "\n"
            );
    
            echo("Done");
        }
    
        private function getHeaderList() {
    
            $headerList = [];
            foreach ($_SERVER as $name => $value) {
                if (preg_match('/^HTTP_/',$name)) {
                    // convert HTTP_HEADER_NAME to Header-Name
                    $name = strtr(substr($name,5),'_',' ');
                    $name = ucwords(strtolower($name));
                    $name = strtr($name,' ','-');
    
                    // add to list
                    $headerList[$name] = $value;
                }
            }
    
            return $headerList;
        }
    }
    (new DumpHTTPRequestToFile)->execute('./dumprequest.txt');
    ?>
    

    enter image description here

    1 回复  |  直到 7 年前
        1
  •  0
  •   cmbuckley    7 年前

    这看起来您正在使用过时的PHP版本,无论是作为Dreamweavers处理程序还是在您的服务器上。这些是PHP5.3中的语法错误,但在PHP5.4中是允许的,称为短数组语法和实例化速记: http://php.net/manual/en/migration54.new-features.php

    您可以使用 $headerList = array() 向后兼容PHP5.3及以下版本。

    同样地,你 can’t use the instantiation shorthand PHP5.4之前的版本;您需要:

    $dump = new DumpHTTPRequestToFile();
    $dump->execute(...);
    

    鉴于PHP5.3已经过时,我强烈建议更新您的PHP版本,而不是通过拖网式的代码使其具有互操作性。