code igniter - 连接失败:发送 ios 通知时为 0

code igniter - Failed to connect: 0 while sending ios notifications

我正在尝试从 PHP Code Igniter 框架向 iOS APNS 发送远程推送通知。

为此,作为一种练习,我已经编写了核心文件并使用相同的帐户,当我在控制器函数中添加相同的代码时,它 运行 很好,它给了我:

"Failed to connect: 0".

经过大量研究后,我了解到该行:

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

为什么相同的代码在核心 PHP 中运行但在 CI 中却不行?

我试着给出我的 .pem 文件的完整路径,如此处所示 Failed to connect: 0 push notification

但运气不好。

请帮助我摆脱这个令人沮丧的问题。

public function sendIosPushNotifications()
{
        // set time limit to zero in order to avoid timeout
        set_time_limit(0);

        // charset header for output
        header('content-type: text/html; charset: utf-8');

        // this is the pass phrase you defined when creating the key
        $passphrase = '';

        // you can post a variable to this string or edit the message here
        if (!isset($_POST['msg'])) {
        $_POST['msg'] = "Notification message here from gokul sxsxsxs!";
        }

        // tr_to_utf function needed to fix the Turkish characters
        $message = $this->tr_to_utf($_POST['msg']);

        // load your device ids to an array
        $deviceIds = array(
        '6a774e1779d1c1932c112ff7a45c337db96605942acbdb45d5c386a329bda381'
        );

        // this is where you can customize your notification
        $payload = '{"aps":{"alert":"' . $message . '","sound":"default"}}';

        $result = 'Start' . '<br />';


        // start to create connection
        $ctx = stream_context_create();
        stream_context_set_option($ctx, 'ssl', 'local_cert', 'Certificates123.pem');
        //stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);

        echo count($deviceIds) . ' devices will receive notifications.<br />';

        foreach ($deviceIds as $item) {
            // wait for some time
            sleep(1);

            // Open a connection to the APNS server
           $fp = stream_socket_client('ssl://gateway.sandbox.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT | STREAM_CLIENT_PERSISTENT, $ctx);

            if (!$fp) {
                exit("Failed to connect: $err" . '<br />');
            } else {
                echo 'Apple service is online. ' . '<br />';
            }

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

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

            if (!$result) {
                echo 'Undelivered message count: ' . $item . '<br />';
            } else {
                echo 'Delivered message count: ' . $item . '<br />';
            }

            if ($fp) {
                fclose($fp);
                echo 'The connection has been closed by the client' . '<br />';
            }
        }

        echo count($deviceIds) . ' devices have received notifications.<br />';

        // function for fixing Turkish characters


        // set time limit back to a normal value
        set_time_limit(30);
   }

这是路径问题...将您的证书文件放在 CI 根目录中.. 并提供类似 ./certificate/filename.pem 的路径,它有效