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.

3
  • 1
    it says "Checking your browser before accessing okex.com" for me; so the server is denying your file_get_contents . Therefore use cURL instead. Commented Feb 4, 2018 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? Commented Feb 4, 2018 at 11:03
  • No, its the other way around: curl is usually faster than file_get_contents Commented Feb 4, 2018 at 11:05

1 Answer 1

7

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;
}
Sign up to request clarification or add additional context in comments.

8 Comments

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.
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.
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."
yes, but this is comment only applies to files on your local machine, not remote files. As it says "supported by your OS"
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.
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.