Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP fails socket connection with Apple notification gateway

Disclaimer: on SO I found many similar questions and some answers but none solved my issue.

I have this easy PHP code:

<?php
    $deviceToken = "myDeviceToken";
    $message = "Hello from server";
    $badge = 1;
    $sound = "default";

    // Construct the notification payload
    $body['aps'] = array(
        'alert' => $message
    );

    if ($badge) {
        $body['aps']['badge'] = $badge;
    }

    if ($sound) {
        $body['aps']['sound'] = $sound;
    }

    /* End of Configurable Items */
    $ctx = stream_context_create();
    stream_context_set_option($ctx, 'ssl', 'local_cert', 'my.pem');

    $fp = stream_socket_client("ssl://gateway.sandbox.push.apple.com:2195", $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx);

    if (!$fp) {
        print "Failed to connect $err $errstrn";
        return;
    } else {
        print "Connection OK\n";
    }

    $payload = json_encode($body);
    $msg = chr(0) . pack("n", 32) . pack("H*", str_replace(" ", "", $deviceToken)) . pack("n", strlen($payload)) . $payload;
    print "sending message :" . $payload . "\n";
    fwrite($fp, $msg);
    fclose($fp);
?>

When called from browser I receive:

[13-Dec-2011 18:52:52] PHP Warning: stream_socket_client() [function.stream-socket-client]: SSL operation failed with code 1. OpenSSL Error messages: error:14094410:SSL routines:SSL3_READ_BYTES:sslv3 alert handshake failure in /home/srv/public_html/myTest.php on line 28

[13-Dec-2011 18:52:52] PHP Warning: stream_socket_client() [function.stream-socket-client]: Failed to enable crypto in /home/srv/public_html/myTest.php on line 28

[13-Dec-2011 18:52:52] PHP Warning: stream_socket_client() [function.stream-socket-client]: unable to connect to ssl://gateway.sandbox.push.apple.com:2195 (Unknown error) in /home/srv/public_html/myTest.php on line 28

Any idea of what's going on?

like image 975
AsTheWormTurns Avatar asked Dec 14 '11 07:12

AsTheWormTurns


1 Answers

Some random finding from the internet which could help:

  1. It may be a certificate problem. Try the stream options allow_self_signed and verify_peer to check that.

  2. Try to use explicitely sslv2:// or sslv3:// ?

  3. Permission problem on "/dev/urandom"

like image 66
Savageman Avatar answered Oct 04 '22 07:10

Savageman