user2244804
user2244804

Reputation:

HTTP request failed! HTTP/1.1 503 Service Temporarily Unavailable

I am using function file_get_contents to get contents from web page. Some website working well but most of them give me this error

failed to open stream: HTTP request failed! HTTP/1.1 503 Service Temporarily Unavailable

Here is my simple code

echo file_get_contents("url");

When i run this url in browser it works fine.what can be the issue?

Upvotes: 7

Views: 23930

Answers (2)

dazzafact
dazzafact

Reputation: 2860

With this script most Websites think you are a Browser:

    <?php
        getWebsite('http://mywebsite.com');

        function getWebsite($url='http://mywebsite.com'){
        
        $ch = curl_init();
        $user_agent='Mozilla/5.0 (Windows NT 6.1; rv:8.0) Gecko/20100101 Firefox/'.rand(8,100).'.0';
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($ch, CURLOPT_AUTOREFERER, false);
        curl_setopt($ch, CURLOPT_VERBOSE, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);

        curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_SSLVERSION,CURL_SSLVERSION_DEFAULT);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        $webcontent= curl_exec ($ch);
        $error = curl_error($ch); 
        curl_close ($ch);
        return  $webcontent;

        }
    ?>

Upvotes: 2

Hiren Soni
Hiren Soni

Reputation: 574

503 means the functions are working and you're getting a response from the remote server denying you. If you ever tried to cURL google results the same thing happens, because they can detect the user-agent used by file_get_contents and cURL and as a result block those user agents. It's also possible that the server you're accessing from also has it's IP address blackballed for such practices.

Mainly three common reasons why the commands wouldn't work just like the browser in a remote situation.

1) The default USER-AGENT has been blocked.
2) Your server's IP block has been blocked.
3) Remote host has a proxy detection. 

Upvotes: 7

Related Questions