How To Communicate Between PHP Scripts (Sample Source Code Included)如何沟通之间的PHP脚本(样本源代码包含)
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脚本,例如在WordPress , phpbb , phpmyadmin等运作,在自己的世界,没有什么跨进程间通信,即使他们是运行在同一台Apache服务器。 However sometimes you need to have a seamless way to communicate between PHP scripts without deadlock or resource contention.不过有时你需要有一个无缝的通信方式之间的PHP脚本没有僵局,或资源争议的焦点。 This is where messaging becomes useful.这是信息成为有用的。 PHP allows you to easily communicate between scripts running on the same machine. PHP中可以让您轻易地之间的沟通,脚本运行在同一台机器上。
PHP provides a set of function for inter-process communication. PHP中提供了一套功能为跨进程间通信。 They are:他们是:
msg_get_queue — Create or attach to a message queue msg_get_queue -创建或附加到一个消息队列
msg_receive — Receive a message from a message queue msg_receive -收到一条消息,从消息队列
msg_remove_queue — Destroy a message queue msg_remove_queue -摧毁一个消息队列
msg_send — Send a message to a message queue msg_send -将讯息传送给消息队列
msg_set_queue — Set information in the message queue data structure msg_set_queue -设定资料,在消息队列的数据结构
msg_stat_queue — Returns information from the message queue data structure msg_stat_queue -传回的资料,从消息队列的数据结构
Let’s see an actual example with two scripts which communicate with each other.让我们看看一个实际的例子有两个剧本,其中互相沟通。 Save the files (preferably with the given names) on your htdocs (/var/www/html on Linux) directory.储存档案(最好是与特定的名称)对您的htdocs ( /是var / www /在Linux上的HTML )目录中。 First run the msg_send.php to add message to the queue.首先运行msg_send.php新增讯息给队列中。 Then run msg_receive.php to get the messages and display them.然后运行msg_receive.php得到讯息,并显示它们。
File: msg_send.php 档案: msg_send.php
Notes: This file adds two message to the queue注:此文件添加了两个讯息给队列
File: msg_receive.php 档案: msg_receive.php
Note: This file receives the message and prints them.注意:此文件接收邮件和印刷品。
The output from the first script will be similar to:输出的第一个脚本将类似于:
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 )阵列( [ 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:产量从第二个脚本将类似于:
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 )这是讯息# 1阵列( [ 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 ) ,这是信息# 2阵列( [ msg_perm.uid ] = > 48 [ msg_perm.gid ] = > 48 [ msg_perm 。模式] = > 438 [ msg_stime ] = > 1194961462 [ msg_rtime ] = > 1194961471 [ msg_ctime ] = > 1194961406 [ msg_qnum ] = > 0 [ msg_qbytes ] = > 16384 [ msg_lspid ] = > 9474 [ msg_lrpid ] = > 9476 ) Filed under提起下 Enterprise Software企业软件 , , Headline News头条新闻 , , How To如何 , , Open Source Software开放源码软件 , , PHP PHP的 , , Programming编程 , , Tech Note技术说明 , , Web网页 | |
| |
RSS 2.0 2.0 | |
Trackback Trackback跟踪 this Article |此文章|
Email this Article电子邮件此文章
You may also like to read您也可以想读 |




May 6th, 2008 at 8:41 am 2008年5月6日在上午8时41分
[...] can be used to store and retrieve data across processes. [ … … ]可以用来存储和检索数据的过程。 This is also another alternative way to communicate between php scripts.这也是另一种选择的通信方式之间的PHP脚本。 Normally shared memory is used for caching frequently used data in memory for php scripts on the [...]通常共享存储器是用来缓存经常使用的数据在内存中为PHP脚本对[ … … ]