christosapl / php-apns Goto Github PK
View Code? Open in Web Editor NEWAutomatically exported from code.google.com/p/php-apns
Automatically exported from code.google.com/p/php-apns
PHP APNS Provider as Service ------------------------------ A small set of PHP scripts (to be run as a background service) to send messages to APNS (Apple Push Notifications Service). Features: * Messages are sent from a queue. You can have a single, central queue and many instances of this script that send messages. Thus, this solution is extremely scalable. * The connection with APNS servers is closed after X minutes of inactivity. * If the connection with APNS servers is dropped (for whatever reason), the script creates a new connection automatically. * 100% pure PHP code! Included files: * PushMonitor.php (created by Alessandro Segala - released under LGPL) The main script: this is the script you will run. It checks for new items in the queue at regular intervals and sends them to Apple through the PushService script. * PushService.php (created by Luke Rhodes and adapted by Alessandro Segala - released into the public domain) Manages the connection with APNS servers. This script should never be executed directly: it is started by PushMonitor. * PushErrors.log A log file where the scripts write errors. Make sure it is writable. Requirements: * PHP with CLI support and Memcache extension o on Linux servers, if they aren't already installed, you should find these packages in your distro's repisitories (on Debian: "sudo aptitude install php-cli php-memcache"). o on Mac OSX Leopard, the built-in version of PHP doesn't have the Memcache extension: you might want to grab the version compiled by entropy.ch. * MemcacheQ o This is a little daemon that manages the message queue. o It is based on MemcacheDB, so it extremely fast and persistent (based on Berkeley DB, a non-relational database). o Uses the same protocol as Memcached, so you can connect to the MemcacheQ dameon using one of the many libraries, or even just with telnet. o To install it, you must compile it (requires about 2 minutes): http://memcachedb.org/memcacheq/ Installation: 1. Copy all files to a directory (e.g. /usr/local/push). 2. Place your push certificate in the same folder. Name it "apns.pem" (if you wish to change this name, please change it also in PushService.php). Don't add any password to the certificate. 3. If you need to use the sandbox, change the hostname in PushService.php. 4. Set the options in PushMonitor.php. In particular, you might wish to change the queue name. Usage: * Start the PushMonitor.php script. The standard way is: php -f /usr/local/push/PushMonitor.php You might want to create an init.d/rc.d/launchd script to execute the file as a background process. * To send messages, just add them to the queue!
PushMonitor will consume up to whatever "memory_limit" is set to in php.ini and
then become unresponsive and need a restart. The reason for this is the call to
create_function() in the loop on line 89 of PushMonitor.php. I added the
equivalent function to the code and the memory leak went away.
Seams like this is expected with create_function()
https://bugs.php.net/bug.php?id=6333
"[2000-09-07 11:43 UTC] [email protected]
This isn't a bug but how it works. Each create_function() call creates a new
unique function. So obviously if you define zillions in an infinite loop you'll
run out of memory."
Thank you so much for making this code available. It works extremely well. I'm
able to send an average of 1.5 million push notifications a day on very modest
hardware.
Thanks,
Darren
Original issue reported on code.google.com by [email protected]
on 21 Aug 2011 at 4:49
Whenever PushMonitor.php gets a message from the queue, it gives me this error
"Error while
starting child process"
I've followed the installation instructions.
My specs:
PHP 5.2.6 running on CentOS release 5.2 (Final) on my MediaTemple VPS
Any suggestions?
Original issue reported on code.google.com by [email protected]
on 27 Jun 2009 at 1:02
In the readme it says "To send messages, just add them to the queue!"
What does this mean??? What php do we include and a code snippet would be
handy. I can't see any function like add to queue in any of the code :-(
Original issue reported on code.google.com by [email protected]
on 23 Jun 2009 at 9:09
We are having a lot of trouble in getting this to work for our iphone app - we
wanted to see if
someone wants a small cash bounty to help us to get it working on our server?
please contact [email protected] if you are able / interested in some quick $ in
helping us to
implement!
thanks
Original issue reported on code.google.com by [email protected]
on 30 Jun 2009 at 12:57
The use of PHP_NORMAL_READ in PushService.php appears to cause PushService.php
to work
incorrectly under PHP 5.2.9 closing the connection after the token is sent (and
not waiting for the
message)
Changing these lines to the following;
$deviceToken = trim(socket_read($clientSocket, 1024));
$message = trim(socket_read($clientSocket, 1024));
Omitting the PHP_NORMAL_READ appears to have corrected this
Original issue reported on code.google.com by [email protected]
on 24 Jun 2009 at 10:13
What steps will reproduce the problem?
1. sending a message
2. waiting for the PushMonitor to kill the service process
What is the expected output? What do you see instead?
Expect the service process to be killed, instead app just hangs
What version of the product are you using? On what operating system?
Debian
Please provide any additional information below.
I suspect the code in process::kill() which seems to have been copied from the
PHP docs doesn't get the child process IDs from the
parent PID correctly.
The line :
$pids = preg_split('/\s+/', `ps -o pid --no-heading --ppid $ppid`);
makes no sense to me as the preg split should surely be on the output of the
command 'ps -o pid --no-heading --ppid $ppid' not the
command line itself.
Is there a reason we need to find child PIDs? From what I can see the
PushService.php file doesnt open any child processes.
Ive replaced the function with the following code which works for me:
public function kill()
{
global $debug;
$status = proc_get_status($this->pointer);
print_r($status);
if($status['running'])
{
if($debug)
echo 'proc is running...closing pipes.';
fclose($this->pipes[0]);
fclose($this->pipes[1]);
fclose($this->pipes[2]);
$ppid = $status['pid'];
proc_terminate($this->pointer);
return (proc_close($this->pointer) == 0);
}
}
I'm not sure if I need the proc_close condition on return.
Original issue reported on code.google.com by [email protected]
on 28 Aug 2009 at 11:54
Hi, I know it's not really a problem with php-apns but I am having trouble
getting the script to
start automatically when my server boots.
If I enter the startup commands manually everything works as it should.
I've tried creating an init.d script but it does not work.
Which user should be starting memcache & pushmonitor.php? Should they both be
root?
Do they need to be called after apache starts?
A tutorial on how to accomplish this would be greatly appreciated.
I'm on the latest version of ubuntu on slicehost.
Thanks,
Jim
Original issue reported on code.google.com by [email protected]
on 27 Jul 2009 at 11:45
What steps will reproduce the problem?
1. Send a successful push notification
2. Far end closes socket or socket becomes unusable
3. Try to send another Push notification and fwrite to the socket fails
What is the expected output? What do you see instead?
The fwrite error needs to be caught and a retry on another socket attempted..
What version of the product are you using? On what operating system?
Centos 5.3
Please provide any additional information below.
We were noticing the service hanging.
We solved this partly by adding the following to catch the socket fwrite
error in PushService.php. This now catches 75% of the hangs - there is
still another hang out there which we havent solved yet - we suspect
SendNotification is waiting for ever for a response to the fwrite:-
//Catch fwrite error in SendNotification
function sendNotification($deviceToken, $message)
{
//added catch fwrite error
$apnsMessage = chr(0) . chr(0) . chr(32) . pack('H*', str_replace(' ', '',
$deviceToken)) . chr(0) . chr(strlen($message)) . $message;
$fwritten=fwrite($this->apnsConnection, $apnsMessage);
if ($fwritten==0 || $fwritten===FALSE || $fwritten < strlen($apnsMessage))
{
error_log(date('Y-m-d H:i')." - fwrite failed:\n".$message."\nTo device:
".$deviceToken."\n return = ".$fwritten."\n", 3, 'PushServiceError.log');
return -1;
}
return $fwritten;
}
//Spot the send failure in listenForClients()
function listenForClients()
{
$this->serviceConnection = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_bind($this->serviceConnection, $this->serviceHost,
$this->servicePort);
socket_listen($this->serviceConnection, 10);
echo 'LISTENING ',$this->servicePort,"\n";
while($clientSocket = socket_accept($this->serviceConnection))
{
socket_write($clientSocket, "OK\n");
$deviceToken = trim(socket_read($clientSocket, 1024, PHP_NORMAL_READ));
$message = trim(socket_read($clientSocket, 1024, PHP_NORMAL_READ));
if(!empty($deviceToken) && !empty($message))
{
//SB added catch fwrite error
if ($this->sendNotification($deviceToken, $message) >= 0)
{
socket_write($clientSocket, "SENT\n");
}
else
{
socket_write($clientSocket, "ERROR\n");
}
}
else
{
socket_write($clientSocket, "ERROR\n");
}
socket_close($clientSocket);
}
}
Original issue reported on code.google.com by [email protected]
on 20 Oct 2009 at 8:49
The following PHP packages seem to also be required and they don't
necessarily install be default on all systems:
1) php-openssl
2) php-sockets
I think they should be added to the "Requirements" section of the README file
Original issue reported on code.google.com by [email protected]
on 3 Nov 2009 at 3:19
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.