1, the installation amqp expand the installation process
2, download the toolkit php-amqplib
composer require php-amqplib/php-amqplib
3, the code follows
[Consumer] news
. 1 <? PHP 2 // configuration information . 3 $ conn_args = Array ( . 4 'Host' => '127.0.0.1', . 5 'Port' => '5672', . 6 'Login' => 'ZCW', . 7 'password '=>' 123456 ', . 8 ' Vhost '=>' / ' . 9 ); 10 $ The e_name =' Exchange1 '; // switch name . 11 $ q_name =' Queue1 '; // name of the queue 12 is $ k_route =' Route1 ' ;// Routing Key 13 14 // create a connection and Channel 15 $conn = new AMQPConnection($conn_args); 16 if (!$conn->connect()) { 17 die("Cannot connect to the broker!\n"); 18 } 19 $channel = new AMQPChannel($conn); 20 21 //创建交换机 22 $ex = new AMQPExchange($channel); 23 $ex->setName($e_name); 24 $ex->setType(AMQP_EX_TYPE_DIRECT); // Direct type (commonly used fanout, Direct, Topic, headers) 25 $ EX -> setFlags (AMQP_DURABLE); // lasting 26 is 27 28 // create queue 29 $ Q = new new AMQPQueue ( $ Channel ); 30 $ Q -> the setName ( $ q_name ); 31 is $ Q -> setFlags (AMQP_DURABLE); // persistence 32 33 is $ Total = $ Q -> declareQueue (); // get all of the message number 34 is 35 / / switch and binding queue and the specified route key 36 $ Q -> the bind (The e_name $ , $ k_route ); 37 [ 38 is // . 1, the blocking message receiving mode 39 the while ( True ) { 40 $ Q -> Consume ( 'the processMessage' ); 41 is // $ Q-> Consume ( 'the processMessage', AMQP_AUTOACK) ; // automatic ACK response 42 } 43 is 44 is // 2 non-blocking mode can receive messages regularly call 45 // IF (Total $) { 46 is // for ($ I = 0; $ I <$ Total; $ I ++) { 47 // $ $ = Q- Envelope> GET (); 48 // IF ($ Envelope) { 49 // envelope- $ $ MSG => the getBody (); 50 // echo $ msg "\ n" ;. // process the message 51 is // $ Q-> ACK ($ envelope-> getDeliveryTag ()); // manual transmission ACK response 52 //} 53 //} 54 // } 55 56 is $ Conn -> the disconnect (); 57 is 58 / * * 59 * consumption callback function 60 * process message 61 is * / 62 is function the processMessage ( $ Envelope , $ Queue ) { 63 is $ MSG = $ Envelope -> the getBody () ; 64 echo $ MSG . "\ n-"; // process the message 65 $ Queue -> ACK ( $ Envelope -> getDeliveryTag ()); // manually send ACK response 66 } 67 >?
[Production News Service
. 1 <? PHP 2 // configuration information . 3 $ conn_args = Array ( . 4 'Host' => '127.0.0.1', . 5 'Port' => '5672', . 6 'Login' => 'ZCW', . 7 'password '=>' 123456 ', . 8 ' Vhost '=>' / ' . 9 ); 10 $ The e_name =' Exchange1 '; // switch name . 11 // $ q_name =' Queue1 '; // without queue name 12 is $ k_route = 'route1';// Routing Key 13 14 // create a connection and Channel 15 $ conn = new newAMQPConnection ( $ conn_args ); 16 IF (! $ Conn -> Connect ()) { . 17 Die ( "Can Not Connect to The Broker \ n-!" ); 18 is } . 19 $ Channel = new new AMQPChannel ( $ Conn ); 20 is // Create Object switch 21 is $ EX = new new AMQPExchange ( $ Channel ); 22 is $ EX -> the setName ( $ The e_name ); 23 is // send message 24 // $ Channel-> the startTransaction (); // start transaction 25 for($i=0; $i<5; ++$i){ 26 $ex->publish($message, $k_route)."\n"; 27 } 28 29 //$channel->commitTransaction(); //提交事务 30 31 $conn->disconnect(); 32 33 ?>