使用 Multi-Curl 进行链接检查

发布于 2024-10-02 02:58:25 字数 1597 浏览 3 评论 0原文

我正在构建一个链接检查器函数来检查链接是否具有代码 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

终难遇 2024-10-09 02:58:25

我解决了这个问题 - 返回的订单对于理解并与结果结合起来很重要。对于可能正在寻找答案的人:

      $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();
          $returnedOrder = array();

          do{

            curl_multi_exec($master, $running);
            $info = curl_multi_info_read($master);

            if($info['handle']) {
              $finalresult[] = curl_multi_getcontent($info['handle']);
              $returnedOrder[] = array_search($info['handle'], $curl_arr, true);
              curl_multi_remove_handle($master, $info['handle']);
              curl_close($curl_arr[end($returnedOrder)]);
            }

          $previousActive = $running;
          }
          while($running > 0);

          $res = array_combine($returnedOrder, $finalresult);
          curl_multi_close($master);

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:

      $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();
          $returnedOrder = array();

          do{

            curl_multi_exec($master, $running);
            $info = curl_multi_info_read($master);

            if($info['handle']) {
              $finalresult[] = curl_multi_getcontent($info['handle']);
              $returnedOrder[] = array_search($info['handle'], $curl_arr, true);
              curl_multi_remove_handle($master, $info['handle']);
              curl_close($curl_arr[end($returnedOrder)]);
            }

          $previousActive = $running;
          }
          while($running > 0);

          $res = array_combine($returnedOrder, $finalresult);
          curl_multi_close($master);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文