我在编写以下函数时遇到了一个奇怪的问题。简而言之,此函数连接到URL并使用curl函数获取输出。但是,当我执行此功能时,我得到“服务器意外断开连接”消息:
/**
* Connects to remote URL and posts parameters, returns the result
*
* @param string $URL
* @param string $ArrayPostParameters
* @param string $HTTPRequestType
* @param string $HTTPAuth
* @param string $HTTPAuthUsername
* @param string $HTTPAuthPassword
* @param string $ConnectTimeOutSeconds
* @param string $ReturnHeaders
* @return array
* @author xxxxxxxxxx
*/
function DataPostToRemoteURL($URL, $ArrayPostParameters, $HTTPRequestType = 'POST', $HTTPAuth = false, $HTTPAuthUsername = '', $HTTPAuthPassword = '', $ConnectTimeOutSeconds = 1, $ReturnHeaders = false)
{
$PostParameters = implode('&', $ArrayPostParameters);
$CurlHandler = curl_init();
curl_setopt($CurlHandler, CURLOPT_URL, $URL);
if ($HTTPRequestType == 'GET')
{
curl_setopt($CurlHandler, CURLOPT_HTTPGET, true);
}
elseif ($HTTPRequestType == 'PUT')
{
curl_setopt($CurlHandler, CURLOPT_PUT, true);
}
elseif ($HTTPRequestType == 'DELETE')
{
curl_setopt($CurlHandler, CURLOPT_CUSTOMREQUEST, 'DELETE');
}
else
{
curl_setopt($CurlHandler, CURLOPT_POST, true);
curl_setopt($CurlHandler, CURLOPT_POSTFIELDS, $PostParameters);
}
curl_setopt($CurlHandler, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($CurlHandler, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($CurlHandler, CURLOPT_FOLLOWLOCATION, 5);
curl_setopt($CurlHandler, CURLOPT_CONNECTTIMEOUT, $ConnectTimeOutSeconds);
curl_setopt($CurlHandler, CURLOPT_TIMEOUT, $ConnectTimeOutSeconds);
curl_setopt($CurlHandler, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.3) Gecko/2008092417 Firefox/3.0.3');
// The option doesn't work with safe mode or when open_basedir is set.
if ((ini_get('safe_mode') != false) && (ini_get('open_basedir') != false))
{
curl_setopt($CurlHandler, CURLOPT_FOLLOWLOCATION, true);
}
if ($ReturnHeaders == true)
{
curl_setopt($CurlHandler, CURLOPT_HEADER, true);
}
else
{
curl_setopt($CurlHandler, CURLOPT_HEADER, false);
}
if ($HTTPAuth == true)
{
curl_setopt($CurlHandler, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($CurlHandler, CURLOPT_USERPWD, $HTTPAuthUsername.':'.$HTTPAuthPassword);
}
$RemoteContent = curl_exec($CurlHandler);
if (curl_error($CurlHandler) != '')
{
return array(false, curl_error($CurlHandler));
}
curl_close($CurlHandler);
return array(true, $RemoteContent);
}
当我调试函数时,我注意到curl_exec()函数导致了这个问题。没有curl_error消息,也没有生成PHP错误。
您对原因有什么看法吗?在PHP.INI中禁用safe_mode以获取您的信息。
提前感谢您的帮助!
答案 0 :(得分:1)
你是怎么称呼这个功能的?如果您将$ ConnectTimeOutSeconds选项保留为默认值1,则CURLOPT_TIMEOUT设置可能会导致问题,因为连接并返回页面可能需要1秒以上的时间。