php - 在 php 中正确放置和命名 .pem 文件以用于苹果通知的位置

标签 php mysql ios cron

我的 cron 文件位于根目录的 cron 文件夹下。但它总是显示错误 -

Warning: stream_socket_client(): SSL: crypto enabling timeout in /home/myname/beta.myhost.com/cron/dailycron.php on line 43

Warning: stream_socket_client(): Failed to enable crypto in /home/myname/beta.myhost.com/cron/dailycron.php on line 43

Warning: stream_socket_client(): unable to connect to ssl://gateway.sandbox.push.apple.com:2195 (Unknown error) in /home/myname/beta.myhost.com/cron/dailycron.php on line 43

43 行大约是 -

$apns = stream_socket_client('ssl://' . $apns_url . ':' . $apns_port, $error, $error_string, 2, STREAM_CLIENT_CONNECT, $stream_context);

我的代码如下-

<?php

//############################# Database connection ################################/

//######################### Daily notification code ############################################/

$result = mysql_query("SELECT DISTINCT EntryId, twittes, groupname, created_at FROM users_twitte NATURAL JOIN (SELECT EntryId, MAX( created_at ) created_at FROM users_twitte GROUP BY EntryId) t");
while( $row = mysql_fetch_array($result) ) {

  // Get user array from EntryId
  $rowdata = $row['EntryId'];

  $result2 = mysql_query("SELECT * FROM `users_subscription` WHERE EntryId = $rowdata");

  while( $row2 = mysql_fetch_assoc($result2) ) {
    $group_name = $row['groupname'];
    $grp_usr_id = $row2['uid'];
    send_notification($group_name, $grp_usr_id);
  }
}

// Send Notification
function send_notification($groupname, $uid) {

    // GET device token from uid
    //$device_token = get_device_token($uid);   

    $payload = array();
    $payload['aps'] = array('alert' => "THERE IS AN MESSAGE FROM $groupname", 'badge' => 0, 'sound' => 'default');
    $payload = json_encode($payload);

    $apns_url = NULL;
    $apns_cert = NULL;
    $apns_port = 2195;

    $apns_url = 'gateway.sandbox.push.apple.com';
    $apns_cert = '/home/myname/beta.myhost.com/cron/ck.pem';

    $stream_context = stream_context_create();
    stream_context_set_option($stream_context, 'ssl', 'local_cert', $apns_cert);
    stream_context_set_option($stream_context, 'ssl', 'passphrase', "12345");

    $apns = stream_socket_client('ssl://' . $apns_url . ':' . $apns_port, $error, $error_string, 2, STREAM_CLIENT_CONNECT, $stream_context);

    $get_token_info = get_device_token_and_type($uid);
    if( 1 === $get_token_info['status'] ) {
        if(0 === $get_token_info['device_type']) {
            $device_token = $get_token_info['$device_token'];   
            $apns_message = chr(0) . chr(0) . chr(32) . pack('H*', str_replace(' ', '', $device_token)) . chr(0) . chr(strlen($payload)) . $payload;
        }
        fwrite($apns, $apns_message);
        @socket_close($apns);
        @fclose($apns);
    }
}

// GET device token for uid
function get_device_token_and_type($uid) {
    $dev_tok_type_arr = array();
    // GET EMAIL id from uid
    $result = mysql_query("SELECT LoginData.device_token, LoginData.device_type FROM LoginData INNER JOIN users ON LoginData.userid = users.email WHERE users.uid =$uid");
    if( mysql_num_rows($result) > 0 ) {
        while( $rows = mysql_fetch_assoc($result) ) {
            $dev_tok_type_arr = array('device_token'=> $rows['device_token'], 'device_type'=> $rows['device_type'], 'status'=> 1);
        }
    }
    else {
        $dev_tok_type_arr = array('status'=>0, 'message'=> 'No device token exists for the userid');
    }
    return $dev_tok_type_arr;
}

?>

我的 .pem 文件在同一个 cron 文件夹中,我在 - $apns_cert 提到了绝对路径。让我知道我做错了什么,我检查了正确的密码。

最佳答案

只需将您的 pem 文件放在您的 php 脚本的同一文件夹中,并将您的变量更改为:

$apns_cert = 'ck.pem';

关于php - 在 php 中正确放置和命名 .pem 文件以用于苹果通知的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26222083/

相关文章:

php - 无法创建具有空登录名的用户

mysql - 由于在 MySQL 中使用保留字作为表名或列名而导致的语法错误

ios - iPad Split View尺寸零件

ios - 清除 UIImageView 触摸绘制的内容

php - 如果页面包含 2 个提交按钮,如何构建页面

php - 如何使用 PHP 解析序列化数据?

php - python 纹 : download file accessible through PHP script

php - 有没有比 VALUES($var1, $var2, $var3) 更好的方法向 MySQL 查询插入值?

mysql - 如何在 mysql 中按字段和自动 ID 进行分组

ios - 启用 Facebook 重大更改 2013 年 2 月 : login Does Not Work