What can cause file_get_contents to give a "timeout" error?

5

I asked this question here , but hardly, for being a problem that seems to be specific, the solution would be something that would be something generic.

But it occurred to me in this question that I asked is that I'm having problems with the file_get_contents function.

When I make a request through it, an error is always returned:

file_get_contents('https://getcomposer.org/versions') 

The return is:

PHP warning: file_get_contents(https://getcomposer.org/versions): failed to open stream: Connection timed out on line 1

However, when I open this url in the browser, everything works perfectly. Furthermore, if I try to make the request in url https://www.google.com the same problem occurs.

But the strange thing is that not all urls are causing this problem.

If I do this, it works:

 file_get_contents('https://pt.stackoverflow.com/')

I thought at first that the problem was with https in urls, but that does not seem to be it.

If I use curl it works, but I do not want to use it, because I depend exclusively on file_get_contents on that occasion.

What can you do with what file_get_contents gives timeout error in some specific urls, which do they normally open by the browser?

Remark : I would not like answers like "using curl as a workaround," since I really need to use file_get_contents for this purpose of the more, an answer with curl would not answer my question).
asked by anonymous 13.01.2017 / 12:26

3 answers

1

There are four possibilities:

  • Firewall or any blocking or proxy software configured on your machine or network (somewhat difficult to state)

  • As of version 5.6, PHP started working entirely differently with SSL ( link ), requiring preconfigured stream to work the certificates better, when needed.

    I understand you said that if you access https://pt.stackoverflow.com it works, but the question is that the certificate might work well for one site and maybe for another not , this is not necessarily your fault, but maybe because there is a problem with the certificate of getcomposer or your machine, anyway you can take the actual proof like this:

    $arrContextOptions = array(
        'ssl' => array(
            'verify_peer' => false,
            'verify_peer_name' => false
        )
    );  
    
    $response = file_get_contents('https://getcomposer.org/versions', false, stream_context_create($arrContextOptions));
    
    echo $response;
    

    If it works, it's a problem with the certificates, maybe from your machine.

  • DNS using your internet provider (I'll call only ISP to get easier) or the ISP itself, yes this is complicated, for example in the company where I work the ISP failed in some website, even the site of the company fails or is too slow to open, but the moment I use 4G the site loads normally, this can not be solved locally, until today I have problems where I work. In short, it may be that the composer server conflicts / fails just with your ISP.

  • Of course, the problem may actually be the secure connection certificates that just fail with the composer server, I've even seen, I've had similar problems with the npm

    What you can do to fix HTTPS

    You can download this link , you can use wget

    $ wget http://curl.haxx.se/ca/cacert.pem
    

    And then point at PHP.INI as well (available since php5.6):

    openssl.cafile=/path/cacert.pem
    

    Restart Apache (if it's apache).

      

    Very Important Note

         

    In Linux distributions, usually php.ini for CLI is different, composer runs using CLI , usually the CLI php.ini is in the folder:

    /etc/php/5.6/cli/php.ini
    
         

    Then you will have to edit both /etc/php/5.6/cli/php.ini and php.ini used by Apache and both add openssl.cafile=/path/cacert.pem

         

    Apache should be something like /etc/php5/apache2/php.ini

         

    In older versions of Ubuntu, folders like /etc/php/5.4/ , /etc/php/5.5/ and /etc/php/5.6/ are exchanged for /etc/php5/

        
    13.01.2017 / 20:04
    0
    $streamContext = stream_context_create(array(
        'http' => array(
            'method' => 'GET',
            'timeout' => 30
         )
    ));
    

    Where the key timeout is in seconds.

    Then:

    file_get_contents('https://getcomposer.org/versions', 0, $streamContext);
    
        
    13.01.2017 / 12:38
    0

    Start by trying to include www .

    You can also try: ini_set('default_socket_timeout', 900); (or by the Zooboomafoo response script) Increase the timeout that is 60 segundos by default.

    Verify that your server can access external resources if there are no restrictions in the firewall.

    And you can try disabling Ipv6 or setting the Socket context with bindto

    If this does not resolve, use cURL .. (zuera!;))

        
    13.01.2017 / 12:50