我正在使用亚马逊 PHP SDK V3.13.1,我必须在 30 秒内发送至少 10,000 条推送通知。现在我正在使用publishAsync
它更快的方法,但我仍然没有及时发送。
所以我已经实现了套接字并每次发送一堆 3500 推送。以下是我发送套接字请求的控制器功能。
$parts = parse_url(base_url() . "welcome/send_signal");
for ($i = 1; $i <= 10000; $i++) {
$device_type_ids_arr[$i]['token_id'] = "User token";
$device_type_ids_arr[$i]['arn'] = "User arn";
$device_type_ids_arr[$i]['member_id'] = $i;
if ((count($device_type_ids_arr) == 3500) || $i == 10000) {
$postData['devices'] = $device_type_ids_arr;
$postData['pushSet'] = $pushSet;
$postData['push_content'] = $push_content;
$post_string = http_build_query($postData);
$device_type_ids_arr = array();
$fp = fsockopen($parts['host'], isset($parts['port']) ? $parts['port'] : 80, $errno, $errstr, 600);
if (!$fp) {
echo "Some thing Problem";
}
$out = "POST " . $parts['path'] . " HTTP/1.1\r\n";
$out .= "Host: " . $parts['host'] . "\r\n";
$out .= "User-Agent: " . $_SERVER['HTTP_USER_AGENT'] . "\r\n";
$out .= "Content-Type: application/x-www-form-urlencoded\r\n";
$out .= "Content-Length: " . strlen($post_string) . "\r\n";
$out .= "Connection: Close\r\n\r\n";
$out .= $post_string;
fwrite($fp, $out);
fclose($fp);
}
}
以下是我的函数,它接收套接字数据并发送推送通知。
$sns = new Aws\Sns\SnsClient(array(
'version' => 'latest',
'key' => "my sns key",
'secret' => "secret",
'region' => "region",
'profile' => "amazon_user_profile",
'debug' => false,
'http' => array('verify' => false)
));
foreach ($device_id_arr as $device_detail) {
try {
$promises[] = $sns->publishAsync(array(
'Message' => '{ "GCM": "{\"data\": { \"message\": \"Hello user\" } }"}',
'MessageStructure' => 'json',
'TargetArn' => "member sns arn"
));
} catch (Exception $e) {
$errorMsg = $e->getMessage();
}
}
$results = \GuzzleHttp\Promise\settle($promises)->wait(TRUE);
$fp = fopen("test_parallel.txt", "a+");
fwrite($fp, "result:" . print_r($results, true) . "\r\n");
fclose($fp);
当我发送 10 条通知时,这工作正常,但是当我发送 3500 次推送时,它不起作用并且没有给我任何回应。我也试过这个方法。带有 Guzzle 的 MultiCurl 的 Amazon AWS PHP 开发工具包?但它给了我错误Argument 1 passed to Aws\AwsClient::execute() must implement interface Aws\CommandInterface, array given
$sns = new Aws\Sns\SnsClient(array(
'version' => 'latest',
'key' => "my sns key",
'secret' => "secret",
'region' => "region",
'profile' => "amazon_user_profile",
'debug' => false,
'http' => array('verify' => false)
));
foreach ($device_id_arr as $device_detail) {
try {
$publishCommands[] = $sns->getCommand('Publish', array(
"Message" => '{ "GCM": "{\"data\": { \"message\": \"' . $push_content . '\", \"type\": \"' . PUSH_TYPE_SIGNAL . '\" } }"}',
"MessageStructure" => "json",
"TargetArn" => $device_detail['arn']
));
} catch (Exception $e) {
$errorMsg = $e->getMessage();
}
}
try {
$successfulCommands = $sns->execute($publishCommands);
$failedCommands = array();
} catch (\Guzzle\Service\Exception\CommandTransferException $e) {
$successfulCommands = $e->getSuccessfulCommands();
$failedCommands = $e->getFailedCommands();
}
foreach ($failedCommands as $failedCommand) {
$fp = fopen("test_parallel4.txt", "a+");
fwrite($fp, "result:" . print_r($result, true) . "\r\n");
fclose($fp);
}
$messageIds = array();
foreach ($successfulCommands as $successfulCommand) {
$messageIds[] = $successfulCommand->getResult()->get('MessageId');
}
那么有人对此有解决方案吗?我主要关心的是在 30 秒内发送数千条推送通知。