簡體   English   中英

PHP在URL錯誤的curl函數循環中停頓

[英]PHP stalls on loop of curl function with bad url

我有一個包含數千個URL的數據庫,我正在檢查頁面上的鏈接(最終查找特定的鏈接),因此我通過循環拋出以下函數,並且每隔一段時間,其中一個URL是不好的,然后整個程序停頓並停止運行,並開始建立已使用的內存。 我以為添加CURLOPT_TIMEOUT可以解決此問題,但沒有解決。 有任何想法嗎?

$options = array(
    CURLOPT_RETURNTRANSFER => true,         // return web page
    CURLOPT_HEADER         => false,        // don't return headers
    CURLOPT_FOLLOWLOCATION => true,         // follow redirects
    CURLOPT_ENCODING       => "",           // handle all encodings
    CURLOPT_USERAGENT      =>  "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13'",     // who am i
    CURLOPT_AUTOREFERER    => true,         // set referer on redirect
    CURLOPT_TIMEOUT        => 2,          // timeout on response
    CURLOPT_MAXREDIRS      => 10,           // stop after 10 redirects
    CURLOPT_POST            => 0,            // i am sending post data
       CURLOPT_POSTFIELDS     => $curl_data,    // this are my post vars
    CURLOPT_SSL_VERIFYHOST => 0,            // don't verify ssl
    CURLOPT_SSL_VERIFYPEER => false,        //
    CURLOPT_VERBOSE        => 1                //
);

$ch      = curl_init($url);
curl_setopt_array($ch,$options);
$content = curl_exec($ch);
$err     = curl_errno($ch);
$errmsg  = curl_error($ch) ;
$header  = curl_getinfo($ch);
curl_close($ch);

//  $header['errno']   = $err;
//  $header['errmsg']  = $errmsg;
$header['content'] = $content;

#Extract the raw URl from the current one
$scheme = parse_url($url, PHP_URL_SCHEME); //Ex: http
$host = parse_url($url, PHP_URL_HOST); //Ex: www.google.com
$raw_url = $scheme . '://' . $host; //Ex: http://www.google.com

#Replace the relative link by an absolute one
$relative = array();
$absolute = array();

#String to search
$relative[0] = '/src="\//';
$relative[1] = '/href="\//';

#String to remplace by
$absolute[0] = 'src="' . $raw_url . '/';
$absolute[1] = 'href="' . $raw_url . '/';

$source = preg_replace($relative, $absolute, $content); //Ex: src="/image/google.png" to src="http://www.google.com/image/google.png"

return $source;

如果找不到URL,curl_exec將返回false。 HTTP狀態代碼將為零。 檢查curl_exec的結果,並檢查HTTP狀態代碼。

$content = curl_exec($ch);
$httpStatus = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ( $content === false) {
   if ($httpStatus == 0) {
    $content = "link was not found";
   }
}
....

當前的方式,代碼行

header['content'] = $content;

將獲得false的值。 這不是您想要的。

我正在使用curl_exec,如果找不到URL,我的代碼也不會停頓。 該代碼保持運行。 您可能最終在瀏覽器中什么也沒有,在Firebug控制台中看到一條消息,例如“ 500 Internal Server Error”。 也許這就是您所說的失速。

因此,基本上您不知道,只是猜測curl請求正在暫停。

對於這個答案,我也只能猜測。 您可能還需要設置以下curl選項之一: CURLOPT_CONNECTTIMEOUT

如果連接已停止,則可能不會考慮其他超時設置。 我不確定,但是請參閱設置超時時間到3000ms時為什么CURL會在1000ms超時?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM