1

I have two project on same server. I want some data form on my website so I am using file_get_contents; most of the time I get the 500 internal error

I checked that my url fopen is on using phpinfo().

halfer
  • 19,824
  • 17
  • 99
  • 186
user441423
  • 31
  • 1
  • 2
  • 7
  • Please provide more tags, such as php and so on, or describe your question in more details, so that it will be known what language you use, what server you use, what are you trying to do, etc. – Illarion Kovalchuk Dec 13 '10 at 09:47
  • possible duplicate of [Why I'm getting 500 error when using file\_get\_contents(), but works in a browser?](http://stackoverflow.com/questions/10524748/why-im-getting-500-error-when-using-file-get-contents-but-works-in-a-browser) – kenorb Mar 16 '15 at 17:44

1 Answers1

4

With default settings, file_get_content() doesn't work behind a proxy or it cannot handle timeouts. It's normally recommended to read local files.

Therefore use cURL instead.

Below function could be used for the job:

function http_request($uri, $time_out = 10, $headers = 0)
{
    // Initializing
    $ch = curl_init();

    // Set URI
    curl_setopt($ch, CURLOPT_URL, trim($uri));

    curl_setopt($ch, CURLOPT_HEADER, $headers);

    // 1 - if output is not needed on the browser
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

    // Time-out in seconds
    curl_setopt($ch, CURLOPT_TIMEOUT, $time_out);

    // Executing
    $result = curl_exec($ch);

    // Closing the channel
    curl_close($ch);

    return $result;
}

Let me know whether your're using Linux or Windows to give you cURL installation tips

Amil Waduwawara
  • 1,632
  • 1
  • 16
  • 14
  • i had install cURL on server but i have one question that how to get data proper some div or some value using above function.? – user441423 Dec 13 '10 at 10:40
  • It must be done with PHP scripting. For e.g., in your PHP script, you may have ...
    ....
    – Amil Waduwawara Dec 13 '10 at 10:53
  • `file_get_contents()` indeed *can* handle timeouts by specifying context, however I found that it doesn't like when server returns `error 500`, so breaking out the trusty old curl is the answer – Yarek T Nov 28 '12 at 11:27
  • 1
    @YarekT, cURL is probably your best option but FYI you can still configure file_get_contents to return the response upon receieving an HTTP error. See: http://stackoverflow.com/a/3710526/120497 – djskinner Mar 26 '13 at 12:32