我正在尝试向我们应用程序的用户发送FCM推送通知,我需要向每个用户发送13k定制消息,其中每个通知都包含他的姓名和一些数据,因此我需要为每个用户发送FCM请求,我的问题是
如何在不降低性能的情况下发送此数量的请求并增加服务器负载?
因为我尝试了许多答案,并使用“内部服务器错误”使服务器停机,但我也尝试对请求进行循环,并且每个循环使PHP睡眠(1)或更多秒,并且使我超时,所以我尝试了使用set_time_limit和init最大执行时间来增加超时时间,但是一旦我运行PHP文件,它也会给我内部服务器错误。
我正在使用ajax从cPanel调用此PHP文件 并且应该做这份工作 在PHP脚本中,我每次将用户划分为1000个用户。
$curlResults = array();
$curlRequests = array_chunk($curlRequests, 250);
foreach ($curlRequests as $requests) {
try {
$results = $this->executeFCMRequests($requests);
$curlResults = array_merge($curlResults, $results);
sleep(2);
} catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
}
}
function executeFCMRequests($requests)
{
try {
$master = curl_multi_init();
$requests_count = count($requests);
$curl_arr = array();
for ($i = 0; $i < $requests_count; $i++) {
$request = $requests[$i];
$curl_arr[$i] = $request;
curl_multi_add_handle($master, $curl_arr[$i]);
}
$running = null;
do {
curl_multi_exec($master, $running);
} while ($running > 0);
for ($i = 0; $i < $requests_count; $i++) {
$results[] = curl_multi_getcontent($curl_arr[$i]);
curl_multi_remove_handle($master, $curl_arr[$i]);
}
curl_multi_close($master);
} catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
}
return $results;
}
如何创建每个请求的请求数组
function createFCMCurlRequest($regIds, $title, $message, $image, $shouldOpen, $shopId, $url, $notificationId)
{
// API access key from Google API's Console
define('API_ACCESS_KEY', 'AAAATQ-------------------------------------------');
$registrationIds = $regIds;
$msg = array
(
'body' => $message,
'title' => $title,
// 'Link' => $link
);
$data = Array(
'shouldOpen' => $shouldOpen,
'notificationId' => $notificationId,
'shopId' => $shopId,
);
if ($url != null) {
$data['url'] = $url;
}
$fields = array
(
'registration_ids' => $registrationIds, //$registrationIds,
'notification' => $msg,
'data' => $data,
'content_available' => true,
'priority' => 'high',
);
$headers = array
(
'Authorization: key=' . API_ACCESS_KEY,
'Content-Type: application/json'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://fcm.googleapis.com/fcm/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));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 0);
curl_setopt($ch, CURLOPT_TIMEOUT, 400);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
return $ch;
}
当我发送请求时,最多2K个请求代码可以正常工作,但超过该服务器的返回值,却出现内部服务器错误或超时甚至ERR空响应。