0

我希望尽快进行一堆 REST api 调用。我目前有大约 1,000 个请求要提出。

任何处理或类似的事情都不需要这些调用的结果。我只需要将它们全部发布到 api url。

我当然在我的循环中尝试过,这非常慢。我也尝试过使用 curl_multi_exec ,但这也很慢。这是那个代码。

foreach($users as $user){
                $ch = curl_init();
                curl_setopt($ch, CURLOPT_USERAGENT, 'Mandrill-PHP/1.0.36');
                curl_setopt($ch, CURLOPT_POST, true);
                curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
                curl_setopt($ch, CURLOPT_HEADER, false);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
                curl_setopt($ch, CURLOPT_TIMEOUT, 600);
                curl_setopt($ch, CURLOPT_URL, 'https://mandrillapp.com/api/1.0/messages/send.json');
                curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
                curl_setopt($ch, CURLOPT_VERBOSE, TRUE);
                curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
                curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($mandril_message));

                curl_multi_add_handle($mh,$ch);
}
        $active = null;
        //execute the handles
        do {
            $mrc = curl_multi_exec($mh, $active);
        } while ($mrc == CURLM_CALL_MULTI_PERFORM);

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

关于如何快速做到这一点的任何想法。在没有 curl 代码的情况下,我的其余代码只需要一秒钟左右即可完成。

4

2 回答 2

2

好的,所以我终于找到了一些时间来使用 mandril api 使其正常工作。我从大约 2000 个 api 请求变为 1 个。处理时间从 10 分钟变为 6.8 秒。

所以基本上你只需要确保你在那里使用合并变量并为每组合并变量指定一个接收者。

这是一个如何执行此操作的示例。

"to": [
        {
            "email": "recipient1.email@example.com"
        },
        {
            "email": "recipient2.email@example.com"
        }
    ],
"merge_vars": [
        {
            "rcpt": "recipient1.email@example.com",
            "vars": [
                {
                    "name": "FNAME",
                    "content": "John"
                },
                {
                    "name": "FEED",
                    "content": "Your personalized feed content here"
                }
            ],
            "rcpt": "recipient2.email@example.com",
            "vars": [
                {
                    "name": "FNAME",
                    "content": "Jane"
                },
                {
                    "name": "FEED",
                    "content": "Your personalized feed content here"
                }
            ]
        }
    ]
于 2013-07-30T20:51:17.330 回答
0

一千个 REST 请求会很慢。这里的技巧是异步运行作业,这样用户就不会受到影响。您可以向相关 PHP 脚本发送 AJAX 请求并增强 PHP 脚本以在某处写入进度数据($_SESSION、平面文件、数据库等)。然后您可以发送单独的 AJAX 调用以获取作业进度并显示在作业运行时更新给用户。

这一切都假设这是我们在这里讨论的网页而不是 shell 脚本。

于 2013-07-18T17:48:54.150 回答