Why I'm getting 500 error when using file_get_contents(), but works in a browser?

file-get-contents, php

Solution

Try this workaround:

$opts = array('http'=>array('header' => "User-Agent:MyAgent/1.0\r\n"));
$context = stream_context_create($opts);
$header = file_get_contents('https://www.example.com',false,$context);

If this doesn't work, maybe you cant read from https?

Problem

``` $html = file_get_contents("https://www.[URL].com"); echo $html; ``` produces this in the error logs: PHP Warning: file_get_contents(https://www.[URL].com) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.1 500 Internal Server Error in /Applications/MAMP/htdocs/test.php on line 13"; However, the site works fine in a browser. I tried using cURL as well. I don't get any errors in the log file, but `$html` now echoes: Server Error in '/' Application. Object reference not set to an instance of an object. ...some more debugging info Any ideas how to work around this?

Original source