2014-05-02 56 views
2

我不知道为什么...但是我在标题中列出的所有方法都很慢。他们需要大约10秒钟,但当我访问这些网站时,他们立即加载。以下是我使用file_get_conents的代码之一:fsockopen,cURL和file_get_contents很慢

<?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; 
?> 

此代码仅仅是我使用的一个示例。但即使使用此代码,它的加载速度也很慢。我正在使用“连接:关闭”也。这可能是PHP配置的问题,或者其他的问题?我正在使用CentOS和cPanel + WHM。我有shell和root权限。

+0

您的服务器必须是缓慢的,有什么主机? –

+0

PhotonVPS,当我加载我的网站时速度非常快。只有标题中列出的方法。有时,如果我刷新PHP页面两次,它将加载速度很快。这很奇怪... – user3597487

+0

我也遇到了问题..你有什么线索吗? –

回答

1

这可能是因为你的服务器上的DNS是缓慢的,试试这个:

更换

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

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