使用 Multi-Curl 进行链接检查
我正在构建一个链接检查器函数来检查链接是否具有代码 200/301/302。
我想检查大约 1000 个链接,因此我使用了 Multi-CURL 功能,并且确实获得了所有标头、代码以及 URL 重定向到的 URL。
问题是 Multi-CURL 并行执行,将所有 URL 添加到curl_multi_add_handle 并返回它获得的结果并忽略其余的结果。
我从标题中知道我返回了哪个结果,但我不知道哪个 URL 带来了它。是否有我针对特定执行句柄请求的 URL 的标识符(可能与 curl_multi_info_read 有关)?
这是我的代码:
$curls = $listofurls;
$curl_arr = array();
$master = curl_multi_init();
for($i = 0; $i < $node_count; $i++) {
$curl_arr[$i] = curl_init($curls[$i][0]);
curl_setopt($curl_arr[$i],CURLOPT_FRESH_CONNECT,true);
curl_setopt($curl_arr[$i],CURLOPT_CONNECTTIMEOUT,10);
curl_setopt($curl_arr[$i],CURLOPT_HEADER,true);
curl_setopt($curl_arr[$i],CURLOPT_CUSTOMREQUEST,'HEAD');
curl_setopt($curl_arr[$i],CURLOPT_RETURNTRANSFER,true);
curl_setopt($curl_arr[$i],CURLOPT_NOBODY,true);
curl_setopt($curl_arr[$i],CURLOPT_AUTOREFERER, 1);
curl_setopt($curl_arr[$i],CURLOPT_TIMEOUT,30);
curl_multi_add_handle($master, $curl_arr[$i]);
}
$finalresult = array();
do{
curl_multi_exec($master, $running);
$info = curl_multi_info_read($master);
if($info['handle']) {
$finalresult[] = curl_multi_getcontent($info['handle']);
curl_multi_remove_handle($master, $info['handle']);
}
$previousActive = $running;
}
while($running > 0);
curl_multi_close($master);
感谢 Help.Thanx。
Im building a Link Checker function that checks if the link has code 200/301/302.
I want to check about 1000 links so i I used the Multi-CURL functionality and i do get all the headers, codes, the URL to which a URL redirected.
The Problem is that Multi-CURL executes in parallel adding all the URLs to curl_multi_add_handle and returns the results it gets and ignores the rest.
I know from the header which result i got back but i dont know which URL brought it. Is there an Identifier which URL i requested against a specific executed handle (may be something to do with curl_multi_info_read )?
Here my Code:
$curls = $listofurls;
$curl_arr = array();
$master = curl_multi_init();
for($i = 0; $i < $node_count; $i++) {
$curl_arr[$i] = curl_init($curls[$i][0]);
curl_setopt($curl_arr[$i],CURLOPT_FRESH_CONNECT,true);
curl_setopt($curl_arr[$i],CURLOPT_CONNECTTIMEOUT,10);
curl_setopt($curl_arr[$i],CURLOPT_HEADER,true);
curl_setopt($curl_arr[$i],CURLOPT_CUSTOMREQUEST,'HEAD');
curl_setopt($curl_arr[$i],CURLOPT_RETURNTRANSFER,true);
curl_setopt($curl_arr[$i],CURLOPT_NOBODY,true);
curl_setopt($curl_arr[$i],CURLOPT_AUTOREFERER, 1);
curl_setopt($curl_arr[$i],CURLOPT_TIMEOUT,30);
curl_multi_add_handle($master, $curl_arr[$i]);
}
$finalresult = array();
do{
curl_multi_exec($master, $running);
$info = curl_multi_info_read($master);
if($info['handle']) {
$finalresult[] = curl_multi_getcontent($info['handle']);
curl_multi_remove_handle($master, $info['handle']);
}
$previousActive = $running;
}
while($running > 0);
curl_multi_close($master);
Appreciate the Help.Thanx.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我解决了这个问题 - 返回的订单对于理解并与结果结合起来很重要。对于可能正在寻找答案的人:
I got it solved-Its the returned Order thats important to understand and combine with the result. For someone who may be looking for the answer: