PHP:504网关超时错误?

时间:2017-09-04 16:51:17

标签: php mysql

我知道这个问题已被问过几次,但我正在寻找另一个问题的答案。

基本上,我有一个MYSQL数据库,其中有超过10000条iPhone的设备ID记录,用于推送通知。

现在,我需要运行一个PHP文件,该文件将连接到Apple APNS服务器,以便向我的MYSQL数据库中的这10000条记录发送推送通知。

当我运行我的php页面时,过了一会儿我得到一个超时错误...

在我的研究中,我遇到了一些解决方案,但大多数都要求在服务器上的etc文件夹中编辑一些东西。

我的问题是:

有没有办法选择让MYSQL数据库中的30条记录,然后停止然后选择另一条30然后停止然后再停止30条,依此类推,直到谁的记录被发送到PN?

这是我目前的代码:

$message = "Welcome";

$sqld = "SELECT * FROM pushUsers ORDER BY id DESC";
$queryd = mysqli_query($db_conx, $sqld);
$productCountd = mysqli_num_rows($queryd); // count the output amount
if ($productCountd > 0) {
    //while($rowd = mysqli_fetch_array($queryd, MYSQLI_ASSOC)){ 
        while($rowd = mysqli_fetch_assoc($queryd)){ 
             $deviceID = $rowd["deviceNo"];
             $deviceType = $rowd["deviceType"];

             if($deviceType == 'iPhone' || $deviceType == 'iPad'){

 ///////////////SEND PUSH NOTU8FOCATION FOR iOS//////////////////////

              // Put your device token here (without spaces):

   $deviceToken = ''. $deviceID.'';


    // Put your private key's passphrase here:
    $passphrase = '123456';

    ////////////////////////////////////////////////////////////////////////////////

    $ctx = stream_context_create();
    stream_context_set_option($ctx, 'ssl', 'local_cert', 'apple_push_notification_production.pem');
    stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);

    // Open a connection to the APNS server
    //****IMPORTANT**** LIVE APNS URL: 

    //ssl://gateway.sandbox.push.apple.com:2195


    $fp = stream_socket_client(
      'ssl://gateway.push.apple.com:2195', $err,
      $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);

    if (!$fp)
      exit("Failed to connect: $err $errstr" . PHP_EOL);

    echo 'Connected to APNS' . PHP_EOL;

    // Create the payload body
    $body['aps'] = array(
        'alert' => ''.$message.'',
        'sound' => 'default',
        'link_url' => 'http://xxxx.xom',
        'category' => 'APP',
        'badge'  => '1',
    );

    // Encode the payload as JSON
    $payload = json_encode($body);

    // Build the binary notification
    $msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;

    // Send it to the server
    $result = fwrite($fp, $msg, strlen($msg));

    if (!$result)
      echo 'Message not delivered' . PHP_EOL;
    else
      echo 'Message successfully delivered' . PHP_EOL;

    // Close the connection to the server
    fclose($fp);

    ///////////////END SEND PUSH NOTU8FOCATION FOR iOS//////////////////////

}


    }
} else {
}

任何帮助都会受到赞赏。

提前致谢。

2 个答案:

答案 0 :(得分:0)

有几种方法可以解决您的问题。

我无法帮你编写代码,相反我会写一些概念性提示。

更改PHP中的最长执行时间

使用set_time_limit()来增加执行时间限制。

但是,如果您的PHP在safe_mode中运行,则此函数无效。

拆分你的工作

这样做取决于你如何调用这个PHP文件,如果它是一个cron作业,你可以保留推送通知的最后一个索引,然后多次调用cron作业。

提高性能

推送服务应该有一个批量方法,您可以在一个http请求中发送许多通知。 HTTP请求是这里的瓶颈,而不是数据库。

每个请求都有自己的延迟,如果数据库中有10k行,则会有10k个请求。如果请求需要10毫秒(非常短的时间),您将需要100秒来执行脚本。

如果您以批量方式发送通知,每个请求接受100个通知并运行50毫秒,则只需要100个请求,并且只需5秒钟即可执行。

这样,您可以使用LIMITOFFSET拆分mysql查询,这样您只需在发送请求之前在内存中加载。

答案 1 :(得分:0)

是的,您需要立即发送100-100或500-500的推送通知,因为您的服务器可能不会一次性强大10,000记录进程,其他原因可能是您的执行时间为30秒或像这样的东西,所以一个脚本运行特定的时间,然后它给予超时。

$message = "Welcome";
for($i=10000 ;$i>0;$i-$limit){
//your code here
sleep(2);
}