0

I'm using fopen('http://server/path') and I'd like to get body of http request that php is sending to remote server (for checking & logging).

I've found and read this discussion: PHP file_get_contents() and setting request headers

I know how to set headers I need.

I'm looking for a way to get full http request body that php is sending or just sent to remote server.

$url = 'http://server/path'

$content = json_encode(array(
      'a' => 1
    , 'b' => 2
));

$options = array(
    'http' => array(
        'header' => array(
              'Content-Type: application/json'
            , 'Content-Length: ' . strlen($content)
        )
        , 'method'            => 'POST'
        , 'content'           => $content
    )
);

$context = stream_context_create($options);

$f = fopen($url, 'r', false, $context);

// >>> Here I'd like to get full request that php is just sent to remote server <<<

fclose($f);

For the code provided, I'd like to be able to get this:

POST /path HTTP/1.0
Host: server
Content-Type: application/json
Content-Length: 13

{"a"=1,"b"=2}

Is it possible to do while using fopen?

Thanks.

fifonik
  • 1,556
  • 1
  • 10
  • 18
  • Why not use a proper HTTP request library like [cURL](http://php.net/curl), or an abstraction with a nicer API like [Guzzle](http://docs.guzzlephp.org/en/stable/) (my favorite). – Nathan Feb 07 '19 at 01:54
  • I'd be happy to do so. Unfortunately, I have old php 5.3 installed on the intranet server and no cURL available. Admins refused to do any changes in the server configuration. – fifonik Feb 07 '19 at 02:02
  • Ouch, that sucks. Those admins should be fired. – Nathan Feb 07 '19 at 02:07
  • `stream_get_meta_data($stream)` would be a crude option. Things like the PEAR http class would work without curl, btw. – mario Feb 07 '19 at 02:11
  • Well, it is complicated. There are a lot of old projects that use the old code. Who will fix them in case any issues? – fifonik Feb 07 '19 at 02:11
  • There is no http **request** body in the stream_get_meta_data response. – fifonik Feb 07 '19 at 02:14

0 Answers0