0

My Code:

<?php
    $url = "http://www.myurl.com/";
    /*$opts = array(
        'http'=>array(
            'method'=>"GET",
            'header'=>"User-Agent: ".$_SERVER['HTTP_USER_AGENT']
        )
    );
    $context = stream_context_create($opts);*/
    $content = file_get_contents($url);
    echo $content;
?>

I receive the following error:

Warning: file_get_contents(http://www.myurl.com/) [function.file-get-contents]: failed to open stream: Connection timed out in /home/content/myurl/contents.php on line 10

The code won't work for me when I am either creating or not creating a stream context. Interesting thing is that it would work on any other site that I have tried, except for my own.

PS: It does not matter if I do it with or without "http" or "www".

PSPS: allow_url_fopen is ON

PSPSPS: I have set the following in .htaccess:

<filesMatch "\\.(html|htm|php)$">
     Header set Cache-Control "max-age=1, private, must-revalidate"
     </filesMatch>

Could this be a problem?

shamrock
  • 47
  • 10
  • Do you have a GUI and if so have you tried visiting the URL in a web browser? If you have CLI access and have Wget can you try performing the same request with Wget and let me know the result. – Professor of programming Apr 07 '15 at 14:51
  • A connection timeout often means a firewall dropping requests. What's the server operating system, and can you `telnet` to port 80 on this machine from an external one? – kalatabe Apr 07 '15 at 14:52
  • try this one here with by adding an agent to the header http://stackoverflow.com/questions/10524748/why-im-getting-500-error-when-using-file-get-contents-but-works-in-a-browser?rq=1 – albanx Apr 07 '15 at 15:05
  • If you mean something like `"User-Agent: ".$_SERVER["HTTP_USER_AGENT"]."\r\n"` than this won't work neither. – shamrock Apr 07 '15 at 15:21

4 Answers4

0

Have you tried using curl?

    function curl($url){
                $ch = curl_init();
                curl_setopt($ch, CURLOPT_URL, $url);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
                curl_setopt($ch,CURLOPT_FOLLOWLOCATION,true);
                $data = curl_exec($ch);
                curl_close($ch);
                return $data;
      }


      $url = 'http://www.myurl.com/';
      $contents = curl($url);
MrTechie
  • 1,797
  • 4
  • 20
  • 36
  • Yes. Tried to use cUrl too. Your code can load many sites but in my case it is just blank. (even with print_r) – shamrock Apr 07 '15 at 14:58
0

Try to set max execution time to larger period , it may have been set to lower. ini_set('max_execution_time', 60); //1 minute

0

Your server should have a firewall that restricts you from connecting to an external resource. So check your firewall.

Arlind Hajredinaj
  • 8,380
  • 3
  • 30
  • 45
0

It should work on a shared host, but if it is your own server, it might be a network or DNS issue on the server/LAN that prevents the server from resolving 'www.myurl.com' to it's own address. If you aren't using virtual hosts (on apache) or equivalent mechanism, it would work by using 'http://localhost/' instead