Search code examples
phplaravelhttp-headersphp-curl

PHP CURL - Apple push notification servers


I am writing code to send notifications to the Apple push notification servers (APNs) using PHP Laravel. It says in the documents that it requires HTTP/ HPACK header compression.

I've tried using 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);

but the APNS server always returning 'Empty reply from server'


Solution

  • I see a few things that could be problematic.

    Try adding the following to actually turn your request into a POST request.

    curl_setopt($c, CURLOPT_POST, 1);
    

    Your 'path' should also be part of the main URL instead of being added as a header.

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