1

私は従いました: Apple Push Notification Services Tutorial。そして、それは私にとって地元でうまくいきました。次に、サーバーからプッシュ通知を送信したいですか?

サーバーにsimplepush.phpとをアップロードしました。ck.pemチェックhttp://www.myserver/simplepush.phpするとエラーが発生します:

*警告: stream_socket_client() [function.stream-socket-client]: /home/cherry/public_html/simplepush で ssl://gateway.sandbox.push.apple.com:2195 (接続タイムアウト) に接続できません。 21 行目の php 接続に失敗しました: 110 接続がタイムアウトしました*

手伝っていただけませんか?

PHP コード:

<?php

// Put your device token here (without spaces):
$deviceToken = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';

// Put your private key's passphrase here:
$passphrase = 'xxxxxxxxxx';

// Put your alert message here:
$message = 'My first push notification!';

////////////////////////////////////////////////////////////////////////////////

$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);

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

// Encode the payload as JSON
$payload = json_encode($body);

// Build the binary notification
$msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;

// Send it to the server
$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);
4

1 に答える 1

0

PHP は SSL でコンパイルされていますか? それはあなたのローカルバージョンと同じバージョンですか?

または、何らかのファイアウォールがサーバーから 2195 ポートへの接続をブロックしている可能性がありますか?

このサーバーに (何らかのシェルに) ログインして、telnet 経由でこのサーバーとポートに接続できるかどうかを確認してください:

$ telnet gateway.sandbox.push.apple.com 2195
于 2012-04-24T08:45:14.170 に答える