有没有一种方法我可以Array_chunk mysqli结果,我从一个表中循环消息,然后将值传递到一个方法中"Sms";该方法将创建一个Sms对象列表,我通过函数SendBatchSMS传递该列表。我的API端点每个请求只能允许100个调用。
我尝试过数组将列表分块为"$sms";当我打印_r($sms(时,它可以很好地工作,但当回显响应时,无论array_counk函数中指定的大小如何,它都只返回48/249个响应。我的问题是,是否有更好的选择来实现这一点,比如array_chunking mysqli结果而不是数组列表?
$query_sch = "SELECT * FROM ct_queue";
$sch_result = mysqli_query($mysqli, $query_sch);
$rows[] = mysqli_fetch_array($sch_result);
$count = mysqli_num_rows($sch_result);
foreach($sch_result as $value)
{
$phone = $value['phone'];
$sender = $value['sender'];
$message = $value['message'];
$user_id = $value['user_id'];
$link_id = NULL;
$correlator = 'correlator_string';
$endpoint = 'example.com';
$token = "token_string";
// $list = array();
$version = "v1"; //DONT change unless you are using a different version
$instance = new BonTech($token, $version);
$list[] = new Sms($sender, $phone, $message, $correlator, null, $endpoint);
}
$row_chunks = array_chunk($list, 100);
foreach ($row_chunks as $chunk){
$sms = array();
////////here we have 100 messages on each chunk
///////Loop through the messages in side the chunk
foreach ($chunk as $row) {
$sms[] = ($row);
}
// print_r($sms);
}
$response = call_user_func_array(array($instance, "sendBatchSMS"), $sms);
$response = json_encode($response, true);
$results = json_decode($response, true);
print_r($response);
在foreach
循环完成后使用$sms
。因此,它将只包含最后一块。你需要在循环中使用它。
也不需要使用循环来将$chunk
复制到$sms
。
由于在第一个foreach
循环之前调用了mysqli_fetch_array($sch_result)
,因此您也将跳过第一行结果。
$instance
似乎不依赖于$value
,所以它不应该在foreach
循环中。
$query_sch = "SELECT * FROM ct_queue";
$sch_result = mysqli_query($mysqli, $query_sch);
$list = array();
foreach($sch_result as $value)
{
$phone = $value['phone'];
$sender = $value['sender'];
$message = $value['message'];
$user_id = $value['user_id'];
$link_id = NULL;
$correlator = 'correlator_string';
$endpoint = 'example.com';
$list[] = new Sms($sender, $phone, $message, $correlator, null, $endpoint);
}
$token = "token_string";
$version = "v1"; //DONT change unless you are using a different version
$instance = new BonTech($token, $version);
$row_chunks = array_chunk($list, 100);
foreach ($row_chunks as $sms){
$response = call_user_func_array(array($instance, "sendBatchSMS"), $sms);
print_r($response);
}