如何通过电子邮件发送PHP中的整个请求

时间:2018-06-10 20:46:16

标签: php

我正在尝试创建一个通过电子邮件发送有关请求的所有可能信息的php。目前我使用的是:

电子邮件请求php:

<?php

$remoteIp = $_SERVER['REMOTE_ADDR'];
$remoteHost = $_SERVER['HTTP_HOST'];
$remoteRef = $_SERVER['HTTP_REFERER'];
$remoteUrl = $_SERVER['REQUEST_URI'];
$userAgent = $_SERVER['HTTP_USER_AGENT'];
$yourEmailAddress = "user@mail.com";
$emailSubject = "New request from: ".$remoteIp;

$emailContent = "The URL Request was made to: $remoteUrl 
The request REFERER was: $remoteRef
The IP was $remoteIp
The User Agent was: $userAgent";

// send the message
mail($yourEmailAddress, $emailSubject, $emailContent);
?>

我想将Entire http request(GET或POST)添加到已发送的电子邮件中,以便$emailContent看起来像这样:

    $emailContent = "The URL Request was made to: $remoteUrl 
    The request REFERER was: $remoteRef
    The IP was $remoteIp
    The User Agent was: $userAgent"
    The Full request was: 
    $fullrequest";

环顾四周,我发现这个https://gist.github.com/magnetikonline/650e30e485c0f91f2f40允许你创建文件,但我不知道如何将它与我的PHP放在一起,所以它向我发送了一封包含请求的电子邮件。 (不需要创建文件)

这个PHP我喜欢整合到我的电子邮件请求php

<?php
// https://gist.github.com/magnetikonline/650e30e485c0f91f2f40
class DumpHTTPRequestToFile {
    public function execute($targetFile) {
        $data = sprintf(
            "%s %s %s\n\nHTTP headers:\n",
            $_SERVER['REQUEST_METHOD'],
            $_SERVER['REQUEST_URI'],
            $_SERVER['SERVER_PROTOCOL']
        );
        foreach ($this->getHeaderList() as $name => $value) {
            $data .= $name . ': ' . $value . "\n";
        }
        $data .= "\nRequest body:\n";
        file_put_contents(
            $targetFile,
            $data . file_get_contents('php://input') . "\n"
        );
        echo("Done!\n\n");
    }
    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');

任何人都可以帮助我将class DumpHTTPRequestToFile添加到$fullrequest吗?

1 个答案:

答案 0 :(得分:2)

这样的东西
<?php

$message="The following request was made:\n";

foreach($_REQUEST as $k=>$v){

$message.=$k." : ".$v."\n\n";

}

mail($to_address, $subject, $message, $headers);

?>

$_REQUEST替换为您选择的超级全局。如果$_SERVER没有你想要的全部内容,或者在多个超全球($_REQUEST等)上运行类似的循环。