IBM Support

Using PHP to access MQ

Question & Answer


Question

Using PHP to access MQ

Answer

On Demand Consulting
Author: Phil Bareham

Introduction

MQ provides support for a number of programming languages, for example C, COBOL and Java. The full list is documented in the MQ Knowledge Centre here:

https://www.ibm.com/support/knowledgecenter/SSFKSJ_8.0.0/com.ibm.mq.dev.doc/q022830_.htm

IBM does not provide a PHP API for MQ. There is a PHP Extension Community Library (PECL) package for that provides a PHP wrapper to the MQ C libraries. Again it must be stressed that IBM does not provide support for this extension.
 

Running PHP MQ Examples

This document describes how to get the put and get example PHP programs supplied with the PHP MQ extension working with the PHP MQ package being downloaded from:

http://pecl.php.net/package/mqseries

Using the instructions found here:

https://blog.phpdeveloper.org/2009/07/29/mqphp-linking-ibms-websphere-mq-to-php/

These steps were carried out on RHEL 6.8 using MQ V8.0 which was already installed but at /opt/mqm80 rather than the default of /opt/mqm we also had the MQClient package installed.
 

Preliminary setup on Linux

We installed the Linux PHP packages and the required dependencies Installed PHP-dev package and dependencies to get the phpize command and downloaded the MQSeries extension from:

http://pecl.php.net/package/mqseries

We used version 0.14.2.
 

Build the MQSeries extension library

The next step was to build the PHP MQSeries library which involved the following commands:

phpize

Which ran OK with little output then:

./configure --with-libdir=lib64

This ran OK when we updated the configure script to add /opt/mqm80 to the SEARCH_PATH= variable on line 4169. Then we ran:

make

Which ran OK and created mqseries_helper.lo mqseries.lo and mqseries.la in /home/gb036159/0PJB/temp/mqseries-0.14.2/mqseries-0.14.2. We found mqseries.so in:

./home/gb036159/0PJB/temp/mqseries-0.14.2/mqseries-0.14.2/.libs/mqseries.so
./home/gb036159/0PJB/temp/mqseries-0.14.2/mqseries-0.14.2/modules/mqseries.so

Next to check the location of the PHP extensions directory we ran:

php -i | grep extension_dir

Which showed the php extension dir to be:

extension_dir => /usr/lib64/php/modules => /usr/lib64/php/modules

We then copied mqseries.so to /usr/lib64/php/modules from /home/gb036159/0PJB/temp/mqseries-0.14.2/mqseries-0.14.2/modules using the command:

sudo cp mqseries.so /usr/lib64/php/modules/mqseries.so

Finally used the php -m command to confirm that the MQSeries extension has been installed.
 

Run a simple PHP MQ program to connect to the queue manager


The instructions linked to above provide a sample PHP program that connects to a queue manager the PHP code is:

#!/usr/bin/php -q
<?php
$mq_host_ip='127.0.0.1';
$queue_name = 'QL_PHP_TEST';
$mq_server = 'IB10QMGR';
$mqcno = array(
'Version' => MQSERIES_MQCNO_VERSION_2
'Options' => MQSERIES_MQCNO_STANDARD_BINDING
'MQCD' => array(
"ChannelName" => "IB10QMGR.SVRCONN"
'ConnectionName' => $mq_host_ip
'TransportType' => MQSERIES_MQXPT_TCP
)
);
// Connect to the MQ server
mqseries_connx($mq_server$mqcno$conn$comp_code$reason);
if ($comp_code !== MQSERIES_MQCC_OK) {
     trigger_error('Cannot open connection to server: ' .$mq_serverE_USER_ERROR);
}else{
     echo 'Connection good!';
}
?>


This sample was modified to suit our local MQ setup the name of our queue manager was IB10QMGR and the channel name was IB10QMGR.SVRCONN. This sample program only connects to the queue manager using MQ client - it doesn't attempt to put or get any messages. We saved this PHP script as php_mq_test and then ran it:

[gb036159@oc6834500546 PHP-MQ]$ ./php_mq_test.php
Connection good!
[gb036159@oc6834500546 PHP-MQ]$

When we stopped the queue manager IB10QMGR and re-ran the test PHP script we saw the following output:

[gb036159@oc6834500546 PHP-MQ]$ ./php_mq_test.php
PHP Fatal error: Cannot open connection to server: IB10QMGR in /media/DE9E18C39E18965D/PHP-MQ/php_mq_test.php on line 19
[gb036159@oc6834500546 PHP-MQ]$

Run the put.php and get.php examples

In the extracted MQSeries PHP package there is an Examples sub-directory which contains a number of PHP MQ samples. We used the put.php and get.php samples. Both of these were modified to change the name of the queue manager to IB10QMGR and the MQ client channel name to IB10QMGR.SVRCONN also the MQ queue name was changed to QL_PHP_TEST.

We ran put.php three times:

[gb036159@oc6834500546 PHP-MQ]$ ./put.php
done.<br>[gb036159@oc6834500546 PHP-MQ]$ ./put.php
done.<br>[gb036159@oc6834500546 PHP-MQ]$ ./put.php
done.<br>[gb036159@oc6834500546 PHP-MQ]$


Then we checked the depth of the QL_PHP_TEST:

gb036159@oc6834500546 PHP-MQ]$ echo "dis ql(QL_PHP_TEST) CURDEPTH" | runmqsc IB10QMGR
5724-H72 (C) Copyright IBM Corp. 1994 2014.
Starting MQSC for queue manager IB10QMGR.


     1 : dis ql(QL_PHP_TEST) CURDEPTH
AMQ8409: Display Queue details.
    QUEUE(QL_PHP_TEST)             TYPE(QLOCAL)
    CURDEPTH(3)
One MQSC command read.
No commands have a syntax error.
All valid MQSC commands were processed.
[gb036159@oc6834500546 PHP-MQ]$

Next we ran get.php to get the messages from the queue we ran this program four times:

[gb036159@oc6834500546 PHP-MQ]$ ./get.php
Hello World<br>[gb036159@oc6834500546 PHP-MQ]$ ./get.php
Hello World<br>[gb036159@oc6834500546 PHP-MQ]$ ./get.php
Hello World<br>[gb036159@oc6834500546 PHP-MQ]$ ./get.php
GET CompCode:2 Reason:2033 Text:No message available.<br><br>[gb036159@oc6834500546 PHP-MQ]$


The fourth run resulted in the MQ reason code 2033 No message available - which is expected because we put three messages to the queue.
 

[{"Business Unit":{"code":"BU053","label":"Cloud & Data Platform"},"Product":{"code":"SSQTW3","label":"IBM On Demand Consulting for Hybrid Cloud"},"Component":"","Platform":[{"code":"PF025","label":"Platform Independent"}],"Version":"All Versions","Edition":"","Line of Business":{"code":"","label":""}}]

Document Information

Modified date:
16 March 2019

UID

ibm10776269