使用PHP在后台运行多个cURL [英] Run multiple cURLs in background using PHP
问题描述
我想在Ubuntu上使用PHP在后台运行多个cURL任务. 有几种方法,但是我不确定应该选择哪种方法.
I want to run multiple cURL tasks in background using PHP on Ubuntu. There are a few ways, but I'm not sure which one I should choose.
方法1:使用操作系统的cURL
<?php
require_once('database.php');
$db = new Database; // SQLite3 database
$query = $db->query("SELECT * FROM users");
while ($user = $query->fetchArray(SQLITE3_ASSOC)) {
exec("nohup curl --url http://example.com/?id=".$user['id']." &");
}
?>
方法2: http://www. paul-norman.co.uk/2009/06/asynchronous-curl-requests
Way 2: http://www.paul-norman.co.uk/2009/06/asynchronous-curl-requests
<?php
require_once('database.php');
$db = new Database; // SQLite3 database
$query = $db->query("SELECT * FROM users");
while ($user = $query->fetchArray(SQLITE3_ASSOC)) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://example.com/?id=".$user['id']);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, true);
curl_setopt($ch, CURLOPT_TIMEOUT_MS, 1);
curl_exec($ch);
curl_close($ch);
}
?>
我不明白2的工作方式.有人可以解释吗?如果我有10万至50万用户,应该选择哪种方式? 请注意,cURL可以完成大约2-8秒的工作.我不确定2可以正常工作,因为如果它可以完成几秒钟并且超时设置为1 ms-未完成时连接会停止吗?
I don't understand how way 2 works. Can someone explain it? What way should I choose if I have 100 000 - 500 000 users? Please note that cURL can do its job for about 2 - 8 seconds. I'm not sure way 2 will work, because if it can do its job for a few seconds and the timeout is set to 1 ms - will the connection be stopped when it isn't done?
EDIT :方法2对我不起作用,因为需要更长的超时时间.方法1可能会导致您的计算机工作慢得多.我这样做是:当我需要所有用户的ID时,我不会执行1000个请求,而是将数据库(SQLite)发送到另一台服务器.然后,它查找ID.
EDIT: Way 2 doesn't work for me, because higher timeout is needed. Way 1 may cause your computer work much slower. I did this: when I need the ID of all users I don't do 1000 requests but I send the database (SQLite) to my another server. Then, it looks for the IDs.
推荐答案
您的第一个代码将在后台运行curl请求,即tle循环将运行N次,发出N个curl请求,并且PHP脚本将完成(无论卷曲请求).
Your first code will run the curl request in background, i.e tle loop will run N number of times issuing N curl request and the PHP script will complete (irrespective of curl request).
第二个选项将发出CURL请求,Web服务器将向您发送状态504(超时),但是脚本仍在后台运行-只是停止向客户端发送数据.
The Second Option will issue a CURL Request and the web server will send you status 504 (Timeout), however the script still runs in the background -- it just stops sending data to the client.
我仍然不确定1ms的超时时间,因为connect_time可能会更高.
Still I am not sure with 1ms timeout because the connect_time might be higher.
这篇关于使用PHP在后台运行多个cURL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!