PHP:在套接字服务器内发送apns不起作用?

 mobiledu2502921803 发布于 2023-02-05 11:45

本周我尝试使用php套接字,但有一件事似乎不起作用.

我想从套接字服务器发送Apple推送通知(apns).套接字服务器工作正常但打开另一个套接字向apns发送消息不起作用.

我使用的服务器套接字脚本类似于php.net的示例.(http://www.php.net/manual/en/sockets.examples.php)我用来发送apns通知的脚本是以下功能:

function sendToAPNS($data, $deviceTokens, $pem, $password, $debug=0)
{
    $server = ($debug == 1) ? 'gateway.sandbox.push.apple.com' : 'gateway.push.apple.com';

    $ctx = stream_context_create();
    stream_context_set_option($ctx, 'ssl', 'local_cert', '/home/testing/pushNotifications/'.$pem);
    stream_context_set_option($ctx, 'ssl', 'passphrase', $password);

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

    if (!$fp)
    {
        echo 'APNS error:'. $err .' '. $errstr ."\n";
        return;
    }

    $body['aps'] = array('alert' => $data['message']);

    foreach($data AS $key => $value)
    {
        if($key != 'message') $body['aps'][$key] = $value;
    }       

    $payload = json_encode($body);

    for($i = 0,$max=count($deviceTokens); $i < $max; $i++) 
    {
        $msg = chr(0) . pack('n', 32) . pack('H*', $deviceTokens[$i]) . pack('n', strlen($payload)) . $payload;
        $result = fwrite($fp, $msg, strlen($msg));

        if (!$result)
            BPLog('Message['. $data['ID'] .'] not delivered to '. $deviceTokens[$i] .': '. PHP_EOL);

       // ob_flush();
       // flush();
    }

    fclose($fp);

    return true;        
}

显示以下警告:

PHP Warning:  stream_socket_client(): SSL operation failed with code 1. OpenSSL Error messages:
error:14094410:SSL routines:func(148):reason(1040) in /home/testing/pushNotifications/index.php on line 162

Warning: stream_socket_client(): SSL operation failed with code 1. OpenSSL Error messages:
error:14094410:SSL routines:func(148):reason(1040) in /home/testing/pushNotifications/index.php on line 162
PHP Warning:  stream_socket_client(): Failed to enable crypto in /home/testing/pushNotifications/index.php on line 162

Warning: stream_socket_client(): Failed to enable crypto in /home/testing/pushNotifications/index.php on line 162
PHP Warning:  stream_socket_client(): unable to connect to ssl://gateway.push.apple.com:2195 (Unknown error) in /home/testing/pushNotifications/index.php on line 162

Warning: stream_socket_client(): unable to connect to ssl://gateway.push.apple.com:2195     (Unknown error) in /home/testing/pushNotifications/index.php on line 162

但是该功能在其他环境中工作.我已经在一个php文件中对它进行了测试,该文件可以从浏览器中调用,它会像它应该的那样发送.我使用证书的绝对路径,这不是问题.

也许在php中存在差异,因为从命令行调用套接字服务器.或者也许根本无法从套接字服务器中打开另一个套接字流?

有谁知道问题可能是什么?

撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有