【发布时间】:2015-08-17 09:34:35
【问题描述】:
大家好,我正在制作一个工具,该工具将从数据库中获取电话号码并发出 curl/api 请求以验证号码并获取其信息,然后根据 API 响应更新数据库中的某些字段。
所以我有一个名为phones 的表
->id
->number
->network
->country
所以在我的表中只有 id 和 number 有值,network 和 country 为空。这就是我将使用 API 来根据数字更新这些字段的原因。但是有一个问题,所以基本上会发生什么我会像这样循环所有这些数字:
$phone = Phone::all();
foreach ($phone as $key => $value)
{
// Call the API that will get the details in the current number
// and update the details in the table for the current number
/** Some code for API Call **/
//Update Script
$update = Phone::find($value->id);
$update->network = $network;
$update->country = $country;
$update->country_prefix = $country_prefix;
$update->status = $status;
$update->remarks = $remarks;
$update->save();
}
这可以很好地完成我的任务,但问题是当我循环输入时这非常慢,比如说 50,000 条记录,因为在它可以发送下一个 curl 请求之前,它必须等待前一个的响应对?问题是如何使每个循环计数为 20 个请求?因为我使用的 API 支持每秒 20 个请求,所以我不想最大化它。
我知道我的循环会改变,因为我需要一次获取 20 条记录,并且不再重复相同的记录。
【问题讨论】:
标签: php multithreading curl