Stream_socket_client (): Unable to connect to ssl: //gateway.sandbox.push.apple.com: 2195 (Connection refused)

I made a php file to send notifications to Apple iphone users. It works on another server but doesn't work on my server. I made the .pem file exactly and also opened the port number 2195,2196. But still its not working. Please help me to get rid of this problem. Here is my php code for sending push notification:

 <?php
// Put your device token here (without spaces):
$deviceToken = 'f672c26cbfb279e45c1b66d0ddb738d8043c785d5bb8dd20a72d52ae88d4a604';
// Put your private key 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);
?>

      

+3


source to share


2 answers


I found this article having the same problems and my problem was actually two problems and thought it might be helpful here. I am on Ubuntu / Php 5.5

Check your certificates!

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

      

When I found the above test, I realized that I was missing the root center certificate on my server, point 1. Once I ran the above and it worked, I realized that the library I was using was transferring only one file, two.

1. Root certificate

On my server, I did not have the correct root certificate installed and hence it was unable to validate my PEM file. I'll get to the next one. After I found this page [Where can I get the CTR CA file of the Entrust Root file?] [1] I checked and I have the same problem. I am assuming you d / l to your home folder ...

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

      



You should see a response indicating that the certificate has been installed or updated. Now I was getting somewhere. I started getting another connection error related directly to my PEM file. Something I could work with.

2. Your PEM file

If you are using a library like mine that uploads a single file and you passed the above test using both your certificate and key, you need to combine your key and certificate. For me using my example names above push-crt.pem

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

      

then I used a combined key / certificate pair and everything started working.

Uf. Hope this helps someone.

+3


source


Try this code and make sure your certificate mentioned in the server path, especially ck.pem



    $deviceToken = 'f672c26cbfb279e45c1b66d0ddb738d8043c785d5bb8dd20a72d52ae88d4a604';
    // Put your private key 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);

    }

      

+2


source







All Articles