David Turton
David Turton

Reputation: 385

stream_socket_client unable to connect to Apple APNS (Permission Denied)

I have a rare situations for sending push notifications via php which I cant figure out. I have a simple php script that send the notification shown below. If I execute this file via the command line php script.php it works just fine. If I execute via web http://domain.com/script.php it give me a Permission Denied Error. I have the correct cert path, not password for the cert. Any ideas?

Warning: stream_socket_client(): unable to connect to ssl://gateway.push.apple.com:2195 (Permission denied)

    ...............

    $apns = connect_apns('gateway.push.apple.com', '2195');
    $write = send_payload($apns, $deviceToken, $payload);
    fclose($apns);
    }

    function connect_apns($apnsHost, $apnsPort) {
    $streamContext = stream_context_create();
    stream_context_set_option($streamContext, 'ssl', 'local_cert', 'cert.pem');
    return stream_socket_client('ssl://' . $apnsHost . ':' . $apnsPort, $error, $errorString, 60, STREAM_CLIENT_CONNECT, $streamContext);
    }

    function send_payload($handle, $deviceToken, $payload) {
    $apnsMessage = chr(0) . chr(0) . chr(32) . pack('H*', str_replace(' ', '', $deviceToken)) . chr(0) . chr(strlen($payload)) . $payload;
    return fwrite($handle, $apnsMessage);
    }

Upvotes: 2

Views: 2472

Answers (1)

David Turton
David Turton

Reputation: 385

so after much time spending looking at this I noticed that I had Selinux enabled. If you want to keep it enabled you need to set the property httpd_can_network_connect to true by issuing this command: setsebool -P httpd_can_network_connect 1. Otherwise just just disable selinux by going to vim /etc/selinux/config and set SELINUX=disabled.

Upvotes: 3

Related Questions