2

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.

Maik Silber
  • 71
  • 1
  • 7
  • it says **"Checking your browser before accessing okex.com"** for me; so the server is denying your `file_get_contents` . Therefore use `cURL` instead. – Martin Feb 04 '18 at 10:59
  • but why does the server allow me to call the url when i write it directly in my browser but with using the PHP file_get_contents function it does not work? Are you sure with cURL the server will not block me again? And using cURL can give also performance issue or not? Because file_get_contents use less power? – Maik Silber Feb 04 '18 at 11:03
  • No, its the other way around: `curl` is usually faster than `file_get_contents` – Erik Kalkoken Feb 04 '18 at 11:05

1 Answers1

5

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_get_file_contents($URL)
    {
        $c = curl_init();
        curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($c, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($c, CURLOPT_URL, $URL);
        $contents = curl_exec($c);
        curl_close($c);

        if ($contents) return $contents;
        else return FALSE;
    }
Erik Kalkoken
  • 27,607
  • 7
  • 70
  • 97
  • ok thanks. Do you know if there is a performance difference between cURL or file_get_contents using? Because i have in a mind that i did read somethink like that which says file_get_contents use less system power. – Maik Silber Feb 04 '18 at 11:17
  • no, as I said above. curl is usually faster and more stable than file_get_contents. Its the standard approach in PHP to make http request. The only benefit of file_get_contents() is that its is easier to use, but I never heard of anyone claiming that it uses less resources. – Erik Kalkoken Feb 04 '18 at 11:20
  • maybe i have understand it wrong, what i did read is this comment "file_get_contents() is the preferred way to read the contents of a file into a string. It will use memory mapping techniques if supported by your OS to enhance performance." – Maik Silber Feb 04 '18 at 11:23
  • yes, but this is comment only applies to files on your local machine, not remote files. As it says "supported by your OS" – Erik Kalkoken Feb 04 '18 at 11:27
  • alright thanks i gone exchange all http calls with cURL – Maik Silber Feb 04 '18 at 11:33
  • thanks when my reputation points become more then 15 i give you a upvote for sure – Maik Silber Feb 04 '18 at 12:04
  • awesome. if my answer solved your question, please mark it complete. ty! – Erik Kalkoken Feb 04 '18 at 12:06
  • 1
    i will do, as soon as i had the time to exchange all my codes then i will see the results if it work with cURL this time then i mark this also as completed. I did not mark here any of my questions as complete before, but anyway i will find that button. – Maik Silber Feb 04 '18 at 12:30