cnalo Posted December 3, 2018 Share Posted December 3, 2018 Hello, I am a newbie regarding PHP and have done mostly only front-end web design for a couple of years and recently started querying DB's and displaying the data, but now this is a new project. 1) I have an external application sending HTTP POST requests to a specific php page on my web server. 2) I would like to store each new POST in a MySQL table and sort the value of all header names in their designated columns and add the current timestamp as well. The headers included are "X", "Z", "Player", "UUID" my DB has the columns 'X', 'Z', 'Player', UUID', 'Timestamp'. I've tested code snippets as the ones below, but fail to incorporate them in the bigger picture of what I'm trying to achieve. This one saves the data into a CSV, but it rewrites the file, appending a new line would already be a big step forward. <?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.csv'); This snippet also seems to return the headers and their values but I don't know how to efficiently write and append it to a TXT or CSV, or preferably, directly into a MySQL database. <?php foreach (getallheaders() as $name => $value) { echo "$name: $value"; echo "<br>"; } ?> Any help towards storing the header values of HTTP POST requests into a database would be greatly appreciated! Also, storing the body data in a separate column is optional, but nice to have. Quote Link to comment Share on other sites More sharing options...
gw1500se Posted December 3, 2018 Share Posted December 3, 2018 This article should get you going. Quote Link to comment Share on other sites More sharing options...
requinix Posted December 3, 2018 Share Posted December 3, 2018 I sure hope the headers aren't actually "X", "Y", "Player", and "UUID". Are you saying that $_SERVER has entries like "HTTP_X" and "HTTP_PLAYER"? Because if so then whoever set this up needs to be re-educated about HTTP. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.