Unexpected HTTP/1.x request: POST /3/device/XXXX
Asked Answered
P

5

10

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);
Pulse answered 5/12, 2017 at 4:35 Comment(2)
Did you ever find a solution? I'm currently facing this exact issue and can't really find any other solutionsElastance
Same here, have you found how to fix it?Quotidian
S
0

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.

Shipload answered 28/12, 2017 at 12:47 Comment(0)
I
0

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

Interleaf answered 13/3, 2019 at 17:9 Comment(0)
H
0

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.

Hyssop answered 13/3, 2019 at 17:26 Comment(0)
L
0

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

Lonnielonny answered 29/1, 2020 at 13:37 Comment(1)
How to enable it if it is not enabled?Phobos
I
0

I had this same error until I switch the HTTP version

CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_2TLS
Importunate answered 14/6, 2023 at 18:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.