HttpProducer.php
3.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<?php
/**
* Created by PhpStorm.
* User: billy
* Date: 25/04/2017
* Time: 7:58 PM
*/
namespace App\Common\MQ;
use App\Common\Http;
/**
* Class HttpProducer
* @package App\Common\MQ
*/
class HttpProducer
{
//签名
private static $signature = "Signature";
//在MQ控制台创建的Producer ID
private static $producerId = "ProducerID";
//阿里云身份验证码
private static $aks = "AccessKey";
/**
* @var
*/
private $url;
/**
* @var
*/
private $ak;
/**
* @var
*/
private $sk;
/**
* @var
*/
private $pid;
/**
* @var
*/
private $topic;
/**
* @var
*/
private $content;
/**
* @var
*/
private $tag;
/**
* @var
*/
private $key;
/**
* HttpProducer constructor.
* @param $url
* @param $ak
* @param $sk
* @param $pid
* @param $topic
* @param $content
* @param $tag
* @param $key
*/
public function __construct($url, $ak, $sk, $pid, $topic, $content, $tag, $key)
{
$this->url = $url;
$this->ak = $ak;
$this->sk = $sk;
$this->pid = $pid;
$this->topic = $topic;
$this->content = $content;
$this->tag = $tag;
$this->key = $key;
}
/**
* @param array $headerParam
* @param array $urlParam
* @return mixed
*/
public function produce($headerParam=array(), $urlParam=array())
{
$date = time()*1000;
$newline = "\n";
//POST请求url
$postUrl = $this->url."/message/?topic=".$this->topic."&time=".$date."&tag=".$this->tag."&key=".$this->key;
foreach ($urlParam as $key=>$value)
{
$postUrl.='&'.$key.'='.$value;
}
//签名字符串
$signString = $this->topic.$newline.$this->pid.$newline.md5($this->content).$newline.$date;
//计算签名
$sign = Util::calSignature($signString,$this->sk);
//构造签名标记
$signFlag = $this::$signature.":".$sign;
//构造密钥标记
$akFlag = $this::$aks.":".$this->ak;
//标记
$producerFlag = $this::$producerId.":".$this->pid;
//构造HTTP请求头部内容类型标记
$contentFlag = "Content-Type:text/html;charset=UTF-8";
//构造HTTP请求头部
$headers = array(
$signFlag,
$akFlag,
$producerFlag,
$contentFlag,
);
foreach ($headerParam as $key=>$value)
{
array_push($headers, $key.':'.$value);
}
$result = Http::PostWithHeader($this->content, $headers, $postUrl);
$result = json_decode($result, true);
return $result;
}
/**
* @param $delayTime
* @return mixed
*/
public function produceDelayMessage($delayTime)
{
$deliveryTime = time()*1000 + $delayTime * 1000;
$param['startdelivertime'] = $deliveryTime;
return $this->produce(array(), $param);
}
/**
* @param $shardingKey
* @return mixed
*/
public function produceOrderMessage($shardingKey)
{
$param = array();
$param['isOrder'] = true;
$param['shardingKey'] = $shardingKey;
return $this->produce($param);
}
}