Home » Php » php – Unexpected HTTP/1.x request: POST /3/device/XXXX

php – Unexpected HTTP/1.x request: POST /3/device/XXXX

Posted by: admin July 12, 2020 Leave a comment

Questions:

Hi i am sending IOS push notifications using apns and curl in php and getting this error message.

I also define ‘CURL_HTTP_VERSION_2_0’ but still getting this error:

Unexpected HTTP/1.x request: POST /3/device/

Here is my code:

       $key_file = XXXXXX';
       $secret = null; 
       $private_key = JWKFactory::createFromKeyFile($key_file, $secret, [
                'kid' => '3W6B5LQQHX',
                'alg' => 'ES256',
                'use' => 'sig',
    ]);

    $payload = [
        'iss' => 'RUK725A7V4',
        'iat' => time(),
    ];

    $header = [
        'alg' => 'ES256',
        'kid' => $private_key->get('kid'),
    ];

    $jws = JWSFactory::createJWSToCompactJSON(
                    $payload, $private_key, $header
    );


    if (!defined('CURL_HTTP_VERSION_2_0')) {
        define('CURL_HTTP_VERSION_2_0', 3);
    }

        $http2_server = 'https://api.development.push.apple.com'; 
        $app_bundle_id = 'com.MD.example';

        $token = $device->device_id;
        $url = "{$http2_server}/3/device/{$token}";

        // headers
        $headers = array(
            "apns-topic: {$app_bundle_id}",
            'Authorization: bearer ' . $jws
        );

This is the curl actual curl request.

         // other curl options
        curl_setopt_array($http2ch, array(
            CURLOPT_URL => $url,
            CURLOPT_PORT => 443,
            CURLOPT_HTTPHEADER => $headers,
            CURLOPT_POST => TRUE,
            CURLOPT_POSTFIELDS => $data,
            CURLOPT_RETURNTRANSFER => TRUE,
            CURLOPT_TIMEOUT => 30,
            CURLOPT_SSL_VERIFYPEER => false,
            CURLOPT_HEADER => 1,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_2_0,
        ));
        $result = curl_exec($http2ch);
How to&Answers:

I was getting the same Unexpected HTTP/1.x request: POST /3/device/XXXX response when using cURL in PHP but never when using curl on the command line.

Simply restarting the apache webserver with sudo apachectl restart fixed the problem.

Answer:

Problem:

For me this was because OpenSSL and nghttp2 were missing from curl and/or my server. I also believe an older version of PHP (less than 7.0) could also cause this issue.

You can check if curl is using OpenSSL and nghttp2 using the following terminal command on your server:

curl -V

If the output does not mention OpenSSL and nghttp2 then this is why you are seeing the HTTP/1.x error.

I was using the AWS EC2 instance ‘Amazon Linux 2 AMI’. I could not find a way to install OpenSSL or nghttp2 so I had to create a new server, see below.

Solution:

I had to create a new AWS EC2 instance of ‘Amazon Linux AMI 2018.03.0 (HVM)’.

Note: Do not create instance of ‘Amazon Linux 2 AMI (HVM)’ as you will end up with the original issue.

I then followed this to install PHP7 on the new server:
How to install PHP 7 on EC2 t2.micro Instance running Amazon Linux Distro

I then checked curl version, OpenSSL and nghttp2 were both present:

curl -V

I then put all the code and resources onto the new server and it worked!

Note: The actual PHP code I used to create the notification can be found here:
Send iOS Push notification in php with .p8 file

Answer:

I was using an older version of PHP where phpinfo() showed that HTTP2 wasn’t enabled for the curl library. Upgrading PHP to a more recent version fixed this issue for me.

Answer:

Kindly enable your http2 in curl package of your php.

Also see the phpinfo() if you have already enabled the http2 support for curl or not.

After that just set your CURLOPT_HTTP_VERSION curl option with 3

enter image description here