2

I have no clue why... But all the methods I listed in the title are slow. They take about 10 seconds, yet when I visit the sites, they load immediately. Here is one of my codes that uses file_get_conents:

<?php
$search = $_GET['search'];
$postdata = http_build_query(
array(
'searchnode' => '' . $search . ''
)
);
$opts = array('http' =>
array(
'method'  => 'POST',
'header'  => 'Connection: close',
'content' => $postdata
)
);
$context  = stream_context_create($opts);
$result = file_get_contents('http://google.com', false, $context);
echo $result;
?>

This code is just an example of what I use. But even using this code, it loads slow. I am using "Connection: close" also. Is this possibly a problem with the PHP config, or something else? I am using CentOS with cPanel + WHM. I do have shell and root access.

user3597487
  • 41
  • 2
  • 4
  • your server must be slow, what host? – David Xu May 02 '14 at 19:48
  • PhotonVPS, it is very fast when I load my website. It is only with the methods listed in the title. Sometimes, it will load fast if I refresh the PHP page twice. This is weird... – user3597487 May 02 '14 at 19:51
  • I'm also getting the problem .. do u have any clue ? – Bhavesh G Sep 19 '14 at 13:48
  • Ten seconds is typically DNS timeouts. As you specified http://google.com without trailing slash, it's likely that your first request gets redirected, which may lead to a DNS lookup. Maybe one of the DNS servers specified runs in a timeout, check /etc/resolv.conf and try "dig @1.2.3.4 google.com" for each ip address configured in /etc/resolv.conf – Thomas L. Aug 11 '15 at 08:04

1 Answers1

1

It could be that the DNS on your server is slow, try this:

replace

$result = file_get_contents('http://google.com', false, $context);

with

$ip = gethostbyname('google.com');    
$result = file_get_contents("http://$ip", false, $context);