MQSeries PHP ExtentionHome
PECL home pagedownload Documentation

PHP Samples.

Samples to get PHP working with the mqseries client extention for PHP.
For installation instructions see getting started.

First we have to connect to a running queue manager. This can be done either through the mqseries_conn or mqseries_connx. The mqseries_connx has more option for specifying the client channel, qmanager name, location etc. When using mqseries_conn the environment must be setup correctly for it to succeed. When running in a shared apache environment this might not be possible to do. Thats when the mqseries_connx method comes to help.

To start with we define an array with connect options. The original IBM API uses C-structs to communicate with the underlying MQSeries API. We cannot use these same structs in our PHP script. The way it is solved here is to use an array of key<->value pairs where the key is the name of the corresponding struct field name. For values, names etc. see also the IBM Redbooks.

$mqcno = array(
		'StrucId' => MQSERIES_MQCNO_STRUC_ID,
		'Version' => MQSERIES_MQCNO_VERSION_2,
		'Options' => MQSERIES_MQCNO_STANDARD_BINDING,
		'MQCD' => array('ChannelName' => 'D800MQ.CLIENT',
				'ConnectionName' => 'localhost',
				'TransportType' => MQSERIES_MQXPT_TCP)
		);
Once that is setup we can call the mqseries_connx method.
mqseries_connx('D800MQ', &$mqcno, &$conn, &$comp_code,&$reason);
if ($comp_code !== MQSERIES_MQCC_OK) {		
	printf("CompCode:%d Reason:%d Text:%s", 
			$comp_code, 
			$reason, 
			mqseries_strerror($reason));
}

We have to pass in the connect options but also the other parameters by reference. This allows the method to return more then one value. The most important one is the $conn. This represents the MQSerie connection handle. All subsequent method calls need this handle to call the correct queue manager. It is possible to open more then one queue manager.

Next two calls are to illustrate how different mqseries_open methods work. The first opens an object of type queue manager for inquiry. The returned $obj handle can then be use as an input parameter when calling the inq method.

The second open simply opens a queue residing on the connected queue manager. This can either be a local queue, remote queue or even a queue alias.

$mqods = array('ObjectType' => MQSERIES_MQOT_Q_MGR);
mqseries_open($conn, &$mqods, 
              MQSERIES_MQOO_INQUIRE | MQSERIES_MQOO_FAIL_IF_QUIESCING,
              &$obj, &$comp_code,&$reason);
if ($comp_code !== MQSERIES_MQCC_OK) {
	printf("CompCode:%d Reason:%d Text:%s", 
			$comp_code, 
			$reason, 
			mqseries_strerror($reason));
}

$mqods = array('ObjectName' => 'TESTQ', 'ObjectQMgrName' => 'D800MQ');
mqseries_open(
	$conn,
	&$mqods,
	MQSERIES_MQOO_INQUIRE 
		| MQSERIES_MQOO_INPUT_AS_Q_DEF 
		| MQSERIES_MQOO_FAIL_IF_QUIESCING 
		| MQSERIES_MQOO_OUTPUT,
	&$obj_snd,
	&$comp_code,
	&$reason);
if ($comp_code !== MQSERIES_MQCC_OK) {
	printf("CompCode:%d Reason:%d Text:%s", 
			$comp_code, 
			$reason, 
			mqseries_strerror($reason));
	exit;
}

Important here is the $obj and $obj_snd parameters. These are "call by reference" passed to the open methods. Afther successful completion of the method these hold the MQSeries object handle. Which should be used when calling methods that work on mqseries objects.

In the next part an inquiry is made on the Queue Manager object and on the Queue object.

$int_attr = array();
$char_attr = "";

mqseries_inq($conn, $obj, 
		1, array(MQSERIES_MQCA_Q_MGR_NAME), 
		0, &$int_attr, 
		48, &$char_attr, 
		&$comp_code, &$reason);

if ($comp_code !== MQSERIES_MQCC_OK) {
	printf("CompCode:%d Reason:%d Text:%s", 
			$comp_code, 
			$reason, 
			mqseries_strerror($reason));
} else {
	echo "INQ QManager name result ".$char_attr."";
} 

mqseries_inq($conn, $obj_snd, 
		2, array(MQSERIES_MQCA_Q_NAME,MQSERIES_MQIA_MAX_Q_DEPTH), 
		1, &$int_attr, 
		48, &$char_attr, 
		&$comp_code, &$reason);

if ($comp_code !== MQSERIES_MQCC_OK) {
	printf("CompCode:%d Reason:%d Text:%s", 
			$comp_code, 
			$reason, 
			mqseries_strerror($reason));
} else {
	echo "INQ TESTQ result qname="
			.$char_attr
			." max queue depth="
			.$int_attr[0]." ";
} 

Note the 48 as the size of the expected character attribute. This is not the size of the &char_attr itself as in PHP we cannot specify a buffer. The parameter is needed to allocate enough memory that the method can succeed.

Finally we close the opened objects. Garbage collection is also done by the extention not closing object or connection will be closed by the extention.

mqseries_close($conn, $obj, 
               MQSERIES_MQCO_NONE, &$comp_code, &$reason);
if ($comp_code !== MQSERIES_MQCC_OK) {
	printf("CompCode:%d Reason:%d Text:%s\n", 
			$comp_code, 
			$reason, 
			mqseries_strerror($reason));
}

And finally we close the connection

mqseries_disc($conn, &$comp_code, &$reason);
if ($comp_code !== MQSERIES_MQCC_OK) {
	printf("CompCode:%d Reason:%d Text:%s\n", 
			$comp_code, 
			$reason, 
			mqseries_strerror($reason));
}