简体   繁体   English

PHP 可以读取传入 $_REQUEST 或 $_POST 变量的侦听器脚本

[英]PHP listener script that can read incoming $_REQUEST or $_POST variables

I am trying to write a PHP script that listens for incoming $_REQUEST or $_POST variables that are sent by a web application as part of a 2-way communication.我正在尝试编写一个 PHP 脚本来侦听作为双向通信的一部分由 web 应用程序发送的传入 $_REQUEST 或 $_POST 变量。

HTTP GET calls will be made to the web app in format similar to this . HTTP GET 调用将以与此类似的格式对 web 应用程序进行。

The WEBAPP will then send a POST response to my listener script (http://TRAVISNG.com/listener.php) and so I was wondering if my php script could parse it without me executing the php script manually?然后,WEBAPP 将向我的侦听器脚本 (http://TRAVISNG.com/listener.php) 发送一个 POST 响应,所以我想知道我的 php 脚本是否可以在我不手动执行 php 脚本的情况下解析它?

Note that I am not referring to writing a script that listens for network requests on a socket.请注意,我不是指编写一个在套接字上侦听网络请求的脚本。

Basically, I want to parse the POST data sent by the web app and write it out to a log file.基本上,我想解析 web 应用程序发送的 POST 数据并将其写入日志文件。 Therefore, every time I run my listener script it will read the log file and print out all the POST responses sent to my listener script.因此,每次我运行我的侦听器脚本时,它都会读取日志文件并打印出所有发送到我的侦听器脚本的 POST 响应。

Here's some of the code that I've written:这是我编写的一些代码:

<?php

    // Read incoming POST request
    if (!empty($_POST)){
        $params = join(" ", $_POST);
        //print_r($params);
        echo "|$params|";
    }

    // Print params & timestamp to file called listenerLog.txt
    $logFile = "http://travisng.com/listenerLog.txt";

    $fileHandle = fopen($logFile, 'a') or die("Unable to open the listenerLog.txt.");
    fwrite($fileHandle, $params);
    fclose($fileHandle);

    $output = file_get_contents($logFile);

    // Print listenerLog.txt
    //echo $output;

?>

Cheers,干杯,

Travis特拉维斯

To overcome this issue, I decided to create a CGI listener script and process REQUEST parameters from there.为了克服这个问题,我决定创建一个 CGI 侦听器脚本并从那里处理 REQUEST 参数。 The CGI script was invoked whenever a GET/POST request came through and the requests were written out to a log file.每当 GET/POST 请求通过并将请求写入日志文件时,就会调用 CGI 脚本。

Based on your example call/url they will be in the $_GET global.根据您的示例调用/url,它们将位于 $_GET 全局中。

So try:所以试试:

// Read incoming POST request
if (!empty($_GET)){
    $params = join(" ", $_GET);
    //print_r($params);
    echo "|$params|";
}

You could use $_REQUEST - this will contain items $_GET and $_POST and (depending on version or php.ini config $_COOKIE too).您可以使用$_REQUEST - 这将包含项目$_GET$_POST和(也取决于版本或 php.ini 配置$_COOKIE )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM