stream_socket_client():无法连接到ssl://gateway.sandbox.push.apple.com:2195(连接被拒绝)

时间:2014-11-11 12:49:47

标签: php ios iphone ssl

我制作了一个php文件,用于向苹果iphone用户发送通知。它为其他服务器工作但不在我的服务器上工作。我准确地制作了.pem文件,并打开了端口号2195,2196。但它仍然不起作用。 请有人帮我解决这个问题。这是我发送推送通知的PHP代码:

 <?php
// Put your device token here (without spaces):
$deviceToken = 'f672c26cbfb279e45c1b66d0ddb738d8043c785d5bb8dd20a72d52ae88d4a604';
// Put your private key's passphrase here:
$passphrase = 'pushchat';
// Put your alert message here:
$message = 'Welcome in testing';
$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);
?>

2 个答案:

答案 0 :(得分:3)

尝试此代码并确保服务器路径中提到的证书特别是ck.pem

    $deviceToken = 'f672c26cbfb279e45c1b66d0ddb738d8043c785d5bb8dd20a72d52ae88d4a604';
    // Put your private key's passphrase here:
    $passphrase = 'pushchat';

    // Put your alert message here:
    $message = 'Hello';

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

    $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);

    }

答案 1 :(得分:3)

我发现这篇文章时遇到了同样的问题,我的问题实际上是两个问题,并认为它在这里很有用。我在Ubuntu / Php 5.5

测试您的证书!

prompt\#: openssl s_client -connect gateway.sandbox.push.apple.com:2195
     -cert ./push-crt.pem 
     -key ./push-key.pem 
     -CApath ./entrust_root_certification_authority.pem

当我发现上面的测试时,我意识到我在服务器上缺少根权限证书,第1项。一旦我运行上面的它并且它工作了我意识到我使用的库只传递了一个文件,更多关于项目2。

1。根证书

在我的服务器上,我没有安装正确的根证书,因此无法验证我的PEM文件。我接下来会谈到那个。一旦我找到这个页面[在哪里获取Entrust Root CA PEM文件?] [1]我检查过,我遇到了同样的问题。我假设你d / l到你的主文件夹...

prompt\#: sudo cp ~/entrust_root_certification_authority.pem 
    /usr/local/share/ca-certificates/entrust_root_certification_authority.crt
prompt\#: sudo update-ca-certificates

您应该看到响应表明已安装或更新了证书。现在,我到了某个地方。我开始得到与我的PEM文件直接相关的不同连接错误。我可以使用的东西。

2。您的PEM文件

如果您使用的是像我这样的库,它传递了一个文件并且您使用证书和密钥通过了上述测试,那么您需要将密钥和证书组合在一起。对我来说,使用push-crt.pem上面的示例名称

prompt\#: cat push-crt.pem > push.pem
prompt\#: echo >> push.pem
prompt\#: cat push-key.pem >> push.pem

然后我使用了组合的密钥/证书对,一切都开始工作了。

呼。希望它可以帮到某人。

相关问题