简体   繁体   English

PHP CURL - Apple 推送通知服务器

[英]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.我正在编写代码以使用 PHP Laravel 向 Apple 推送通知服务器 (APN) 发送通知。它在文档中说它需要 HTTP/HPACK header 压缩。

I've tried using cURL我试过使用 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'但 APNS 服务器总是返回“来自服务器的空回复”

I see a few things that could be problematic.我看到了一些可能有问题的事情。

Try adding the following to actually turn your request into a POST request.尝试添加以下内容以实际将您的请求转换为 POST 请求。

curl_setopt($c, CURLOPT_POST, 1);

Your 'path' should also be part of the main URL instead of being added as a header.您的“路径”也应该是主要 URL 的一部分,而不是作为 header 添加。

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

In addition to the previous suggestion, we should also be able to use certificate bases auth system .除了前面的建议,我们还应该能够使用基于证书身份验证系统

Example snippet below:下面的示例片段:

<?php
  
    $ch = curl_init();
  
    curl_setopt($ch, CURLOPT_URL, 'https://path-to-auth-example.com/token');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, "grant_type=client_cert");
  
    curl_setopt($ch, CURLOPT_SSLCERT, "path/to/your/pem-file.pem");
    curl_setopt($ch, CURLOPT_SSLCERTTYPE, "PEM");
  
    $headers = [];
    $headers[] = 'Content-Type: application/x-www-form-urlencoded';
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  
    $result = curl_exec($ch);
    if (curl_errno($ch)) {
        //catch your error wisely
    }
    curl_close($ch);
  
    $result = json_decode($result);
      
    print_r($result);
  
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM