PHP重写的消息队列MQ类-问答-阿里云开发者社区-阿里云

开发者社区> 问答> 正文

PHP重写的消息队列MQ类

gia114 2016-04-01 17:46:15 9757
阿里云的大神们好像是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 **/

消息中间件 Java PHP 开发工具
分享到
取消 提交回答
全部回答(2)
  • ymy
    2017-01-15 15:15:13
    RePHP重写的消息队列MQ类
    你好,我遇到一个问题,就是可以发送,但是在调用接收消息$data = $AM->revice();时候,总是返回 空
    0 0
  • 西秦说云
    2016-04-02 06:34:44
    感谢分享!!!!!!!!!!!
    0 0
添加回答

集结各类场景实战经验,助你开发运维畅行无忧

推荐文章
相似问题