ios push notification sent using .p8 file using php code

Viewed 1445

I have try ios push notification using .p8 file in php 5.6 but give this type error

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

Here My code

$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);
1 Answers

In order to use .p8 files your server has to have HTTP/2 installed with CURL. Easiest way to check this is to create a php_info file and look in the cURL section. By default cURL is not compiled with HTTP/2.

Related