编程语言
首页 > 编程语言> > php-需要批量发送通知1000

php-需要批量发送通知1000

作者:互联网

我已经创建了通知应用程序,现在我想向1000个用户批次发送通知,但是由于我是新用户,所以我不知道如何创建该批次.

这是我要推送通知的代码.

<?php
include('header.php');

define( 'API_ACCESS_KEY', '[API-KEY comes here]' );

$sql="SELECT * FROM user_data";
$result = mysqli_query($conn, $sql) or die ('Error'.mysqli_error($conn));
$registrationIds=array();
while($row=mysqli_fetch_assoc($result)){

    $registrationIds[] = $row['allow'];
}
$ids=json_encode($registrationIds);
$fields = array
(
    'registration_ids'  => $registrationIds
);

$headers = array
(
    'Authorization: key=' . API_ACCESS_KEY,
    'Content-Type: application/json'
);

$ch = curl_init();
curl_setopt( $ch,CURLOPT_URL, 'https://android.googleapis.com/gcm/send' );
curl_setopt( $ch,CURLOPT_POST, true );
curl_setopt( $ch,CURLOPT_HTTPHEADER, $headers );
curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $fields ) );
$result = curl_exec($ch );
curl_close( $ch );

echo $result;


?>

现在,我可以将通知推送给用户,但是如果我有10000个用户,如何以1000个为单位发送通知?

解决方法:

这样的事情(假设您想在每个curl请求中发送一批1000个注册ID):

$batch_size = 1000; // 1000 per request

$iterations = ceil(count($registrationIds) / $batch_size); // determine how many iterations are needed

for ($i = 0; $i < $iterations; $i++) {
    $offset = $i * $batch_size;
    $batch_of_1000_user_ids = array_slice($registrationIds, $offset, $batch_size);

    // json_encode, prepare headers and send curl request
}

标签:php,mysql,database,batch-processing
来源: https://codeday.me/bug/20191012/1902473.html