在没有端口2195的情况下发送iOS推送通知


Send iOS push notification without port 2195

以前曾问过这个问题,但在答案中,代码不起作用。

如何在不使用端口2195的情况下从PHP发送iOS推送通知,端口2195在我的主机(one.com)上被阻止

我试过这个代码:

$url = 'https://gateway.sandbox.push.apple.com:2195';
$cert = 'ck.pem';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json"));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_SSLCERT, $cert);
curl_setopt($ch, CURLOPT_SSLCERTPASSWD, 'password_hidden_on_stack_overflow');
curl_setopt($ch, CURLOPT_POSTFIELDS, '{"device_tokens": ["458e5939b2xxxxxxxxxxx3"], "aps": {"alert": "test message one!"}}');
$curl_scraped_page = curl_exec($ch);

但它给了我这个错误:解析错误:语法错误,意外的"*"

如何解决这个问题?

编辑

我也尝试过这个代码:

$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', 'password_hidden_on_stack_overflow'); // Add your own ck.pem passphrase here
foreach($deviceIDs as $deviceToken) {
    // Open a connection to the APNS server
    $fp = stream_socket_client(
        'ssl://gateway.sandbox.push.apple.com:2195', $err,
        $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
    if (!$fp) exit("Failed to connect: $err $errstr" . PHP_EOL);
    echo 'Connected to APNS' . PHP_EOL;
    // Create the payload body
    $body['aps'] = array(
        'alert' => $message,
        'sound' => 'default'
        );
    $payload = json_encode($body);
// Build & send notification
$msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;
$result = fwrite($fp, $msg, strlen($msg));
/*if (!$result)
    echo 'Message not delivered' . PHP_EOL;
    else
        echo 'Message successfully delivered' . PHP_EOL;*/
}   
// Close the connection to the server
fclose($fp);

但它给了我这个错误:警告:stream_socket_client():无法连接到ssl://gateway.sandbox.push.apple.com:2195(连接超时)

以前没有使用过这个,但我知道我的一个朋友使用了一些开源代码。这里有一个链接,希望它能有所帮助:php apn