星期二, 9月 29, 2009

PHP5 下使用IPC 不能用的問題

Error Message:
  • PHP Fatal error: Call to undefined function msg_get_queue() in /var/www/html/send.php
  • 檢查 /usr/lib/php/modules/ 缺
    • sysvmsg.so
    • sysvshm.so
    • sysvsem.so

Version:
  • PHP 5.2.9 (cli) (built: Apr 15 2009 09:30:33)

Fix:
  • yum install php-process

Recv:
<?php
$MSGKEY = 519051; // Message

$msg_id = msg_get_queue ($MSGKEY, 0600);

while (1) {
if (msg_receive ($msg_id, 1, $msg_type, 16384, $msg, true, 0, $msg_error)) {
if ($msg == 'Quit') break;
echo "$msg\n";
} else {
echo "Received $msg_error fetching message\n";
break;
}
}

msg_remove_queue ($msg_id);
?>



Send:
<?php
if ( sizeof($argv)<2 ) {
echo "Usage: $argv[0] stat|send|receive|remove msgType MSG [msg] \n\n" ;
echo " EX: $argv[0] send 1 \"This is no 1\" \n" ;
echo " $argv[0] receive ID \n" ;
echo " $argv[0] stat \n" ;
echo " $argv[0] remove \n" ;
exit;
}

$MSGKey = "519051" ;

## Create or attach to a message queue
$seg = msg_get_queue($MSGKey) ;

switch ( $argv[1] ) {
case "send":
msg_send($seg, $argv[2], $argv[3]);
echo "msg_send done...\n" ;
break;

case "receive":
$stat = msg_stat_queue( $seg );
echo 'Messages in the queue: '.$stat['msg_qnum']."\n";
if ( $stat['msg_qnum']>0 ) {
msg_receive($seg, $argv[2], $msgtype, 1024, $data);
var_dump($msgtype);
var_dump($data);
echo "\n";
}
else {
echo "No Msg...\n";
}
break;

case "stat":
print_r( msg_stat_queue($seg) );
break;

case "remove":
msg_remove_queue($seg);
break;
}
?>


Reference:
  • http://tw.php.net/manual/en/function.msg-receive.php

沒有留言: