How To Communicate Between PHP Scripts (Sample Source Code Included) �p�q������PHP�}���]�˥����N�X�]�t�^
PHP scripts such as WordPress, phpBB, phpMyAdmin etc. operate in their own world without much inter-process communication, even if they are running on the same Apache server. PHP�}���A�Ҧp�bWordPress �A phpbb �A phpmyadmin���B�@�A�b�ۤv���@�ɡA�S�������i�{���q�H�A�Y�ϥL�̬O�B��b�P�@�xApache�A�Ⱦ��C However sometimes you need to have a seamless way to communicate between PHP scripts without deadlock or resource contention.���L���ɧA�ݭn���@�ӵL�_���q�H�覡������PHP�}���S����A�θ귽��ij���J�I�C This is where messaging becomes useful.�o�O�H���������Ϊ��C PHP allows you to easily communicate between scripts running on the same machine. PHP���i�H��z�����a���������q�A�}���B��b�P�@�x�����W�C
PHP provides a set of function for inter-process communication. PHP�����ѤF�@�M�\�ର��i�{���q�H�C They are:�L�̬O�G
msg_get_queue -- Create or attach to a message queue msg_get_queue -�ЫةΪ��[��@�Ӯ��C
msg_receive -- Receive a message from a message queue msg_receive -����@���A�q���C
msg_remove_queue -- Destroy a message queue msg_remove_queue -�R���@�Ӯ��C
msg_send -- Send a message to a message queue msg_send -�N�T���ǰe�����C
msg_set_queue -- Set information in the message queue data structure msg_set_queue -�]�w��ơA�b���C���ƾڵ��c
msg_stat_queue -- Returns information from the message queue data structure msg_stat_queue -�Ǧ^����ơA�q���C���ƾڵ��c
Let's see an actual example with two scripts which communicate with each other.��ڭ̬ݬݤ@�ӹ�ڪ��Ҥl����Ӽ@���A�䤤���۷��q�C Save the files (preferably with the given names) on your htdocs (/var/www/html on Linux) directory.�x�s�ɮס]�̦n�O�P�S�w���W�١^��z��htdocs �] /�Ovar / www /�bLinux�W��HTML �^�ؿ�C First run the msg_send.php to add message to the queue.����B��msg_send.php�s�W�T�������C���C Then run msg_receive.php to get the messages and display them.�M��B��msg_receive.php�o��T���A����ܥ��̡C
File: msg_send.php �ɮסG msg_send.php
Notes: This file adds two message to the queue�`�G�����K�[�F��ӰT�������C
File: msg_receive.php �ɮסG msg_receive.php
Note: This file receives the message and prints them.�`�N�G������l��M�L��~�C
The output from the first script will be similar to:��X���Ĥ@�Ӹ}���N�����G
Array ( [msg_perm.uid] => 48 [msg_perm.gid] => 48 [msg_perm.mode] => 438 [msg_stime] => 1194961462 [msg_rtime] => 1194961436 [msg_ctime] => 1194961406 [msg_qnum] => 2 [msg_qbytes] => 16384 [msg_lspid] => 9474 [msg_lrpid] => 9472 )�}�C�] [ msg_perm.uid ] = > 48 [ msg_perm.gid ] = > 48 [ msg_perm.mode ] = > 438 [ msg_stime ] = > 1194961462 [ msg_rtime ] = > 1194961436 [ msg_ctime ] = > 1194961406 [ msg_qnum ] = > 2 [ msg_qbytes ] = > 16384 [ msg_lspid ] = > 9474 [ msg_lrpid ] = > 9472 �^ The output from the second script will be similar to:���q�q�ĤG�Ӹ}���N�����G
This is message #1 Array ( [msg_perm.uid] => 48 [msg_perm.gid] => 48 [msg_perm.mode] => 438 [msg_stime] => 1194961462 [msg_rtime] => 1194961471 [msg_ctime] => 1194961406 [msg_qnum] => 1 [msg_qbytes] => 16384 [msg_lspid] => 9474 [msg_lrpid] => 9476 ) This is message #2 Array ( [msg_perm.uid] => 48 [msg_perm.gid] => 48 [msg_perm.mode] => 438 [msg_stime] => 1194961462 [msg_rtime] => 1194961471 [msg_ctime] => 1194961406 [msg_qnum] => 0 [msg_qbytes] => 16384 [msg_lspid] => 9474 [msg_lrpid] => 9476 )�o�O�T���� 1�}�C�] [ msg_perm.uid ] = > 48 [ msg_perm.gid ] = > 48 [ msg_perm.mode ] = > 438 [ msg_stime ] = > 1194961462 [ msg_rtime ] = > 1194961471 [ msg_ctime ] = > 1194961406 [ msg_qnum ] = > 1 [ msg_qbytes ] = > 16384 [ msg_lspid ] = > 9474 [ msg_lrpid ] = > 9476 �^ �A�o�O�H���� 2�}�C�] [ msg_perm.uid ] = > 48 [ msg_perm.gid ] = > 48 [ msg_perm �C�Ҧ�] = > 438 [ msg_stime ] = > 1194961462 [ msg_rtime ] = > 1194961471 [ msg_ctime ] = > 1194961406 [ msg_qnum ] = > 0 [ msg_qbytes ] = > 16384 [ msg_lspid ] = > 9474 [ msg_lrpid ] = > 9476 �^ Filed under���_�U Enterprise Software��~�n�� , �A Headline News�Y��s�D , �A How To�p�� , �A Open Source Software�}�X�n�� , �A PHP PHP�� , �A Programming�s�{ , �A Tech Note�N���� , �A Web���� | |
| |
RSS 2.0 2.0 | |
Trackback Trackback��踪 this Article |���峹|
Email this Article�q�l�l�峹
You may also like to read�z�]�i�H�QŪ |





































May 6th, 2008 at 8:41 am 2008�~5��6��A�b�W��08��41��
[...] can be used to store and retrieve data across processes. [ �K �K ]�i�H�ΨӦs�x�M�˯��ƾڪ��L�{�C This is also another alternative way to communicate between php scripts.�o�]�O�t�@�ؿ�ܪ��q�H�覡������PHP�}���C Normally shared memory is used for caching frequently used data in memory for php scripts on the [...]�q�`�@�ɦs�x���O�Ψӽw�s�g�`�ϥΪ��ƾڦb���s����PHP�}����[ �K �K ]