gpt4 book ai didi

PHP 捕捉 cURL CURLOPT_TIMEOUT 和 CURLOPT_CONNECTTIMEOUT 事件并采取行动

转载 作者:可可西里 更新时间:2023-11-01 00:23:25 24 4
gpt4 key购买 nike

我想在检测到 CURLOPT_CONNECTTIMEOUT 和 CURLOPT_TIMEOUT 时进行检测、捕获并执行某些操作。

我该怎么做?

我有以下标题:

public static $userAgents = array(
'FireFox3' => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; pl; rv:1.9) Gecko/2008052906 Firefox/3.0',
'GoogleBot' => 'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)',
'IE7' => 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)',
'Netscape' => 'Mozilla/4.8 [en] (Windows NT 6.0; U)',
'Opera' => 'Opera/9.25 (Windows NT 6.0; U; en)'
);
public static $options = array(
CURLOPT_USERAGENT => 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)',
CURLOPT_AUTOREFERER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FRESH_CONNECT => true,
CURLOPT_COOKIEJAR => "cookies.txt",
CURLOPT_COOKIEFILE => "cookies.txt",
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_CONNECTTIMEOUT => 5,
CURLOPT_TIMEOUT => 300,
//CURLOPT_COOKIESESSION => false,
);

和下面的函数,其中 $res 是一个包含 html 文件名的数组

public function multiCurl($res, $options = "") {

if (count($res) <= 0)
return False;

$handles = array();

if (!$options) // add default options
$options = self::$options;

// add curl options to each handle
foreach ($res as $k => $row) {
$ch{$k} = curl_init();
$options[CURLOPT_URL] = $row['url'];
curl_setopt_array($ch{$k}, $options);
$handles[$k] = $ch{$k};
}

$mh = curl_multi_init();

foreach ($handles as $k => $handle) {
curl_multi_add_handle($mh, $handle);
}

$running_handles = null;
//execute the handles
do {
$status_cme = curl_multi_exec($mh, $running_handles);
} while ($cme == CURLM_CALL_MULTI_PERFORM);

while ($running_handles && $status_cme == CURLM_OK) {
if (curl_multi_select($mh) != -1) {
do {
$status_cme = curl_multi_exec($mh, $running_handles);
} while ($status == CURLM_CALL_MULTI_PERFORM);
}
}

foreach ($res as $k => $row) {
$res[$k]['error'] = curl_error($handles[$k]);
if (!empty($res[$k]['error']))
$res[$k]['data'] = '';
else {
//$res[$k]['data'] = curl_multi_getcontent($handles[$k]); // get results
file_put_contents(CRAWLER_FILES . $k . '.html', curl_multi_getcontent($handles[$k]));
}

// close current handler
curl_multi_remove_handle($mh, $handles[$k]);
}
curl_multi_close($mh);
return $res; // return response
}

最佳答案

要获取有关请求的信息,您可以查看 curl_getinfocurl_multi_info_read

然后像这样使用它:

curl_exec($ch);
if(!curl_errno($ch))
{
$info = curl_getinfo($ch);
echo 'Took ' . $info['total_time'] . ' seconds to send a request to ' . $info['url'];
}

在信息数组中,您可以接收以下数据:

  • “网址”
  • “内容类型”
  • “http_code”
  • “header_size”
  • “请求大小”
  • “文件时间”
  • “ssl_verify_result”
  • “redirect_count”
  • “总时间”
  • “namelookup_time”
  • “连接时间”
  • “传输前时间”
  • “上传大小”
  • “size_download”
  • “speed_download”
  • “speed_upload”
  • “下载内容长度”
  • “上传内容长度”
  • “开始传输时间”
  • “重定向时间”
  • “证书信息”
  • “请求头”(这仅在 CURLINFO_HEADER_OUT 由先前的设置时设置调用 curl_setopt())

关于PHP 捕捉 cURL CURLOPT_TIMEOUT 和 CURLOPT_CONNECTTIMEOUT 事件并采取行动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12138022/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com