【问题标题】:stream_socket_client(): unable to connect to ssl://gateway.sandbox.push.apple.com:2195 (Connection refused)stream_socket_client():无法连接到 ssl://gateway.sandbox.push.apple.com:2195(连接被拒绝)
【发布时间】:2014-11-11 12:49:46
【问题描述】:

我制作了一个 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);
?>

【问题讨论】:

  • $ php simplepush.php 的输出是什么?

标签: php ios iphone ssl


【解决方案1】:

我在遇到相同问题时发现了这篇文章,我的问题实际上是两个问题,并认为它在这里可能有用。我在 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 项。一旦我运行了上面的并且它工作了,我意识到我使用的库只通过了一个文件,更多关于那个在项目两个。

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

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

呸。希望它可以帮助某人。

【讨论】:

    【解决方案2】:

    试试这个代码,确保你在服务器路径中提到的证书特别是 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?
    猜你喜欢
    • 1970-01-01
    • 2019-02-02
    • 1970-01-01
    • 2013-02-18
    • 2011-09-16
    • 1970-01-01
    • 1970-01-01
    • 2020-12-31
    • 1970-01-01
    相关资源
    最近更新 更多