如何使用 PHP 中的 cURL 同时打开多个 URL?

2023-11-24

这是我当前的代码:

    $SQL = mysql_query("SELECT url FROM urls") or die(mysql_error()); //Query the urls table
while($resultSet = mysql_fetch_array($SQL)){ //Put all the urls into one variable

                // Now for some cURL to run it.
            $ch = curl_init($resultSet['url']); //load the urls
            curl_setopt($ch, CURLOPT_TIMEOUT, 2); //No need to wait for it to load. Execute it and go.
            curl_exec($ch); //Execute
            curl_close($ch); //Close it off 
        } //While loop

我对 cURL 还比较陌生。我所说的“相对较新”是指这是我第一次使用 cURL。目前,它会加载一个 2 秒,然后加载下一个 2 秒,然后加载下一个。但是,我想让它同时加载所有这些。我确信这是可能的,我只是不确定如何实现。如果有人能指出我正确的方向,我将不胜感激。


您以相同的方式设置每个 cURL 句柄,然后将它们添加到curl_multi_处理。要查看的函数是curl_multi_*功能记录在这里。不过,根据我的经验,尝试一次加载太多 URL 会出现问题(尽管我目前找不到关于它的注释),所以上次我使用curl_mutli_,我将其设置为一次批量处理 5 个 URL。

edit:这是我使用的代码的简化版本curl_multi_:

edit:稍微重写并添加了很多评论,希望能有所帮助。

// -- create all the individual cURL handles and set their options
$curl_handles = array();
foreach ($urls as $url) {
    $curl_handles[$url] = curl_init();
    curl_setopt($curl_handles[$url], CURLOPT_URL, $url);
    // set other curl options here
}

// -- start going through the cURL handles and running them
$curl_multi_handle = curl_multi_init();

$i = 0; // count where we are in the list so we can break up the runs into smaller blocks
$block = array(); // to accumulate the curl_handles for each group we'll run simultaneously

foreach ($curl_handles as $a_curl_handle) {
    $i++; // increment the position-counter

    // add the handle to the curl_multi_handle and to our tracking "block"
    curl_multi_add_handle($curl_multi_handle, $a_curl_handle);
    $block[] = $a_curl_handle;

    // -- check to see if we've got a "full block" to run or if we're at the end of out list of handles
    if (($i % BLOCK_SIZE == 0) or ($i == count($curl_handles))) {
        // -- run the block

        $running = NULL;
        do {
            // track the previous loop's number of handles still running so we can tell if it changes
            $running_before = $running;

            // run the block or check on the running block and get the number of sites still running in $running
            curl_multi_exec($curl_multi_handle, $running);

            // if the number of sites still running changed, print out a message with the number of sites that are still running.
            if ($running != $running_before) {
                echo("Waiting for $running sites to finish...\n");
            }
        } while ($running > 0);

        // -- once the number still running is 0, curl_multi_ is done, so check the results
        foreach ($block as $handle) {
            // HTTP response code
            $code = curl_getinfo($handle,  CURLINFO_HTTP_CODE);

            // cURL error number
            $curl_errno = curl_errno($handle);

            // cURL error message
            $curl_error = curl_error($handle);

            // output if there was an error
            if ($curl_error) {
                echo("    *** cURL error: ($curl_errno) $curl_error\n");
            }

            // remove the (used) handle from the curl_multi_handle
            curl_multi_remove_handle($curl_multi_handle, $handle);
        }

        // reset the block to empty, since we've run its curl_handles
        $block = array();
    }
}

// close the curl_multi_handle once we're done
curl_multi_close($curl_multi_handle);

