php - 我应该将 .pem 文件放在 codeigniter 的什么位置以发送 Apple 推送通知?

标签 php ios codeigniter path apple-push-notifications

我正在使用 REST_controller 库在 CodeIgniter 中开发 Web 服务。它只是一个管理请求的 Controller ,我使用的每种方法都有一个请求。我尝试使用其中一种方法来发送苹果通知,所以我需要使用 ck.pem 文件(它是一个证书)。我知道问题是我没有 .pem 文件的正确路径。

这是我正在使用的代码。我在 Controller 文件夹中有 ck.pem 文件,但它不起作用。

function pushtest_get(){
        $data = array();
    // Put your device token here (without spaces):
        $deviceToken = 'be67171fb22f3ea8fa68c13a9f2ba7229caf3a487aa656b20f769fa2b1fa5b7a';
     // Put your private key's passphrase here:
        $passphrase = 'miguel';
        $message = 'Notification de prueba desde servicio web rest';
    ////////////////////////////////////////////////////////////////////////////////
        $ctx = stream_context_create();
        stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');
        stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
     // 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 $errstr" . PHP_EOL);

        $data['response'] = 'Connected to APNS' . PHP_EOL;

    // Create the payload body
        $body['aps'] = array(
            'alert' => $message,
            'sound' => 'default'
            );

    // 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)
           $data['response'] = 'Message not delivered' . PHP_EOL;
        else
            $data['response'] = 'Message successfully delivered' . PHP_EOL;

    // Close the connection to the server
        fclose($fp);
        $response->skills = $data;
        $this->response($response, 200);
    } 

最佳答案

你的问题可能已经解决了一个多星期了,但我今天遇到了同样的问题,所以我想分享一下。

对于 CodeIgniter 项目,您可以将 ck.pem 文件与 application/目录保持平行,然后下面的代码将按原样运行。

stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');

关于php - 我应该将 .pem 文件放在 codeigniter 的什么位置以发送 Apple 推送通知?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19406093/

相关文章:

php - 我的 Magento 扩展安装脚本将无法运行

javascript - 当我单击更新按钮时,它不会更改/更新(CodeIgniter,更新多行)

php - 使用 SimpleXML 读取 RSS 提要时出现问题

php - MVC架构中,密码加密应该在哪里进行

php - 通过 php 向数据库提供信息

php - 更改本地日期会影响 mysql 中的保存日期

php - 统计文章中的评论数

ios - 自动布局:元素未安装用于不同的方向

ios - XCode 6 verificationController.m 问题

ios - KeychainItemWrapper 链接错误