php - 使用 .p8 文件使用 php 代码发送 ios 推送通知

标签 php apple-push-notifications

我已经尝试使用 php 5.6 中的 .p8 文件进行 ios 推送通知 但给出这种类型的错误

@@uUnexpected HTTP/1.x request: POST /3/device/

这是我的代码

$keyfile = 'C:/xampp/htdocs/aromanew/apns.p8';
                    $keyid = 'B6U2K4SKZP';
                    $teamid = 'BD88DGKDD2';
                    $bundleid = 'com.gatewaytechnolabs.aromaDemo';
                    $url = 'https://api.development.push.apple.com';
                    $token = 'DB347143360A0C70F5B69A424979243D01568073144C4408E99B7E2FC7C3D36E';   
                    $message = '{"aps":{"alert":"Congratulations New Message Has been Published","sound":"default"}}';

                    $key = openssl_pkey_get_private('file://'.$keyfile);

                    $header = ['alg'=>'ES256','kid'=>$keyid];
                    $claims = ['iss'=>$teamid,'iat'=>time()];
                    $header_encoded = rtrim(strtr(base64_encode(json_encode($header)), '+/', '-_'), '=');
                    $claims_encoded = rtrim(strtr(base64_encode(json_encode($claims)), '+/', '-_'), '=');
                    $signature = '';
                     openssl_sign($header_encoded . '.' . $claims_encoded, $signature, $key, 'sha256');
                    $jwt = $header_encoded . '.' . $claims_encoded . '.' . base64_encode($signature);


                    // only needed for PHP prior to 5.5.24
                    if (!defined('CURL_HTTP_VERSION_2_0')) {
                          define('CURL_HTTP_VERSION_2_0', 3);
                      }

                    $http2ch = curl_init();
                    curl_setopt_array($http2ch, array(
                        CURLOPT_URL => "$url/3/device/$token",
                        CURLOPT_PORT => 443,
                        CURLOPT_HTTPHEADER => array(
                          "apns-topic: {$bundleid}",
                          "authorization: bearer $jwt"
                        ),
                        CURLOPT_POST => TRUE,
                        CURLOPT_POSTFIELDS => $message,
                        CURLOPT_RETURNTRANSFER => TRUE,
                        CURLOPT_TIMEOUT => 30,
                        CURLOPT_HEADER => 1
                    ));

                $result = curl_exec($http2ch);

                if ($result === FALSE) {
                    throw new Exception("Curl failed: ".curl_error($http2ch));
                }

                    $status = curl_getinfo($http2ch, CURLINFO_HTTP_CODE);

最佳答案

为了使用 .p8 文件,您的服务器必须安装带有 CURL 的 HTTP/2。检查这一点的最简单方法是创建 php_info 文件并查看 cURL 部分。默认情况下,cURL 不使用 HTTP/2 进行编译。

关于php - 使用 .p8 文件使用 php 代码发送 ios 推送通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47470555/

相关文章:

javascript - 将 json 数据从 php 返回到 ajax

php - 使用 mysql 中的值填充选择输入字段

iOS 推送通知声音不会在打开时取消

ios - Apple Push Notification - 所有消息都已发送但并非所有消息都已送达

php - Woocommerce - 添加到购物车触发器

PHP & mySQL : Year 2038 Bug: What is it? 如何解决?

php - 将用户添加到不和谐公会会生成未经授权的错误

php - 使用 Api 使用推送通知

ios - APNS - 如何连接到反馈服务

objective-c - 找不到适用于应用程序的有效 'aps-environment' 权利字符串