2009-05-27 59 views
0

我遇到以下我寫的函數有一個奇怪的問題。簡而言之,該函數連接到一個URL並使用curl函數獲取輸出。然而,當我執行此功能,我得到一個「服務器意外下降連接」消息:curl_exec函數導致服務器斷開連接

/** 
* 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。

感謝您的幫助提前!

回答

1

你是怎麼調用這個函數的?如果您將$ ConnectTimeOutSeconds選項的默認值設置爲1,則CURLOPT_TIMEOUT設置可能會導致該問題,因爲連接並返回該頁面可能需要1秒多的時間。

+0

嗨,$ ConnectTimeOutSeconds設置爲10秒或15秒,即使設置爲1秒,它應該返回「false」而不是導致「服務器斷開連接意外」錯誤?我錯了嗎? – TamTam 2009-05-28 12:39:55

相關問題