Maik Silber
Maik Silber

Reputation: 71

PHP file_get_contents error 503

i get the error

"503 Service Temporarily Unavailable"

for my call with

$url = "https://www.okex.com/api/v1/ticker.do?symbol=ltc_btc";
  $page = json_decode(file_get_contents($url),true);
  var_dump($page);

PHP file_get_contents

function but when i write the url directly into the browser i can see the page, do they block only the file_get_contents functions or how does this work? Because if they block my ip i could also not visit the site with my browser or?

And this is a call to APi server which gives me json back.

Upvotes: 2

Views: 3976

Answers (1)

Erik Kalkoken
Erik Kalkoken

Reputation: 32747

Its more likely that your webpage has a redirect and file_get_contents() can not handle that, but a browser can.

So the solution is to use curl instead, which is able to handle these kind of situations (e.g. with CURLOPT_FOLLOWLOCATION option).

See also this questions:

Here is a snippet that should work as an easy replacement (based on example from official doc):

function curl_file_get_contents($url)
{
    $session = curl_init();
    curl_setopt($session, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($session, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($session, CURLOPT_URL, $url);

    $contents = curl_exec($session);
    curl_close($session);
    
    return $contents;
}

Upvotes: 7

Related Questions