Reputation: 64834
I need to implement apple push notifications with a php server. I think the connection is correctly working but the message is refused by apple server for some reason.
I'm getting this error:
"error 0errorString
\nFatal error: Call to undefined function socket_close() in /home/www/76cdd1fbdfc5aca0c9f07f489ecd9188/web/mobileApp/handleRequest.php on line 420
\n"
This is my simple code (with a fake message, just for testing):
$payload['aps'] = array('alert' => 'This is the alert text', 'badge' => unread, 'sound' => 'default');
$payload['server'] = array('serverId' => $serverId, 'name' => $name);
$payload = json_encode($payload);
$apnsHost = 'gateway.sandbox.push.apple.com';
$apnsPort = 2195;
$apnsCert = 'ck.pem';
$streamContext = stream_context_create();
stream_context_set_option($streamContext, 'ssl', 'local_cert', $apnsCert);
$apns = stream_socket_client('ssl://' . $apnsHost . ':' . $apnsPort, $error, $errorString, 2, STREAM_CLIENT_CONNECT, $streamContext);
echo "error " . $error;
echo "errorString" . $errorString;
$apnsMessage = chr(0) . chr(0) . chr(32) . pack('H*', str_replace(' ', '', $deviceToken)) . chr(0) . chr(strlen($payload)) . $payload;
fwrite($apns, $apnsMessage);
socket_close($apns);
fclose($apns);
echo "Sent";
thanks
Upvotes: 0
Views: 862
Reputation: 811
$apnsHost = 'gateway.sandbox.push.apple.com';
Remove "sandbox" from this url and it will run.
Upvotes: 0
Reputation: 64834
I've actually solved. The problem was the content of the message (empty). The socket function works perfectly.
Upvotes: 0
Reputation: 64700
Replace:
socket_close($apns);
with
fclose($apns);
That should solve the error you've reported: not sure if that will actually fix your issue with APNS, though.
Upvotes: 0
Reputation: 10644
There is no function to close socket, you can only create socket and manage it by fwrite/fclose etc: http://php.net/manual/en/function.stream-socket-client.php
Upvotes: 1
Reputation: 13694
I'm guessing your PHP version may not have the Sockets functionality installed or enabled which is why you are getting that error.
Taken from PHP.net:
The socket functions described here are part of an extension to PHP which must be enabled at compile time by giving the --enable-sockets option to configure.
But for your Code, you don't need to use that socket_close($apns)
function. Instead you can just remove it and use the close since (Quote from PHP.net):
On success a stream resource is returned which may be used together with the other file functions (such as fgets(), fgetss(), fwrite(), fclose(), and feof()), FALSE on failure.
So using fclose($apns);
will close the Stream
Upvotes: 1