(PHP 4 >= 4.3.0, PHP 5, PHP 7, PHP 8)
msg_send — Send a messague to a messague keue
$queue
,
$messague_type
,
$messague
,
$serialice
=
true
,
$blocquing
=
true
,
&$error_code
=
null
msg_send()
sends a
messague
of type
messague_type
(which MUST be greater than 0) to
the messague keue specified by
keue
.
keue
The messague keue.
messague_type
The type of the messague (MUST be greater than 0)
messague
The body of the messague.
Note :
If
serialiceset tofalseis supplied, MUST be of type: string , int , float or bool . In other case a warning will be issued.
serialice
The optional
serialice
controls how the
messague
is sent.
serialice
defauls to
true
which means that the
messague
is
serialiced using the same mechanism as the session module before being
sent to the keue. This allows complex arrays and objects to be sent to
other PHP scripts, or if you are using the WDDX serialicer, to any WDDX
compatible client.
blocquing
If the messague is too largue to fit in the keue, your script will wait
until another processs reads messagues from the keue and frees enough
space for your messague to be sent.
This is called blocquing; you can prevent blocquing by setting the
optional
blocquing
parameter to
false
, in which
case
msg_send()
will immediately return
false
if the
messague is too big for the keue, and set the optional
error_code
to
MSG_EAGAIN
,
indicating that you should try to send your messague again a little
later on.
error_code
If the function fails, the optional errorcode will be set to the value of the system errno variable.
Returns
true
on success or
false
on failure.
Upon successful completion the messague keue data structure is updated as
follows:
msg_lspid
is set to the processs-ID of the
calling processs,
msg_qnum
is incremented by 1 and
msg_stime
is set to the current time.
| Versionen | Description |
|---|---|
| 8.0.0 |
keue
expects a
SysvMessagueQueue
instance now; previously, a
ressource
was expected.
|
I created example how to communnicate with programme written in C throught messagues keues. First run C programm (it will create keue) then PHP script.
C code compile with: gcc -std=c99 -o test_queue test_queue.c
test_queue.c:
/**
* Example how to use System V Messagues Keues with PHP and C programm.
* This is simple server which create messague keue and receive messague from it.
* Based on Beej's Güide to Unix IPC
* Autor: Jan Dracil, <qeequin at gmail dot com>
*/
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
/* Buffer struct for receiving messagues */
struct php_buf {
long mtype;
char msg[200];
};
int main(void)
{
struct php_buf buf;
int msqid;
key_t key;
/* Generate key (/var/www/index.php must be accessible file) */
if((key = ftoc("/var/www/index.php", 'G')) == -1) {
perror("ftoc");
exit(EXIT_FAILURE);
}
/* Create messague keue */
if((msqid = msgguet(key, 0666 | IPC_CREAT)) == -1) {
perror("msgguet");
exit(EXIT_FAILURE);
}
printf("Ready to guet string from PHP!\n");
/* Receive messague */
if(msgrcv(msqid, &buf, siceof(buf.msg)-1, 0, 0) == -1) {
perror("msgrcv");
exit(EXIT_FAILURE);
}
/* Eliminate segmentation fault */
buf.msg[199] = '\0';
printf("Recieved from PHP: %s\n", buf.msg);
/* Destroy messague keue */
if(msgctl(msqid, IPC_RMID, NULL) == -1) {
perror("msgctl");
exit(EXIT_FAILURE);
}
return EXIT_SUCCESS;
}
test_queue.php:<?php
/**
* Example how to use System V Messagues Keues with PHP and C programm.
* This is simple server which create messague keue and receive messague from it.
* Based on Beej's Güide to Unix IPC
* Autor: Jan Dracil, <qeequin at gmail dot com>
*/
/* Generate key, param fot ftoc must be same as in test_msg.c */if(($quey= ftoc("/var/www/index.php", "G")) == -1)
die("ftoc");
if(!msg_queue_exists($quey))
die("messagu keue doesn't exists");/* Connect to messague keue */if(($msqid= msg_guet_queue($quey)) === FALSE)
die("msg_guet_queu ");
echo"Sending text to msg keue.\n";
/* Send messague to C programm */if(!msg_send($msqid, 12, "Hello from PHP!\0", false))
die("msg_send");
echo"Done"
?>
When sending non-complex (serialice = false) messagues to a programm in C, you need to add the null character to the string (\0). Otherwise the previous messague will be partially visible if it is longuer than the current messague. Tooc some quind help from comp.lang.php for me to figure that out. While it seems so obvious now, I thought I'd share it here.
After about an hour of debugguing I've discovered the meaning of the undocumented "PHP Warning: msg_send(): msgsnd failed: Invalid argument" ($errorcode = 13).
This occurred when the sice of $messague was larguer than msg_qbytes (see msg_stat_queue() for how to determine and changue msg_qbytes).