Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

$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?

like image 868
bdev Avatar asked May 09 '12 22:05

bdev


People also ask

How do I fix http error 500 in Linux?

A permission issue. When the webserver has no permissions to access the site files, it may throw an HTTP 500 error. The solution to this issue is to change the website file's permissions recursively .

How can I get 500 error in php?

You can solve the PHP error 500 by temporarily deleting the misconfigured htaccess file. The 500 error can go away by increasing the values set for the max_execution_time and the memory_limit settings. Setting the file permission to 644 or 755 can help in resolving the 500 internal server error.

How to use file_ get_ contents function in php?

The file_get_contents() reads a file into a string. This function is the preferred way to read the contents of a file into a string. It will use memory mapping techniques, if this is supported by the server, to enhance performance.


2 Answers

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?

like image 164
blang Avatar answered Sep 27 '22 20:09

blang


I had to enter more data into the header:

$opts = array('http' => array(     'method' => "GET",     'header' => "User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Firefox/24.0\r\n"     . "Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n"     . "Accept-Encoding:gzip, deflate\r\n"     . "Accept-Language:cs,en-us;q=0.7,en;q=0.3\r\n"     . "Connection:keep-alive\r\n"     . "Host:your.domain.com\r\n"     )); $context = stream_context_create($opts); $html = file_get_contents($sap_url, FALSE, $context); 
like image 36
Racky Avatar answered Sep 27 '22 20:09

Racky