fsockopen,cURL和file_get_contents很慢

时间:2014-05-02 19:45:41

标签: php curl file-get-contents fsockopen

我不知道为什么......但我在标题中列出的所有方法都很慢。它们需要大约10秒钟,但当我访问这些网站时,它们会立即加载。以下是我使用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;
?>

这段代码只是我使用的一个例子。但即使使用此代码,它也会加载缓慢。我正在使用&#34;连接:关闭&#34;也。这可能是PHP配置或其他问题吗?我正在使用带有cPanel + WHM的CentOS。我确实有shell和root访问权限。

1 个答案:

答案 0 :(得分:1)

可能是您服务器上的DNS速度很慢,请尝试以下操作:

替换

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

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