php-amqplib是AMQP的一个PHP开源实现,而AMQP是一个异步消息传递所使用的应用层协议规范,并且是作为线路层协议,而不是API,AMQP客户端能够无视消息的来源任意发送和接受信息。
php-amqplib是什么?
php-amqplib是Advanced Message Queuing Protocol (AMQP)的一个PHP开源实现。
高级消息队列协议(AMQP)是一个异步消息传递所使用的应用层协议规范。作为线路层协议,而不是API(例如JMS),AMQP 客户端能够无视消息的来源任意发送和接受信息。
php-amqplib库操作RabbitMQ
RabbitMQ基本原理
首先,建议去大概了解下RabbitMQ(以下简称mq)的基本工作原理,可以参考这篇文章
最主要的几个对象如下
对象名称 | |
---|---|
borker | 相当于mq server |
channel | 通道或者频道 |
exchange | 交换机 |
queue | 队列 |
vhost | 虚拟主机(项目队列隔离使用) |
这几个对象在上面的文章说描述的已经非常清楚,这里不要叙述了。
安装操作库
在你的项目目录下的composer.json文件中增加下面内容
{ "require": { "php-amqplib/php-amqplib": "2.7.*" //增加这行 } }
然后接着执行composer update php-amqplib/php-amqplib
。更加方便的做法是如果你的项目已经有了composer.json那么执行执行
composer require php-amqplib/php-amqplib
便可以直接进行安装了
示例演示
首先需要定义交换机、队列以及路由关键字(routing key) 下面是示例代码
<?php require 'vendor/autoload.php'; use PhpAmqpLib\Connection\AMQPStreamConnection; use PhpAmqpLib\Message\AMQPMessage; $conf = [ 'host' => '127.0.0.1', 'port' => 5672, 'user' => 'kd_dev', 'pwd' => 'kd_dev', 'vhost' => '/', ]; $exchangeName = 'kd_sms_send_ex'; //交换机名 $queueName = 'kd_sms_send_q'; //队列名称 $routingKey = 'sms_send'; //路由关键字(也可以省略) $conn = new AMQPStreamConnection( //建立生产者与mq之间的连接 $conf['host'], $conf['port'], $conf['user'], $conf['pwd'], $conf['vhost'] ); $channel = $conn->channel(); //在已连接基础上建立生产者与mq之间的通道 $channel->exchange_declare($exchangeName, 'direct', false, true, false); //声明初始化交换机 $channel->queue_declare($queueName, false, true, false, false); //声明初始化一条队列 $channel->queue_bind($queueName, $exchangeName, $routingKey); //将队列与某个交换机进行绑定,并使用路由关键字 $msgBody = json_encode(["name" => "iGoo", "age" => 22]); $msg = new AMQPMessage($msgBody, ['content_type' => 'text/plain', 'delivery_mode' => 2]); //生成消息 $r = $channel->basic_publish($msg, $exchangeName, $routingKey); //推送消息到某个交换机 $channel->close(); $conn->close();
有几个地方需要注意:
1、$routingKey其实是可以省略的,但是一般都带上方便交换机对消息进行不同队列的推送
2、如果绑定的时候使用了routingKey,那么在basicPublish的时候也要指定routingKey,不然交换机无法路由到指定队列,默认就推送到不使用关键字的队列了(这在我实验的时候遇到的一个坑)
3、上面的exchange_declare和queue_declare以及queue_bind其实也不是必须的,如果在代码运行之前这行交换机和队列名称以及通过管理后台的方式手动添加在mq上,那么可以执行使用,而不需要上面的这3句代码。
执行上面的代码后你也可以在mq管理后台看到对应的显示,如下图
添加后的交换机显示
添加后的队列显示
队列与交换机的绑定关系,以及绑定的路由关键字
路由匹配
上面的代码中,当我们声明初始化交换机的时候第二个参数使用direct参数,其实还有另外3种参数可选。分别为
规则 | 说明 |
---|---|
direct | 精准推送 |
fanout | 广播。推送到绑定到此交换机下的所有队列 |
topic | 组播。比如上面我绑定的关键字是sms_send,那么他可以推送到*.sms_send的所有队列 |
headers | 这个目前不知道是如何推送的 |
---更新---
在创建交换机和队列的时候各个常用参数说明
name: $queue // should be unique in fanout exchange. [队列名称] passive: false // don't check if a queue with the same name exists [是否检测同名队列] durable: false // the queue will not survive server restarts [是否开启队列持久化] exclusive: false // the queue might be accessed by other channels [队列是否可以被其他队列访问] auto_delete: true //the queue will be deleted once the channel is closed. [通道关闭后是否删除队列]
name: $exchange [交换机名称] type: direct [路由类型] passive: false [] durable: true [交换机是否开启持久化] auto_delete: false //the exchange won't be deleted once the channel is closed.
更多相关知识,请访问 PHP中文网!!

To protect the application from session-related XSS attacks, the following measures are required: 1. Set the HttpOnly and Secure flags to protect the session cookies. 2. Export codes for all user inputs. 3. Implement content security policy (CSP) to limit script sources. Through these policies, session-related XSS attacks can be effectively protected and user data can be ensured.

Methods to optimize PHP session performance include: 1. Delay session start, 2. Use database to store sessions, 3. Compress session data, 4. Manage session life cycle, and 5. Implement session sharing. These strategies can significantly improve the efficiency of applications in high concurrency environments.

Thesession.gc_maxlifetimesettinginPHPdeterminesthelifespanofsessiondata,setinseconds.1)It'sconfiguredinphp.iniorviaini_set().2)Abalanceisneededtoavoidperformanceissuesandunexpectedlogouts.3)PHP'sgarbagecollectionisprobabilistic,influencedbygc_probabi

In PHP, you can use the session_name() function to configure the session name. The specific steps are as follows: 1. Use the session_name() function to set the session name, such as session_name("my_session"). 2. After setting the session name, call session_start() to start the session. Configuring session names can avoid session data conflicts between multiple applications and enhance security, but pay attention to the uniqueness, security, length and setting timing of session names.

The session ID should be regenerated regularly at login, before sensitive operations, and every 30 minutes. 1. Regenerate the session ID when logging in to prevent session fixed attacks. 2. Regenerate before sensitive operations to improve safety. 3. Regular regeneration reduces long-term utilization risks, but the user experience needs to be weighed.

Setting session cookie parameters in PHP can be achieved through the session_set_cookie_params() function. 1) Use this function to set parameters, such as expiration time, path, domain name, security flag, etc.; 2) Call session_start() to make the parameters take effect; 3) Dynamically adjust parameters according to needs, such as user login status; 4) Pay attention to setting secure and httponly flags to improve security.

The main purpose of using sessions in PHP is to maintain the status of the user between different pages. 1) The session is started through the session_start() function, creating a unique session ID and storing it in the user cookie. 2) Session data is saved on the server, allowing data to be passed between different requests, such as login status and shopping cart content.

How to share a session between subdomains? Implemented by setting session cookies for common domain names. 1. Set the domain of the session cookie to .example.com on the server side. 2. Choose the appropriate session storage method, such as memory, database or distributed cache. 3. Pass the session ID through cookies, and the server retrieves and updates the session data based on the ID.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

SublimeText3 Chinese version
Chinese version, very easy to use

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Mac version
God-level code editing software (SublimeText3)