Need Response Body of Http 500 with File_Get_Contents (Php)

Need response body of HTTP 500 with file_get_contents (PHP)

You might consider nusoap.

For your question though, it works if you use ignore_errors.

$context = stream_context_create(array(
'http' => array(
'ignore_errors' => true
)
));

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

Fetch the response body on failure using file_get_contents

$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
)
));

How can I handle the warning of file_get_contents() function in PHP?

Step 1: check the return code: if($content === FALSE) { // handle error here... }

Step 2: suppress the warning by putting an error control operator (i.e. @) in front of the call to file_get_contents():
$content = @file_get_contents($site);

How to post data in PHP using file_get_contents?

Sending an HTTP POST request using file_get_contents is not that hard, actually : as you guessed, you have to use the $context parameter.


There's an example given in the PHP manual, at this page : HTTP context options (quoting) :

$postdata = http_build_query(
array(
'var1' => 'some content',
'var2' => 'doh'
)
);

$opts = array('http' =>
array(
'method' => 'POST',
'header' => 'Content-Type: application/x-www-form-urlencoded',
'content' => $postdata
)
);

$context = stream_context_create($opts);

$result = file_get_contents('http://example.com/submit.php', false, $context);

Basically, you have to create a stream, with the right options (there is a full list on that page), and use it as the third parameter to file_get_contents -- nothing more ;-)


As a sidenote : generally speaking, to send HTTP POST requests, we tend to use curl, which provides a lot of options an all -- but streams are one of the nice things of PHP that nobody knows about... too bad...

file_get_contents throws 400 Bad Request error PHP

You might want to try using curl to retrieve the data instead of file_get_contents. curl has better support for error handling:

// make request
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://api.twitter.com/1/statuses/user_timeline/User.json");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);

// convert response
$output = json_decode($output);

// handle error; error output
if(curl_getinfo($ch, CURLINFO_HTTP_CODE) !== 200) {

var_dump($output);
}

curl_close($ch);

This may give you a better idea why you're receiving the error. A common error is hitting the rate limit on your server.

SimpleHTMLDom returns 500 error on printing output

The error message indicates ModSecurity is complaining about Response body being too large. This does not mean there is something wrong with loading HTML using Simple HTML DOM library, it is about the size of response generated by your code (print_r or var_dump parts). I guess this is because the structure of the HTML you're loading requires lots of nested objects to represent DOM tree, so when you try to output the full structure using print_r or var_dump the response becomes too large.

You can verify that the HTML is loaded and parsed by simply printing the plain HTML of the page (use print instead of print_r to print simple_html_dom object):

$html = file_get_html("http://www.google.com");

print($html);

and you will see the HTML is retrieved correctly, and you can work with $html object to manipulate DOM the way you expect to work with simple_html_dom objects.

If you want to change the output limit for ModSecurity so you can generate larger responses, please have a look at this question: Mod Security response/request body size?



Related Topics



Leave a reply



Submit