浏览器上的推送通知在命令行上不起作用

Push notification work on browser doesn't on commandline

我有 php 文件用于 运行ning apns。当我在浏览器上调用网页时它确实有效。但是它不能在终端上调用

php deneme.php

我确认 .pem 文件已正确创建。并且还要确定它在 php 文件中的位置也是正确的(因为在浏览器上可以正常工作)。我运行 out with option。希望大家出出主意。

deneme.php 文件是:

<?php
    $apnsServer = 'ssl://gateway.sandbox.push.apple.com:2195';

    $privateKeyPassword = 'MyPassword123';
    $message = 'Test Push Notifications';

    $deviceToken =
    '2877b691ffd9a3edfa45ee31ff25083f1845e016e7902d130eb09713b1c2ed2f';

    $pushCertAndKeyPemFile =  $_SERVER['DOCUMENT_ROOT'].'/ck.pem';// 'ck.pem';
    $stream = stream_context_create();
    stream_context_set_option($stream,
        'ssl',
        'passphrase',
        $privateKeyPassword);
        stream_context_set_option($stream,
        'ssl',
        'local_cert',
        $pushCertAndKeyPemFile);

    $connectionTimeout = 20;
    $connectionType = STREAM_CLIENT_CONNECT | STREAM_CLIENT_PERSISTENT;
    $connection = stream_socket_client($apnsServer,
    $errorNumber,
    $errorString,
    $connectionTimeout,
    $connectionType,
    $stream);
    if (!$connection){
        echo "Failed to connect to the APNS server. Error no = $errorNumber<br/>";
        exit;
    } else {
        echo "Successfully connected to the APNS. Processing...</br>";
    }
    $messageBody['aps'] = array('alert' => $message,
        'sound' => 'default',
        'badge' => 2,
        );
    $payload = json_encode($messageBody);
    $notification = chr(0) .
    pack('n', 32) .
    pack('H*', $deviceToken) .
    pack('n', strlen($payload)) .
    $payload;
    $wroteSuccessfully = fwrite($connection, $notification, strlen($notification));
    if (!$wroteSuccessfully){
        echo "Could not send the message<br/>";
    }
    else {
        echo "Successfully sent the message<br/>";
    }
    fclose($connection);
?>

当从 CLI 运行 时,没有 $_SERVER[] 可用的超全局变量,因为没有服务器 运行。将 $_SERVER['DOCUMENT_ROOT'].'/ck.pem'; 替换为 ck.pem 文件的绝对路径。