PHP 代码点火器 使用 curl for iOS 推送通知


php code igniter using curl for ios push notifications

我已经做了网络服务,使用 curl 向 ios 发送推送通知,我有用于开发的 ck.pem 文件,其中包含证书和 RSA 私钥,并正确引用它。

但是每次我调用网络服务时,我都会收到相同的错误curl 失败:无法使用客户端证书(找不到密钥或密码短语错误?

所有相关解决方案都不起作用,除了使用"stream_context_create"的替代方案,但我想用 curl 和 idk 来做,问题出在哪里。

在下面找到我的代码:

function test_push_to_ios() {
    $url = 'https://gateway.sandbox.push.apple.com:2195';
    $cert = base_url() . 'backend_includes/ios_cert/ck.pem';
    $gcm_ids = array("xxxxxx");
    $passphrase = "passphrase";
    $message = 'nbad_notification';
    $aps = array('alert' => $message, 'sound' => 'default');
    $fields = array('device_tokens' => $gcm_ids, 'data' => $message, 'aps' => $aps);
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json"));
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_SSLCERT, $cert);
    //curl_setopt($ch, CURLOPT_SSLCERTPASSWD, $passphrase);
    curl_setopt($ch, CURLOPT_SSLKEY, $cert);
    curl_setopt($ch, CURLOPT_SSLKEYPASSWD, $passphrase);
    curl_setopt($ch, CURLOPT_CERTINFO, TRUE);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
    $result = curl_exec($ch);
    if ($result === FALSE) {
        die('Curl failed: ' . curl_error($ch));
    }
    curl_close($ch);
    echo json_encode($result);
}

我没有仔细阅读你的问题。

您正在尝试通过HTTPS请求向Apple发送推送通知。那行不通。Apple 推送通知仅适用于通过 TCP 协议的特定二进制格式。

作为提供商,您可以通过二进制接口与 Apple 推送通知服务进行通信。此接口是面向提供商的高速、高容量接口;它使用流式 TCP 套接字设计与二进制内容相结合。二进制接口是异步的。

你的代码有很多问题:

您似乎将GCM代码与APNS代码混合在一起。 $fields = array('device_tokens' => $gcm_ids, 'data' => $message, 'aps' => $aps);看起来类似于您向 Google Cloud 消息传递服务器发送消息时执行的操作。但是GCM与APNS完全不同,那么你为什么认为它会起作用呢?

您正在发送一个 JSON 正文,这是适用于 GCM 的,但 APNS 使用二进制格式。虽然发往 APNS 的二进制消息中的有效负载包含编码的 JSON 字符串(看起来类似于您的 $aps JSON),但您不能将其打包到另一个 JSON 中并期望它正常工作。

在APNS服务器前面添加https://并不能使其支持HTTPS,因为它没有实现为支持HTTPS(也不是HTTP)。

我建议你使用stream_context,它有效。