user441423
user441423

Reputation: 31

500 Server error file_get_contents

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().

Upvotes: 1

Views: 5210

Answers (1)

Amil Waduwawara
Amil Waduwawara

Reputation: 1642

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

Upvotes: 4

Related Questions