PHP 8.3.4 Released!

msg_send

(PHP 4 >= 4.3.0, PHP 5, PHP 7, PHP 8)

msg_sendメッセージキューにメッセージを送信する

説明

msg_send(
    SysvMessageQueue $queue,
    int $message_type,
    string|int|float|bool $message,
    bool $serialize = true,
    bool $blocking = true,
    int &$error_code = null
): bool

msg_send() は、queue で指定したメッセージキューに対して message_type で指定した型 (0 より大きい数値である必要があります) のメッセージ message を送信します。

パラメータ

queue

メッセージキュー

message_type

メッセージのタイプ (0より大きい数値でなければいけません)

message

メッセージの本体

注意:

serializefalse に設定された場合、 この値は string, int, float または bool のいずれかでなければなりません。 それら以外の場合、警告が発生します。

serialize

オプションのパラメータ serialize は、 message を送信する方法を制御します。 serialize のデフォルト値は true で、 この場合 message が送信される前に セッションモジュールと同じ方法でシリアライズされます。 これにより、配列やオブジェクトのような複雑な形式のデータを 他の PHP スクリプトに送信することが可能となります。 また、もし WDDX シリアライザを使用しているなら、あらゆる WDDX 互換クライアントに対して同じことが可能となります。

blocking

メッセージがキューに収まらないほど大きい場合は、他のプロセスが 現在キューにあるメッセージを読み込んでキューの空き容量が確保されるまで スクリプトの実行を待ち続けます。これをブロックモードといいます。 オプションのパラメータ blockingfalse に設定することでブロックモードではなくすることが可能で、 この場合、もしキューの空き容量よりも大きなメッセージを送信すると msg_send() はすぐに false を返します。 また、オプションのパラメータ error_codeMSG_EAGAIN に設定すると、 少し時間をおいてメッセージを再度送信しなければならないことが戻り値からわかります。

error_code

この関数の実行が失敗した場合、オプションのエラーコードがシステムの errno 変数の値に設定されます。

戻り値

成功した場合に true を、失敗した場合に false を返します。

処理が正常に完了すると、メッセージキューデータ構造体は以下のように更新されます。 msg_lspid には呼び出し元のプロセス ID が 設定され、msg_qnum が 1 増加し、 msg_stime が現在の時刻に設定されます。

変更履歴

バージョン 説明
8.0.0 引数 queue は、 SysvMessageQueue のインスタンスを期待するようになりました。 これより前のバージョンでは、リソースが期待されていました。

参考

add a note

User Contributed Notes 6 notes

up
5
Muffinman
11 years ago
When sending non-complex (serialize = false) messages to a program in C, you need to add the null character to the string (\0). Otherwise the previous message will be partially visible if it is longer than the current message. Took some kind help from comp.lang.php for me to figure that out. While it seems so obvious now, I thought I'd share it here.
up
5
qeekin at gmail dot com
9 years ago
I created example how to comunnicate with programe written in C throught messages queues. First run C program (it will create queue) 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 Messages Queues with PHP and C program.
* This is simple server which create message queue and receive message from it.
* Based on Beej's Guide to Unix IPC
* Autor: Jan Drazil, <qeekin 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 messages */
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 = ftok("/var/www/index.php", 'G')) == -1) {
perror("ftok");
exit(EXIT_FAILURE);
}

/* Create message queue */
if((msqid = msgget(key, 0666 | IPC_CREAT)) == -1) {
perror("msgget");
exit(EXIT_FAILURE);
}

printf("Ready to get string from PHP!\n");

/* Receive message */
if(msgrcv(msqid, &buf, sizeof(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 message queue */
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 Messages Queues with PHP and C program.
* This is simple server which create message queue and receive message from it.
* Based on Beej's Guide to Unix IPC
* Autor: Jan Drazil, <qeekin at gmail dot com>
*/

/* Generate key, param fot ftok must be same as in test_msg.c */
if(($key = ftok("/var/www/index.php", "G")) == -1)
die(
"ftok");

if(!
msg_queue_exists($key))
die(
"message queue doesn't exists");

/* Connect to message queue */
if(($msqid = msg_get_queue($key)) === FALSE)
die(
"msg_get_queue");

echo
"Sending text to msg queue.\n";

/* Send message to C program */
if(!msg_send($msqid, 12, "Hello from PHP!\0", false))
die(
"msg_send");

echo
"Done"
?>
up
1
michael dot NO dot SP dot AM dot cordover+php at gmail dot com
14 years ago
After about an hour of debugging I've discovered the meaning of the undocumented "PHP Warning: msg_send(): msgsnd failed: Invalid argument" ($errorcode = 13).

This occurred when the size of $message was larger than msg_qbytes (see msg_stat_queue() for how to determine and change msg_qbytes).
up
0
shepik at yandex dot ru
14 years ago
$msgtype used in msg_send function can be any positive integer.
up
-1
webmaster at toolshed51 dot com
20 years ago
Sample sending webpage, see msg_receive for matching service

<?php
$MSGKEY
= 519051;

$msg_id = msg_get_queue ($MSGKEY, 0600);

if (!
msg_send ($msg_id, 1, 'Hi', true, true, $msg_err))
echo
"Msg not sent because $msg_err\n";
?>
up
-1
bryan-boone at msn dot com
15 years ago
I think it is kinda lame why I cannot find out (easily) explicitly which types of messages are allowed and which ones are not. Maybe we can start our own little list. I know that strings work, and arrays do not.
To Top