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

3

我制作了一个用于向苹果iphone用户发送通知的php文件。它在其他服务器上运行正常,但在我的服务器上却不工作。我已经准确地制作了.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);
?>

$ php simplepush.php 的输出是什么? - Gago
2个回答

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. 根证书

在我的服务器上,我没有安装正确的根证书,因此无法验证PEM文件。下一步我会解决这个问题。当我找到这个页面[获取Entrust Root CA PEM文件的位置][1]时,我检查并发现我遇到了同样的问题。我假设您将其下载到您的主目录...

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

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

哎呀,希望能对某些人有所帮助。


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

    }

如果您最终关闭连接,为什么要使用STREAM_CLIENT_PERSISTENT? - andreszs

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接