1

If i run this code in PHP with a wrong proxy I get 500 internal server error. How to handle it and continue execution?

$opts = array(
          'http'=>array(
            'method'=>"GET",
            'proxy' => 'tcp://100.100.100.100:80' //a wrong proxy
          )
);

$context = stream_context_create($opts);

$file = file_get_contents('http://ifconfig.me/ip', false, $context);
4
  • What do you mean by "handle it"? Oct 8, 2014 at 15:11
  • the machine running this code issues the 500, or the proxy/url you're trying to hit issues the 500?
    – Marc B
    Oct 8, 2014 at 15:11
  • The machine running this code issues the 500. I don't want it stop next code.
    – Davide
    Oct 8, 2014 at 15:16
  • @jay-blanchard I mean that it must ignore 500 error caused by file_get_contents and continue with eventual next code.
    – Davide
    Oct 8, 2014 at 15:29

1 Answer 1

3

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.

1
  • This cURL approach will show cURL errors, like error 58, CURLE_SSL_CERTPROBLEM. It will not handle HTTP 500 errors. For those, check out this answer: stackoverflow.com/a/408416/1487413. Also, this code sticks the HTTP headers onto the data for some reason, using CURLOPT_HEADER. This isn't very handy. Per the cURL manual "it is not possible to accurately separate them again without detailed knowledge about the protocol in use." Mar 31, 2015 at 16:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.