运行环境准备
用 HTTP 协议发送或者接收消息,请完成以下准备工作。
Windows
- 从 IntelliJ 官网下载并安装 phpStorm 试用版:http://www.jetbrains.com/phpstorm/download/index.html
- 安装完毕之后,打开 phpStorm 开发环境。
其他 IDE 开发环境安装步骤与此类似。
Linux/Unix
从官网上下载 phpStorm 的 Linux 版本:http://www.jetbrains.com/phpstorm/download/index.html
解压下载成功的 phpStorm 安装包: tar xfz PhpStorm-2016.1.tar.gz
进入 phpStorm 的 bin 目录并执行安装脚本: cd phpStorm-2016.1;./phpStorm.sh
在输入注册码页面直接单击试用版。
单击确定直到出现安装完成界面。
运行示例代码
在Windows/Linux/Unix环境下,请按照以下步骤运行示例代码。
在 phpStorm 中创建 PHP 工程(工程名无特殊要求)。
将下文具体示例程序中所提供的配置文件(config.ini)以及示例程序(httpProducer.php, httpConsumer.php, Util.php)拷贝到当前的工程中,如图:
根据示例代码里的说明修改相关配置信息。
右键点击创建的 PHP 文件,选择 Run 执行。
注意:请先执行 httpProducer.php 代码,再执行 httpConsumer.php 代码。
观察执行结果,如果执行结果有问题,请检查 config.ini 配置是否正确。
具体示例程序
以下是配置文件(config.ini)、发送消息(httpProducer.php)和接收消息(httpConsumer.php)以及所用工具方法(Util.php)的示例代码。
1. 配置文件
您需要设置配置文件(config.ini)的相关内容,具体请参考
申请 MQ 资源。
- #您在控制台创建的Topic
- Topic = "xxx"
- #公测环境的URL
- URL = "http://publictest-rest.ons.aliyun.com"
- #阿里云身份验证码
- Ak = "xxx"
- #阿里云身份验证密钥
- Sk = "xxx"
- #MQ控制台创建的Producer ID
- ProducerID = "xxx"
- #MQ控制台创建的Consumer ID
- ConsumerID = "xxx"
说明: URL 中的 Key,Tag 以及 POST Content-Type 没有任何的限制,只要确保 Key 和 Tag 相同唯一即可,可以放在 user.properties 里面。
2. 发送消息示例程序(httpProducer.php)
通过 HTTP 协议发送消息,请参考以下示例代码。
- <?php
- //包含工具类
- include("Util.php");
- /*
- * 消息发布者者
- */
- class HttpProducer
- {
- //签名
- private static $signature = "Signature";
- //在MQ控制台创建的Producer ID
- private static $producerid = "ProducerID";
- //阿里云身份验证码
- private static $aks = "AccessKey";
- //配置信息
- private static $configs = null;
- //构造函数
- function __construct()
- {
- //读取配置信息
- $this::$configs = parse_ini_file("config.ini");
- }
- //计算md5
- private function md5($str)
- {
- return md5($str);
- }
- //发布消息流程
- public function process()
- {
- //打印配置信息
- var_dump($this::$configs);
- //获取Topic
- $topic = $this::$configs["Topic"];
- //获取保存Topic的URL路径
- $url = $this::$configs["URL"];
- //读取阿里云访问码
- $ak = $this::$configs["Ak"];
- //读取阿里云密钥
- $sk = $this::$configs["Sk"];
- //读取Producer ID
- $pid = $this::$configs["ProducerID"];
- //HTTP请求体内容
- $body = "This is a simple php message";
- $newline = "\n";
- //构造工具对象
- $util = new Util();
- for ($i = 0; $i<500; $i++) {
- //计算时间戳
- $date = time()*1000;
- //POST请求url
- $postUrl = $url."/message/?topic=".$topic."&time=".$date."&tag=http&key=http";
- //签名字符串
- $signString = $topic.$newline.$pid.$newline.$this->md5($body).$newline.$date;
- //计算签名
- $sign = $util->calSignature($signString,$sk);
- //初始化网络通信模块
- $ch = curl_init();
- //构造签名标记
- $signFlag = $this::$signature.":".$sign;
- //构造密钥标记
- $akFlag = $this::$aks.":".$ak;
- //标记
- $producerFlag = $this::$producerid.":".$pid;
- //构造HTTP请求头部内容类型标记
- $contentFlag = "Content-Type:text/html;charset=UTF-8";
- //构造HTTP请求头部
- $headers = array(
- $signFlag,
- $akFlag,
- $producerFlag,
- $contentFlag,
- );
- //设置HTTP头部内容
- curl_setopt($ch,CURLOPT_HTTPHEADER,$headers);
- //设置HTTP请求类型,此处为POST
- curl_setopt($ch,CURLOPT_CUSTOMREQUEST,"POST");
- //设置HTTP请求的URL
- curl_setopt($ch,CURLOPT_URL,$postUrl);
- //设置HTTP请求的body
- curl_setopt($ch,CURLOPT_POSTFIELDS,$body);
- //构造执行环境
- ob_start();
- //开始发送HTTP请求
- curl_exec($ch);
- //获取请求应答消息
- $result = ob_get_contents();
- //清理执行环境
- ob_end_clean();
- //打印请求应答结果
- var_dump($result);
- //关闭连接
- curl_close($ch);
- }
- }
- }
- //构造消息发布者
- $producer = new HttpProducer();
- //启动消息发布者
- $producer->process();
- ?>
3. 接收消息示例程序(httpConsumer.php)
通过 HTTP 协议接收消息,请参考以下示例代码。
- <?php
- include ("Util.php");
- /*
- * 消息订阅者
- */
- class HttpConsumer
- {
- //签名
- private static $signature = "Signature";
- //Consumer ID
- private static $consumerid = "ConsumerID";
- //访问码
- private static $ak = "AccessKey";
- //配置信息
- private static $config = null;
- //构造函数
- function __construct()
- {
- //读取配置信息
- $this::$config = parse_ini_file("config.ini");
- }
- //订阅流程
- public function process()
- {
- //打印配置信息
- var_dump($this::$config);
- //获取Topic
- $topic = $this::$config["Topic"];
- //获取Topic的URL路径
- $url = $this::$config["URL"];
- //阿里云身份验证码
- $ak = $this::$config["Ak"];
- //阿里云身份验证密钥
- $sk = $this::$config["Sk"];
- //Consumer ID
- $cid = $this::$config["ConsumerID"];
- $newline = "\n";
- //构造工具对象
- $util = new Util();
- while (true)
- {
- try
- {
- //构造时间戳
- $date = time()*1000;
- //签名字符串
- $signString = $topic.$newline.$cid.$newline.$date;
- //计算签名
- $sign = $util->calSignature($signString,$sk);
- //构造签名标记
- $signFlag = $this::$signature.":".$sign;
- //构造密钥标记
- $akFlag = $this::$ak.":".$ak;
- //标记
- $consumerFlag = $this::$consumerid.":".$cid;
- //构造HTTP请求发送内容类型标记
- $contentFlag = "Content-Type:text/html;charset=UTF-8";
- //构造HTTP头部信息
- $headers = array(
- $signFlag,
- $akFlag,
- $consumerFlag,
- $contentFlag,
- );
- //构造HTTP请求URL
- $getUrl = $url."/message/?topic=".$topic."&time=".$date."&num=32";
- //初始化网络通信模块
- $ch = curl_init();
- //填充HTTP头部信息
- curl_setopt($ch,CURLOPT_HTTPHEADER,$headers);
- //设置HTTP请求类型,此处为GET
- curl_setopt($ch,CURLOPT_CUSTOMREQUEST,"GET");
- //设置HTTP请求URL
- curl_setopt($ch,CURLOPT_URL,$getUrl);
- //构造执行环境
- ob_start();
- //开始发送HTTP请求
- curl_exec($ch);
- //获取请求应答消息
- $result = ob_get_contents();
- //清理执行环境
- ob_end_clean();
- //打印请求应答信息
- var_dump($result);
- //关闭HTTP网络连接
- curl_close($ch);
- //解析HTTP应答信息
- $messages = json_decode($result,true);
- //如果应答信息中的没有包含任何的Topic信息,则直接跳过
- if (count($messages) ==0)
- {
- continue;
- }
- //依次遍历每个Topic消息
- foreach ((array)$messages as $message)
- {
- var_dump($message);
- //获取时间戳
- $date = (int)($util->microtime_float()*1000);
- //构造删除Topic消息URL
- $delUrl = $url."/message/?msgHandle=".$message['msgHandle']."&topic=".$topic."&time=".$date;
- //签名字符串
- $signString = $topic.$newline.$cid.$newline.$message['msgHandle'].$newline.$date;
- //计算签名
- $sign = $util->calSignature($signString,$sk);
- //构造签名标记
- $signFlag = $this::$signature.":".$sign;
- //构造密钥标记
- $akFlag = $this::$ak.":".$ak;
- //构造消费者组标记
- $consumerFlag = $this::$consumerid.":".$cid;
- //构造HTTP请求头部信息
- $delheaders = array(
- $signFlag,
- $akFlag,
- $consumerFlag,
- $contentFlag,
- );
- //初始化网络通信模块
- $ch = curl_init();
- //填充HTTP请求头部信息
- curl_setopt($ch,CURLOPT_HTTPHEADER,$delheaders);
- //设置HTTP请求URL信息
- curl_setopt($ch,CURLOPT_URL,$delUrl);
- //设置HTTP请求类型,此处为DELETE
- curl_setopt($ch,CURLOPT_CUSTOMREQUEST,'DELETE');
- //构造执行环境
- ob_start();
- //开始发送HTTP请求
- curl_exec($ch);
- //获取请求应答消息
- $result = ob_get_contents();
- //清理执行环境
- ob_end_clean();
- //打印应答消息
- var_dump($result);
- //关闭连接
- curl_close($ch);
- }
- }
- catch (Exception $e)
- {
- //打印异常信息
- echo $e->getMessage();
- }
- }
- }
- }
- //构造消息订阅者
- $consumer = new HttpConsumer();
- //启动消息订阅者
- $consumer->process();
- ?>
4. 工具方法(Util.php)示例程序
示例中使用的工具方法如下。
- <?php
- /*
- * 工具类
- */
- class Util
- {
- //计算签名
- public static function calSignature($str,$key)
- {
- $sign = "";
- if(function_exists("hash_hmac"))
- {
- $sign = base64_encode(hash_hmac("sha1",$str,$key,true));
- }
- else
- {
- $blockSize = 64;
- $hashfunc = "sha1";
- if(strlen($key) > $blockSize)
- {
- $key = pack('H*',$hashfunc($key));
- }
- $key = str_pad($key,$blockSize,chr(0x00));
- $ipad = str_repeat(chr(0x36),$blockSize);
- $opad = str_repeat(chr(0x5c),$blockSize);
- $hmac = pack(
- 'H*',$hashfunc(
- ($key^$opad).pack(
- 'H*',$hashfunc($key^$ipad).$str
- )
- )
- );
- $sign = base64_encode($hmac);
- }
- return $sign;
- }
- //计算时间戳
- public static function microtime_float()
- {
- list($usec,$sec) = explode(" ",microtime());
- return ((float)$usec+(float)$sec);
- }
- }
- ?>