Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP and AMQP RabbitMQ consumer
    primarykey
    data
    text
    <p>I have a working PHP script that publishes messages to RabbitMQ that it parses out of a tab-delimited text file. I literally copy/pasted the working code from that file to another file and wanted to establish a consumer that will retrieve messages published to the exchange, json_decode them and insert them into a database.</p> <p>Every attempt and even copy/paste the sample code from PHP.net site, and even examples in SO, fail with blank white screen and no error messages, then it even kills php-fpm process.</p> <p><strong>Any idea why the queue will not bind and what is going wrong here?</strong></p> <ul> <li>Nginx -> php-fpm</li> <li>PHP 5.3.x</li> <li>Macbook Pro (OSX Lion)</li> <li>RabbitMQ (librabbitmq and pecl amqp installed)</li> </ul> <p>Here is one example of what I tried, but I've tried PHP.net and SO examples on AMQP docs and none work. I can publish fine, but when I try to bind a queue it fails and eventually php-fpm locks up.</p> <pre><code>&lt;?php // Report all PHP errors error_reporting(E_ALL); /***************************************** * MQ settings ****************************************/ $mq = array( 'host' =&gt; 'localhost', 'port' =&gt; 5672, 'login' =&gt; 'guest', 'password' =&gt; 'guest', 'exchange' =&gt; 'gbus.user', 'routing_key' =&gt; 'gbus.test.mike', ); /***************************************** * Connect to queue ****************************************/ $conn_args = array('host' =&gt; $mq['host'], 'port' =&gt; $mq['port'], 'login' =&gt; $mq['login'], 'password' =&gt; $mq['password']); $conn = new AMQPConnection($conn_args); $conn-&gt;connect(); $ch = new AMQPChannel($conn); // Create a new queue $q = new AMQPQueue($ch); $q-&gt;declare('test-queue'); $q-&gt;bind($mq['exchange'],$mq['routing_key']); ?&gt; &lt;br&gt; &lt;font color="blue" face="arial" size="4"&gt;File Contents&lt;/font&gt; &lt;hr&gt; &lt;?php while(true){ $msg=$q-&gt;get(); if ($msg['count']&gt;-1){ echo "\n--------\n"; print_r($msg['msg']); echo "\n--------\n"; } sleep(1); } if (!$conn-&gt;disconnect()) { throw new Exception('Could not disconnect'); } ?&gt; </code></pre> <hr> <p>Here is example of what I used to publish to queue and every time I run this, I view in the RabbitMQ control panel the 20 new messages. I limit to 20 for testing but file has tens of thousands of rows.</p> <p><strong>Working publish code:</strong></p> <pre><code>&lt;?php /***************************************** * MQ settings ****************************************/ $mq = array( 'host' =&gt; 'localhost', 'port' =&gt; 5672, 'login' =&gt; 'guest', 'password' =&gt; 'guest', 'exchange' =&gt; 'gbus.user', 'routing_key' =&gt; 'gbus.test.mike', ); /***************************************** * Connect to queue ****************************************/ $conn_args = array('host' =&gt; $mq['host'], 'port' =&gt; $mq['port'], 'login' =&gt; $mq['login'], 'password' =&gt; $mq['password']); $conn = new AMQPConnection($conn_args); $conn-&gt;connect(); $ch = new AMQPChannel($conn); $ex = new AMQPExchange($ch); $ex-&gt;setName($mq['exchange']); /***************************************** * Parse the file ****************************************/ $filename = "/tmp/Users.txt"; $board = "test"; $fd = fopen ($filename, "r"); $contents = fread ($fd,filesize ($filename)); fclose ($fd); $delimiter = "\r\n"; $rows = explode($delimiter, $contents); $counter = 0; ?&gt; &lt;br&gt; &lt;font color="blue" face="arial" size="4"&gt;File Rows (first 20)&lt;/font&gt; &lt;hr&gt; &lt;?php foreach ( $rows as $row ) { $counter++; echo "&lt;b&gt;Row $counter: &lt;/b&gt; $row&lt;br&gt;"; // build list columns list($login_name, $pwd, $account_type, $access_level, $status, $first_name, $last_name, $agent_code) = explode("\t", $row); // build assoc array for json $user = array("domain"=&gt;$board, "username"=&gt;$login_name, "user_id"=&gt;$agent_code, "password"=&gt;$pwd, "first_name"=&gt;$first_name, "last_name"=&gt;$last_name); // Publish a message to the exchange with a routing key $ex-&gt;publish(json_encode($user), $mq['routing_key'], AMQP_NOPARAM, array("content_type"=&gt;"application/data")); if($counter == 20) { break; } } $ch-&gt;close(); $conn-&gt;close(); ?&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload