阿里云的大神们好像是JAVA出身,很多文档都是根据JAVA习惯或规范写的,对我们这种非JAVA的来说,非常不方便。
接合文档和[售后工程师帮助重写了AliONS类,希望对后来者有帮助,少走弯路。也希望大神们看看HTTP协议文档。
<?php
header('Content-Type: text/html; charset=utf-8');
// ====== 测试DEMO =========================
$config = array();
$config['AK'] = ''; // Access Key ID
$config['SK'] = ''; // Access Key Secret
$config['Topic'] = 'phpbin_2016';
$config['PID'] = 'PID_phpbin';
$config['CID'] = 'CID_phpbin';
$AM = new AliONS($config);
//$data = $AM->send('aaa');
//$data = $AM->revice();
$data = $AM->delete('X1BFTkRJTkdNU0dfI3BocGJpbl8yMDE2I3FkaW50ZXJuZXQtMDMjMiM0I0NJRF9waHBiaW4=');
echo '<pre>';
var_dump($data);
exit();
// ==========================================
/**
* 阿里云消息服务(ONS)
* 文档:
* https://help.aliyun.com/document_detail/ons/sdk/java-sdk/ons-http-rule.html?spm=5176.docons/sdk/java-sdk/set-env.6.101.hwHPLu
*
* @author phpbin
*
*/
class AliONS
{
/**
* 服务地址(公测)
*
* @var string
*/
private $_URL = 'http://publictest-rest.ons.aliyun.com';
/**
* Access Key
*
* @var string
*/
private $_AK;
/**
* SecretAccessKey
*
* @var string
*/
private $_SK;
/**
* TopIC
*
* @var string
*/
private $_Topic;
/**
* 生产者ID
*
* @var string
*/
private $_PID;
/**
* 订阅者ID
*
* @var string
*/
private $_CID;
/**
* 时间(毫秒数)
*
* @var string
*/
private $_Time;
/**
* 签名
*
* @var string
*/
private $_Sign;
/**
* 初始
*
*/
public function __construct($config = array())
{
foreach ( $config as $key=>$val)
{
$this->{"_".$key} = $val;
}
$this->_Time = $this->_microtime();
}
/**
* 发送消息
*
* @param string $body 消息内容
* @return array
*/
public function send($body)
{
$url = $this->_URL."/message/?topic=".$this->_Topic."&time=".$this->_Time."&tag=http&key=http";
$signStr = $this->_Topic."\n".$this->_PID."\n".md5($body)."\n".$this->_Time;
$this->_signature($signStr);
$stream = $this->_html($url, $body, 'POST');
return json_decode($stream, true);
}
/**
* 接收消息
* 说明:没有消息返回空数组
*
* @return array
*/
public function revice()
{
$url = $this->_URL."/message/?topic=".$this->_Topic."&time=".$this->_Time."&num=32";
$signStr = $this->_Topic."\n".$this->_CID."\n".$this->_Time;
$this->_signature($signStr);
$stream = $this->_html($url, '', 'GET');
return json_decode($stream, true);
}
/**
* 删除消息
* 说明:删除成功返回空消息
*
* @param string $msgHandle 消息标识
*/
public function delete($msgHandle)
{
$url = $this->_URL."/message/?msgHandle=".$msgHandle."&topic=".$this->_Topic."&time=".$this->_Time;
$signStr = $this->_Topic."\n".$this->_CID."\n".$msgHandle."\n".$this->_Time;
$this->_signature($signStr);
$stream = $this->_html($url, '', 'DELETE');
return json_decode($stream, true);
}
/**
* 生成签名
*
* @param string $signStr
* @return string
*/
private function _signature($signStr)
{
$this->_Sign = base64_encode(hash_hmac('sha1', $signStr, $this->_SK, true));
}
/**
* 生成毫秒时间
*
* @return integer
*/
private function _microtime(){
$time = explode(" ", microtime () );
$time = $time[1].($time [0] * 1000);
$time2 = explode(".", $time );
$time = $time2[0];
return $time;
}
/**
* 提交请求数据
*
* @param string $url 地址
* @param string $post 请求体
* @param string $method 请求类型 POST|PULL|GET|DELETE
* @return mixed
*/
private function _html($url, $post='', $method='POST')
{
$header = array();
$header[] = 'User-Agent: Jetty/9.3.4.RC';
$header[] = 'Host:publictest-rest.ons.aliyun.com';
$header[] = 'AccessKey:'.$this->_AK;
$header[] = 'Signature:'.$this->_Sign;
$header[] = 'ProducerId:'.$this->_PID;
$header[] = 'ConsumerId:'.$this->_CID;
$header[] = 'Content-Type: text/plain;charset=UTF-8';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_TIMEOUT, 100);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
if ( $method) curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
if ( $post) curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$html = curl_exec($ch);
curl_close($ch);
return $html;
}
}
/** End of Class AliONS **/
/** End of file AliONS.php **/
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。
涵盖 RocketMQ、Kafka、RabbitMQ、MQTT、轻量消息队列(原MNS) 的消息队列产品体系,全系列产品 Serverless 化。RocketMQ 中文社区:https://rocketmq-learning.com/