【问题标题】:Apple APNS - Uncaught Exception: Curl failed: No URL setApple APNS - 未捕获的异常:Curl 失败:未设置 URL
【发布时间】:2020-02-09 08:27:32
【问题描述】:

当我在本地机器 (MAMP PRO 5.4) 上测试以下代码时,我可以在我的设备上收到推送通知。

  function sendTestMessage(){

            $keyfile = 'MyAuthKey.p8';               
            $keyid = 'MyKeyId';                        
            $teamid = 'MyTeamId';                         
            $bundleid = 'myBundleId';
            $url = 'https://api.development.push.apple.com'; 
            $token = 'device token';          
            $message = '{"aps":{"alert":"Hi there!","sound":"default"}}';

            $key = openssl_pkey_get_private('file://'.$keyfile);

            $header = ['alg'=>'ES256','kid'=>$keyid];
            $claims = ['iss'=>$teamid,'iat'=>time()];

            $header_encoded = $this->base64($header);
            $claims_encoded = $this->base64($claims);

            $signature = '';
            openssl_sign($header_encoded . '.' . $claims_encoded, $signature, $key, 'sha256');
            $jwt = $header_encoded . '.' . $claims_encoded . '.' . base64_encode($signature);

            // only needed for PHP prior to 5.5.24
            if (!defined('CURL_HTTP_VERSION_2_0')) {
              define('CURL_HTTP_VERSION_2_0', 3);
            }

            $http2ch = curl_init();
            curl_setopt_array($http2ch, array(
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_2_0,
            CURLOPT_URL => "$url/3/device/$token",
            CURLOPT_PORT => 443,
            CURLOPT_HTTPHEADER => array(
            "apns-topic: {$bundleid}",
            "authorization: bearer $jwt"
            ),
            CURLOPT_POST => TRUE,
            CURLOPT_POSTFIELDS => $message,
            CURLOPT_RETURNTRANSFER => TRUE,
            CURLOPT_TIMEOUT => 30,
            CURLOPT_HEADER => 1
            ));

            $result = curl_exec($http2ch);
            if ($result === FALSE) {
              throw new Exception("Curl failed: ".curl_error($http2ch));
            }

              $status = curl_getinfo($http2ch, CURLINFO_HTTP_CODE);
              echo $status;
            }

但是,当我在我的服务器(AWS EC2 Ubuntu 16.04)上测试它时,我收到以下错误:

b>Fatal error</b>:  Uncaught Exception: Curl failed: No URL set! in /var/www/html/classes/PushNotification.php:171
Stack trace:
#0 /var/www/html/api/v1/testPush.php(12): PushNotification-&gt;sendTestMessage()
#1 {main}
  thrown in <b>/var/www/html/classes/PushNotification.php</b> on line <b>171</b><br />

我已经在这里检查并尝试了大多数类似的解决方案,但它不起作用。

【问题讨论】:

    标签: php ios curl apple-push-notifications php-curl


    【解决方案1】:

    我希望这可以帮助其他人。我专注于代码,但它与我的服务器有关。我已经从 UBUNTU 16.04 更新到 18.04,这迫使我更新 PHP 并重新安装 cURL。我刚刚使用命令进行了检查:

    curl --version
    

    我以前的服务器有 curl 版本 7.47,而现在它有 libcurl/7.58.0。

    我还注意到 --version 现在在检查先前版本的版本之后返回 OpenSSL/1.1.1。我猜这是与 OpenSSL 有关的问题,因为苹果开发者网站声明:

    Use HTTP/2 and TLS 1.2 or later to establish a connection between your provider server and one of the following servers: Development server: api.sandbox.push.apple.com:443 Production server: api.push.apple.com:443

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-01-05
      • 1970-01-01
      • 2016-05-19
      • 2012-05-31
      相关资源
      最近更新 更多