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

我们两清 提交于 2019-12-01 00:47:31

问题


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);

回答1:


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.




回答2:


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




回答3:


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.



来源:https://stackoverflow.com/questions/47646162/unexpected-http-1-x-request-post-3-device-xxxx

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!