1

I need to interface with a REST webserver. When I issue a request, it responds (correctly) with HTTP response code 201 (Created), and the relevant information (JSON object) in the body.

I can verify it using cURL.

I'm trying to get the body it via PHP's file_get_contents function, specifying a correct context object, with the following code:

$context = stream_context_create(array(
     'http' => array(
       'method' => 'POST',
       'header' => $header,
       'timeout' => 10.0,
       'content' => $body,
            'ignore_errors' => true
      )
    )
);

$answer = file_get_contents($url, false, $context);

However the function always returns a false value and I cannot get the returned body.

What can I do? (I'd rather not call cURL from PHP)

ADyson
  • 57,178
  • 14
  • 51
  • 63
  • 2
    php has curl methods itself. no need to call curl from shell. https://stackoverflow.com/questions/17134546/curl-getting-the-html-response-body. – Taha Paksu Nov 08 '17 at 17:00
  • 1
    file_get_contents should return you the body. Perhaps your request is failing for some reason. – ADyson Nov 08 '17 at 17:33
  • 1
    Even better, [Guzzle](https://packagist.org/packages/guzzlehttp/guzzle) is a very good wrapper around curl. – Sammitch Nov 08 '17 at 18:24
  • You can advice him to use curl or Guzzle but it does not answer why file_get_contents does not work as expected (and it should work!). – Daniel W. Nov 09 '17 at 16:19
  • perhaps i've found it the http context options provide the clause ignore_errors – Maurizio Ferreira Nov 10 '17 at 08:03
  • Perhaps I've found it. The http_context_options provide the clause 'ignore_erros', however this is valid starting from http version 5.2.10 i'm currently using version 5.1.2 ... – Maurizio Ferreira Nov 10 '17 at 08:09

1 Answers1

0

OK, I've found it. The cause was an old php version, that did'nt handle the 'ignore_errors' parameter. Switching to a more recent version of php, now it works.