概述
本文主要介绍如何通过 PHP SDK 连接阿里云AMQP服务。
测试过程
1、composer.json
{
"require" : {
"php-amqplib/php-amqplib" : "^2.9"
},
"require-dev" : {
"phpunit/phpunit" : "^5"
}
}
2、send code
<?php
require_once __DIR__ . '/vendor/autoload.php';
use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Message\AMQPMessage;
include("AliyunCredentialsProvider.php");
/*接入点*/
$host = "18********617278.mq-amqp.cn-hangzhou-a.aliyuncs.com";
/*默认端口*/
$port = 5672;
/*资源隔离*/
$virtualHost = "yutaoamqptest";
/*阿里云的accessKey*/
$accessKey = "********";
/*阿里云的accessSecret*/
$accessSecret = "********";
/*主账号id*/
$resourceOwnerId = '18********617278';
$connectionUtil = new ConnectionUtil($host, $port, $virtualHost, $accessKey, $accessSecret, $resourceOwnerId);
$connection = $connectionUtil->getConnection();
echo "dwe";
$channel = $connection->channel();
$channel->queue_declare('queuephp', false, false, false, false);
$msg = new AMQPMessage('Hello World!');
$channel->basic_publish($msg, '', 'queuephp');
echo " [x] Sent 'Hello World!'\n";
$channel->close();
$connection->close();
?>
4、receive code
<?php
require_once __DIR__ . '/vendor/autoload.php';
use PhpAmqpLib\Connection\AMQPStreamConnection;
include("AliyunCredentialsProvider.php");
/*接入点*/
$host = "18********617278.mq-amqp.cn-hangzhou-a.aliyuncs.com";
/*默认端口*/
$port = 5672;
/*资源隔离*/
$virtualHost = "yutaoamqptest";
/*阿里云的accessKey*/
$accessKey = "********";
/*阿里云的accessSecret*/
$accessSecret = "********";
/*主账号id*/
$resourceOwnerId = '18********617278';
$connectionUtil = new ConnectionUtil($host, $port, $virtualHost, $accessKey, $accessSecret, $resourceOwnerId);
$connection = $connectionUtil->getConnection();
$channel = $connection->channel();
$channel->queue_declare('queuephp', false, false, false, false);
echo " [*] Waiting for messages. To exit press CTRL+C\n";
$callback = function ($msg) {
echo ' [x] Received ', $msg->body, "\n";
};
$channel->basic_consume('queuephp', '', false, true, false, false, $callback);
while (count($channel->callbacks)) {
$channel->wait();
}
$channel->close();
$connection->close();
?>
6、AliyunCredentialsProvider.php
<?php
use PhpAmqpLib\Connection\AMQPStreamConnection;
Class ConnectionUtil {
private $host;
private $port;
private $virtualHost;
private $accessKey;
private $accessSecret;
private $resourceOwnerId;
function __construct($host, $port, $virtualHost, $accessKey,
$accessSecret, $resourceOwnerId) {
$this->host = $host;
$this->port = $port;
$this->virtualHost = $virtualHost;
$this->accessKey = $accessKey;
$this->accessSecret = $accessSecret;
$this->resourceOwnerId = $resourceOwnerId;
}
private function getUser() {
$t = '0:' . $this->resourceOwnerId . ':' . $this->accessKey;
return base64_encode($t);
}
private function getPassword() {
$ts = (int)(microtime(true)*1000);
$value = utf8_encode($this->accessSecret);
$key = utf8_encode((string)$ts);
$sig = strtoupper(hash_hmac('sha1', $value, $key, FALSE));
return base64_encode(utf8_encode($sig . ':' . $ts));
}
public function getConnection() {
$username = $this->getUser();
$password = $this->getPassword();
return new AMQPStreamConnection($this->host, $this->port,
$username, $password,
$this->virtualHost, false);
}
}
?>