【问题标题】:PHP CURL - Apple push notification serversPHP CURL - Apple 推送通知服务器
【发布时间】:2021-02-08 03:16:19
【问题描述】:

我正在编写代码以使用 PHP Laravel 向 Apple 推送通知服务器 (APN) 发送通知。它在文档中说它需要 HTTP/HPACK 标头压缩。

我尝试过使用 cURL

            $cURLConnection = curl_init();
            if(strcmp(env('APP_ENV'), 'production') == 0) {
                curl_setopt($cURLConnection, CURLOPT_URL, 'api.push.apple.com:443');
            } else {
                curl_setopt($cURLConnection, CURLOPT_URL, 'api.development.push.apple.com:443');
            }
            curl_setopt_array($cURLConnection, [
                CURLOPT_RETURNTRANSFER =>true,
                CURLOPT_HTTP_VERSION   =>CURL_HTTP_VERSION_2_0,
            ]);

            curl_setopt($cURLConnection, CURLOPT_HTTPHEADER, array(
                'path: /3/device/<devicetoken>',
                'authorization: bearer ' . $token,
                'Content-Type: application/json'
            ));
            curl_setopt($cURLConnection, CURLOPT_POSTFIELDS, $postRequest);
            curl_setopt ($cURLConnection, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2);
            $apiResponse = curl_exec($cURLConnection);

但 APNS 服务器总是返回“来自服务器的空回复”

【问题讨论】:

标签: php laravel http-headers php-curl


【解决方案1】:

我看到了一些可能有问题的事情。

尝试添加以下内容以将您的请求实际转换为 POST 请求。

curl_setopt($c, CURLOPT_POST, 1);

您的“路径”也应该是主 URL 的一部分,而不是作为标题添加。

curl_setopt($c, CURLOPT_URL, "https://api.push.apple.com:443/3/device/<devicetoken>");

【讨论】:

  • 谢谢。现在可以走了。所以 HPACK 实际上是由 cURL 处理的?
  • 说实话,我实际上对 HPACK 并不了解,所以我很高兴它起作用了 :D 我刚刚注意到你使用的 URL 错误。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多