Why I'M Getting 500 Error When Using File_Get_Contents(), But Works in a Browser

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

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?

PHP file_get_contents 500 Internal Server error in php

The problem is in your User-Agent header. This worked for me:

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

How to handle 500 internal server error when using file_get_contents with a wrong proxy?

I assume you mean two things when you say “handle”:

  1. If the script connects to a “wrong” proxy, it will wait for a long time to establish the connection, until it times out. The script should set a lower timeout, so users don't wait forever.
  2. If an error occures during accessing the external ressource, don't die or show ugly messages. Instead, pretend everything's cool.

As for 1) The timeout of a remote connection is defined in PHP's default_socket_timeout setting and is by default 60 seconds. You can/should set a much lower timeout for your own calls:

$opts = array(
'http'=>array(
'timeout'=> 2, // timeout of 2 seconds
'proxy' => 'tcp://100.100.100.100:80' //a wrong proxy
)
);

As for 2), you would normally use a try/catch block. Unfortunately, file_get_contents() is one of those old PHP functions that don't throw catchable exceptions.

You can supress a possible error message by prefixing the function call with the @ sign:

$file = @file_get_contents('http://ifconfig.me/ip', false, $context);

But then you can't handle any errors at all.

If you want to have at least some error handling, you should use cURL. Unfortunately, it also doesn't throw exceptions. However, if a cURL error occurs, you can read it with curl_errno()/curl_error().

Here's your code implemented with cURL:

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, "http://ifconfig.me/ip");
curl_setopt($ch, CURLOPT_PROXY, 'tcp://100.100.100.100:80');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 2);
curl_setopt($ch, CURLOPT_HEADER, 1);

$data = curl_exec($ch);
$error = curl_errno($ch) ? curl_error($ch) : '';

curl_close($ch);

print_r($error ? $error : $data);

This way, you can decide what you want to do in the case of an error.

file_get_contents failed to open stream: HTTP request failed! HTTP/1.1 500 Internal Server Error in

file_get_contents (read the tip on blue rectangle) can be easily blocked on server side through php.ini avoid using it. When you want to get data from an other site use curl instead. http://php.net/manual/en/book.curl.php, there are plenty of options that you can use with curl, by playing a bit the following code can work with your url.

$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch,CURLOPT_URL,"https://cgi.ebay.com/ws/eBayISAPI.dll?ViewItemRevisionDetails&item=272908801183");
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/525.13 (KHTML, like Gecko) Chrome/0.A.B.C Safari/525.13");
$data = curl_exec($ch);
curl_close($ch);

and by echoing the $data varible you can see the whole page.

echo $data;

you can try parsing the data from the page by utilizing php DOM Methods and convert them to the data type you want (object class, array etc).



Related Topics



Leave a reply



Submit