$_POST values not available

Hi,

Being completely new to PHP, I’m finding the following quite difficult to resolve.

I have 3 docker containers;

Container-1; just running Ubuntu with cURL installed.
Container-2 (named as ‘phptestnew’); with the PHP script I’m trying to get to work.
Container-3 (named as ’httpdebug’); with ‘http-https-echo’ from GitHub which [quote]” Docker image which echoes various HTTP request properties back to client, as well as in docker logs.”

From Container-1 I run the following command;

curl -d ‘[{“job”:“TodaysJOB”}]’ -X POST -H “Content-Type: application/json” http://phptestnew

From Container-2 / phptestnew the code is;

<?php
$_POST = json_decode(file_get_contents('php://input',true));

$data = http_build_query(
    array(
        'job' => $_POST['job'],
    )
);

$headers = array('http' =>
    array(
        'method'  => 'POST',
        'header'  => 'Content-Type: application/json',
        'content' => $data
    )
);

$context  = stream_context_create($headers);
$result = file_get_contents('http://httpdebug', false, $context);
?>

From Container-3 / httpdebug, the output is;

{ path: '/',
  headers:
   { host: 'httpdebug',
     connection: 'close',
     'content-type': 'application/json' },
  method: 'POST',
  body: '',
  cookies: undefined,
  fresh: false,
  hostname: 'httpdebug',
  ip: 'xx.xx.xx.xx,
  ips: [],
  protocol: 'http',
  query: {},
  subdomains: [],
  xhr: false,
  os: { hostname: 'blah' } }
xx.xx.xx.xx - - [19/Oct/2018:08:07:21 +0000] "POST / HTTP/1.0" 200 368 "-" "-"

The issue as you can see, is that I am not getting anything in the body of the output. I suspect it’s because $_POST is an array, within an array but due to being new to PHP, I don’t know how to fix this.

Any help, is much appreciated.

Thanks

Somehow my original put all my PHP code on one line. Please see below what it should look like;

<?php
$_POST = json_decode(file_get_contents('php://input',true));

$data = http_build_query(
    array(
        'job' => $_POST['job'],
    )
);

$headers = array('http' =>
    array(
        'method'  => 'POST',
        'header'  => 'Content-Type: application/json',
        'content' => $data
    )
);

$context  = stream_context_create($headers);
$result = file_get_contents('http://httpdebug', false, $context);
?>

The code I’m having no luck with (again) -

php-code-issue

(I know nothing about Docker but) Are your containers setup to know the correct IP address for those custom local domains?

Sponsor our Newsletter | Privacy Policy | Terms of Service