Reg
Reg

Reputation: 87

PHP not seeing curl post

I wrote a very simple script to just see what a server was sending like this:

<?php
$html1 = <<<EOT
<!doctype html>
<html lang=en>
  <head>
    <meta charset = 'utf-8'>
    <title>TradingView Test</title>
  </head>

  <body>
    <pre>
EOT;

$html2 = <<<EOT
    </pre>
  </body>
</html>
EOT;

date_default_timezone_set('America/Phoenix');

$file = date('Y/m/d h:i:sa') . "\n";

$url = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http")
       . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";

$file .= "\nURL: $url\n\n";
$html = $file;

if (!empty($_REQUEST)) {
  $r = "\$_REQUEST:\n" . var_export($_REQUEST, true) . "\n\n";
  $file .= $r;
  $html .= htmlspecialchars($r);
}

$headers = apache_request_headers();
if (!empty($headers)) {
  $h = "HEADERS:\n" . var_export($headers, true) . "\n\n";
  $file .= $h;
  $html .= htmlspecialchars($h);
}

//   if (!empty($_SERVER)) {
//     $s = "\$_SERVER:\n" . var_export($_SERVER, true) . "\n\n";
//     $file .= $s;
//     $html .= htmlspecialchars($s);
//   }

$file .= str_repeat('-', 40) . "\n";

file_put_contents('./get-post.log', $file, FILE_APPEND);
# echo $html1 . $html . $html2;

?>

In the example for what the server sends they had this example:

#!/bin/sh
curl -H 'Content-Type: text/plain; charset=utf-8' -d '1111111111,data,more-data' -X POST https://www.testtest.com/

I thought with $_REQUEST I would see all that was sent but I don't see anything and what the script outputs is:

2022/03/30 06:15:22pm

URL: https://www.testtest.com/

HEADERS:
array (
  'X-Https' => '1',
  'Connection' => 'close',
  'Accept-Encoding' => 'gzip',
  'Content-Type' => 'text/plain; charset=utf-8',
  'Content-Length' => '45',
  'User-Agent' => 'Go-http-client/1.1',
  'Host' => 'www.testtest.com',
)

I suspect the issue is that they are not sending the data as a proper POST by naming a field and I can't change how the data is being sent because it's not my server.

Does anyone know what I can look at in PHP to see the data being sent? Or perhaps this is an Apache problem as PHP gets its data through Apache and maybe in that format it's just not getting passed through?

You will note that I have displaying of $_SERVER commented out because even when it wasn't it didn't give me anything that helped.

Upvotes: 0

Views: 103

Answers (1)

Reg
Reg

Reputation: 87

Found it. PHP won't fill $_POST without named arguments so to get the string the way it's being sent this needs to be used:

<?php
$str = file_get_contents('php://input');

Upvotes: 1

Related Questions