Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fetch the response body on failure using file_get_contents

The documentation of file_get_contents says

 On failure, file_get_contents() will return FALSE.

I am integrating with a system which returns error messages in the response and sets the status code to "50x"

Is there a way, I can still fetch the response content ?

like image 323
Prakash Raman Avatar asked Oct 20 '25 05:10

Prakash Raman


1 Answers

$curl = curl_init('http://example.net');
curl_setopt( $curl, CURLOPT_RETURNTRANSFER,1);
$result = curl_exec($curl);
curl_close($curl);

however this may not satisfy your needs, as it requires curl

you may also ignore errors, to still use file_get_contents

$contents = file_get_contents($url, FALSE, stream_context_create(array(
    'http' => array(
        'ignore_errors' => true
     )
));
like image 192
Artek Wisniewski Avatar answered Oct 21 '25 21:10

Artek Wisniewski



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!