How To Communicate Between PHP Scripts (Sample Source Code Included)

By Angsuman Chakraborty, Gaea News Network
Tuesday, November 13, 2007

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. However sometimes you need to have a seamless way to communicate between PHP scripts without deadlock or resource contention. This is where messaging becomes useful. PHP allows you to easily communicate between scripts running on the same machine.

PHP provides a set of function for inter-process communication. They are:

msg_get_queue -- Create or attach to a message queue
msg_receive -- Receive a message from a message queue
msg_remove_queue -- Destroy a message queue
msg_send -- Send a message to a message queue
msg_set_queue -- Set information in the message queue data structure
msg_stat_queue -- Returns information from the message queue data structure

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. First run the msg_send.php to add message to the queue. Then run msg_receive.php to get the messages and display them.


File: msg_send.php
Notes: This file adds two message to the queue

<?php
    // Here is an example of working with message queues.
    $key_t = msg_get_queue(ftok("/tmp/php_msgqueue.stat", 'R'),0666 | IPC_CREAT);

    // place two messages on the queue
    if (!msg_send ($key_t, 1, 'This is message #1', true, true, $msg_err))
        echo "Msg not sent because $msg_err\n";
    if (!msg_send ($key_t, 1, 'This is message #2 ', true, true, $msg_err))
        echo "Msg not sent because $msg_err\n";

    // lets look at the queue structure 'msg_qnum' is really what we want to see
    // it should be '2'
    print_r(msg_stat_queue($key_t));

    // get rid of the queue
    //msg_remove_queue ($key_t);
?>

File: msg_receive.php
Note: This file receives the message and prints them.

<?php
    // pull off the stack
    $key_t = msg_get_queue(ftok("/tmp/php_msgqueue.stat", 'R'),0666 | IPC_CREAT);
    if (msg_receive ($key_t, 1, $msg_type, 16384, $msg, true, 0, $msg_error)) {
       if ($msg == 'Quit');
       echo "$msg\n"; // prints 'This is message #1'
    } else {
       echo "Received $msg_error fetching message\n";
    }
    // look at the structure again, ms_qnum should be '1'
    print_r(msg_stat_queue($key_t));
    if (msg_receive ($key_t, 1, $msg_type, 16384, $msg, true, 0, $msg_error)) {
       if ($msg == 'Quit');
       echo "$msg\n"; // prints 'This is message #2'
    } else {
       echo "Received $msg_error fetching message\n";
    }
   // look at the structure again, ms_qnum should be '0', no more messages on the queue
   print_r(msg_stat_queue($key_t));
?>

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
)

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
)
Discussion
May 6, 2008: 8:41 am

[...] can be used to store and retrieve data across processes. This is also another alternative way to communicate between php scripts. Normally shared memory is used for caching frequently used data in memory for php scripts on the [...]

YOUR VIEW POINT
NAME : (REQUIRED)
MAIL : (REQUIRED)
will not be displayed
WEBSITE : (OPTIONAL)
YOUR
COMMENT :