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

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 get 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 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);
+6
source share
4 answers

I got the same answer Unexpected HTTP/1.x request: POST /3/device/XXXXwhen using cURL in PHP, but never used curl on the command line.

Just restart the apache web server using sudo apachectl restart, fixing the problem.

0
source

Problem:

, OpenSSL nghttp2 curl / . , PHP ( 7.0) .

, curl OpenSSL nghttp2, :

curl -V

OpenSSL nghttp2, HTTP/1.x.

AWS EC2 "Amazon Linux 2 AMI". OpenSSL nghttp2, , . .

:

AWS EC2 "Amazon Linux AMI 2018.03.0 (HVM)".

. "Amazon Linux 2 AMI (HVM)", .

PHP7 : PHP 7 EC2 t2.micro Amazon Linux Distro

curl, OpenSSL nghttp2:

curl -V

, !

. PHP, , : push- iOS php .p8

0

PHP, phpinfo() , HTTP2 curl. PHP .

0

In addition to what @AndyW said, if you are not using AWS, this can help you: https://askubuntu.com/questions/884899/how-do-i-install-curl-with-http2-support

0
source

Source: https://habr.com/ru/post/1666691/


All Articles