鉴于您不需要从 URL 返回任何内容,您可能不需要很多内容,但这就是我将请求分成块的方式BLOCK_SIZE,等待每个块运行后再继续,并捕获来自 cURL 的错误。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何使用 PHP 中的 cURL 同时打开多个 URL? 的相关文章

  • 将数据库中的用户 ID 添加到 Codeigniter 中的会话数据中?

    我是 CodeIgniter 的新手 在从数据库添加用户 ID 用户登录后 到会话数据时遇到问题 这是我的代码问题 之前可能会在 SOF 上被问到 在付出了所有努力之后 我问这个 登录模型
  • PHP 基本身份验证 file_get_contents() [重复]

    这个问题在这里已经有答案了 我需要从网站解析一些 XML 数据 XML 数据是原始格式 但在我需要进行身份验证之前 基于基本网络服务器的身份验证 使用用户名和密码 I tried homepage file get contents htt
  • 在 Laravel 中动态设置数据库连接和语言

    我有 3 个域指向同一个Laravel应用 我想要的是每个人都连接到自己的数据库并根据 TLD 加载自己的语言文件 我可以在哪个文件中设置这些设置 我可以直接在配置文件中执行此操作 或者可以在加载配置之前执行某些事件 我拥有的是一个简短的函
  • PHP使用auto_increment生成短唯一ID?

    我想生成一个简短的 唯一的 ID 而不必检查冲突 我目前正在做类似的事情 但是我当前生成的 ID 是随机的 并且在循环中检查冲突很烦人 并且如果记录数量显着增加 将会变得昂贵 通常担心冲突不是问题 但我想要生成的唯一 ID 是一个由 5 8
  • jruby 的路边(卷曲)?

    我正在尝试将curl 与jruby 结合使用来获得网页 文件的一些响应时间 通常在 ruby 中这不会是一个问题 我可以安装 gem gem install junction 一切都很好 遏制似乎与 jruby 不兼容 那么有没有一种替代方
  • MySQL集群启动失败

    这不是我第一次创建ndbcluster 但我没有收到这样的问题 我正在关注本手册 https hub docker com r mysql mysql cluster by mysql团队 我正在使用回显的默认配置在此 GitHub 存储库
  • 具有更改用户代理上下文的 file_get_contents 不起作用

    我正在尝试获取页面的阅读数和点赞数 网址是 https mp weixin qq com s NPavBeHc8VdWXeSL6kfLRg https mp weixin qq com s NPavBeHc8VdWXeSL6kfLRg 您必
  • 猪的组连接等效吗?

    试图在 Pig 上完成这个任务 寻找 MySQL 的 group concat 等效项 例如 在我的表中 我有以下内容 3fields userid clickcount pagenumber 155 2 12 155 3 133 155
  • 如何让Gmail像加载进度条一样

    我想在页面的中心和顶部创建一个像 Gmail 一样的加载进度条 并适用于所有浏览器 这是基本代码
  • 简单的dom php解析获取自定义数据属性值

    HTML div class something ddsf PHP foreach dom gt find something data rel as this var dump this gt attr 我尝试了这个但错误 在其文档中找不
  • MySQL - 多个结果集

    我正在使用 NET Connector 连接到 MySQL 在我的应用程序中 很少有线程使用相同的连接 因此如果 MySQLDataReader 尚未关闭并且某个线程正在尝试执行查询 则会出现该错误 已经有一个打开的 DataReader
  • 使用 fopen() 包装器创建 ZIP 文件

    如何使用以下命令创建 ZIP 文件fopen 包装器 http es php net manual en wrappers compression php 这显然是not道路
  • 如何确保在 PHP 的“foreach”循环中重置该值?

    我正在写一个简单的 PHP 页面和一些foreach使用了循环 以下是脚本 arrs array a b c foreach arrs as arr if substr arr 0 1 b echo This is b End of fir
  • 接口中的构造方法

    接口中的构造方法不好吗 为什么人们认为有人想要实例化接口 我们想要做的是强制实现者实现构造函数 就像其他接口方法一样 接口就像一个合同 假设我有一个接口 Queue 并且我想确保实现者创建一个带有一个参数的构造函数 该构造函数创建一个单例队
  • PHP 中的多个插入查询[重复]

    这个问题在这里已经有答案了 我正在尝试创建一个 php html 表单 它将结果插入到狗展数据库中 问题是 无论我做什么 我都会收到此错误 查询失败 您的 SQL 语法有错误 检查与您的 MySQL 服务器版本相对应的手册 了解在 INSE
  • PDO语法错误

    我在一个项目中使用 PDO 但提交时出现语法错误 这是我的代码
  • 如何在查询语句之外从mysql查询中获取值?

    这是下面的函数console log function quo value value connection query SELECT role from roles where id 1 function error results fi
  • 如何在数据列表 HTML PHP 中设置选择

    您好我想知道是否有一种方法可以在数据列表中设置选定的值 我想要这样的东西
  • 使用“INSERT ... ON DUPLICATE KEY UPDATE”插入多条记录

    我的表结构 table marks 我的目标 我想用条件插入或更新多条记录 我目前正在通过此查询进行检查 第一步 SELECT FROM marks WHERE student 115 AND param 1 第二步 if records
  • 如何在 PHP 中从字符串类名实例化? [关闭]

    Closed 这个问题需要细节或清晰度 help closed questions 目前不接受答案 如何创建返回方法名称的新实例 不幸的是我收到这个错误 错误 类名必须是有效的对象或字符串 这是我的代码 class Foo public f

随机推荐