activeMQ 与 php的结合使用
来源:互联网 发布:骂人 知乎 编辑:程序博客网 时间:2024/06/05 09:53
1、首先需要安装stomp的php扩展,stomp5.6,将该文件放入php的扩展库ext文件夹中。
2、在php.ini中加入扩展配置:extension=php_stomp.dll,然后重启服务。
3、activeMQ主要有两个操作,即生产端与消费端
生产端代码如下:
<?phpuse Yii;use app\components\Curl;class ProductClass{private $user = "admin";private $password = "admin";private $host = "10.100.200.20";private $port = "8161";private $destination = "queue://smssykjyzmqueue"; // 目的地址private $apipubaddr = "/api/message?"; // api发布地址private $codeMsg = array('20000'=>'发布成功','20001'=>'发布内容不能为空','20002'=>'发布内容格式不对','20003'=>'stomp 未连接成功','20004'=>'发布内容失败','20005'=>'其它错误',);/*** @param $body json 发送的数组内容* @param null $destination* @return string* @throws \app\components\InvalidParamException*/public function sendQueue($body,$destination=null){try {if(empty($body)){return $this->beJson('20001');}// 传输过来的不是jsonif(!Curl::is_json($body)){return $this->beJson('20002');}$to = ($destination && is_string($destination)) ? $destination : $this->destination;$url = 'http://' . $this->user . ':' . $this->password . '@' . $this->host . ":" . $this->port .$this->apipubaddr.'destination='. $to;$res = Curl::http($url, ['body'=>$body], 'post');if($res != 'Message sent'){return $this->beJson('20005');}} catch(StompException $e) {return $this->beJson('20003');}return $this->beJson('20000');}/*** 返回值* @param $code* @return string*/public function beJson($code){return json_encode(array('errcode'=>$code,'errmsg'=>$this->codeMsg[$code]),JSON_UNESCAPED_UNICODE);exit;}}$body = '{ "platform":"6", "sign":"测试", "mobile":"15820786542", "content":"您好!您的验证码为122223", "type":"1", "callback":"http://lib.sms.com/v1/sms-api-send/sms-send", "taskid":"20161031111134170349"}';$obj = new ProductClass();$obj->sendQueue($body);
消费端代码如下:
<?phpdefine('QUEUE', 'smssykjyzmqueue');$user = getenv("ACTIVEMQ_USER");if (!$user) $user = "admin";$password = getenv("ACTIVEMQ_PASSWORD");if (!$password) $password = "admin";$host = getenv("ACTIVEMQ_HOST");if (!$host) $host = "10.100.200.20";$port = getenv("ACTIVEMQ_PORT");if (!$port) $port = 61613;$destination = '/queue/' . QUEUE;try { $url = 'tcp://' . $host . ":" . $port; $stomp = new Stomp($url, $user, $password); $stomp->subscribe($destination); while (true) { if ($stomp->hasFrame()) { $frame = $stomp->readFrame(); if ($frame != NULL) { // 收到的数据为 $frame->body $res = json_decode($frame->body, true); $url = $res['callback']; unset($res['callback']); $res = json_encode($res,JSON_UNESCAPED_UNICODE); $data = http($url, $res, "raw"); // 根据回调地址执行短信发送 $dataArr = json_decode($data, true); if($dataArr['errcode'] == 20000) { file_put_contents('./'.date('Y-m-d').'sykjyzmsmsSend.txt',date('Y-m-d H:i:s')."发送成功------ 参数".$res."--返回值--".$data."\r\n",FILE_APPEND); }else{ file_put_contents('./'.date('Y-m-d').'sykjyzmsmsSend.txt',date('Y-m-d H:i:s')."发送失败------ 参数".$res."--返回值--".$data."\r\n",FILE_APPEND); } $stomp->ack($frame); } } }} catch(StompException $e) { echo $e->getMessage();}function http($url, $params = null, $type = 'get'){ $curl = curl_init(); switch ($type) { case 'get': is_array($params) && $params = http_build_query($params); !empty($params) && $url .= (stripos($url, '?') === false ? '?' : '&') . $params; break; case 'post': curl_setopt($curl, CURLOPT_POST, true); if (!is_array($params)) { throw new InvalidParamException("Post data must be an array."); } curl_setopt($curl, CURLOPT_POSTFIELDS, $params); break; case 'raw': curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json')); curl_setopt($curl, CURLOPT_POST, true); if (is_array($params)) { is_array($params) && $params = json_encode($params, JSON_UNESCAPED_UNICODE); } curl_setopt($curl, CURLOPT_POSTFIELDS, $params); break; default: throw new InvalidParamException("Invalid http type '{$type}.' called."); } if (stripos($url, "https://") !== false) { curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($curl, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1); // 微信官方屏蔽了ssl2和ssl3, 启用更高级的ssl } curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); $content = curl_exec($curl); $status = curl_getinfo($curl); $errno = curl_errno($curl); curl_close($curl); if (isset($status['http_code']) && intval($status['http_code']) == 200) { return $content; } return false;}
1 0
- activeMQ 与 php的结合使用
- spring boot与activemq的结合使用
- activeMQ与spring结合 使用及配置
- AJAX与PHP的结合使用
- ActiveMQ与mqtt.js的结合应用
- Memcached 与 PHP 结合使用
- Redis (五 php与redis的结合使用)
- Redis (五 php与redis的结合使用)
- activeMQ+Spring-mvc结合使用
- ActiveMQ的安装与使用
- FCKeditor结合PHP的使用
- 【ActiveMQ】ActiveMQ的使用
- activemq使用系列: spring与activemq的整合
- 【ActiveMQ】ActiveMQ在CentOS的搭建与使用
- javascript与PHP结合的一些学习心得
- 入门:PHP与MYSQL的结合操作
- php代码与表格元素的结合
- Postfix服务器与PHP的结合
- java8 HashMap解析
- iOS 环信3.2集成 (一)基本使用
- yii框架基本增删改查分页操作
- 【腾讯Bugly干货分享】Android ListView与RecyclerView对比浅析--缓存机制
- Java注解
- activeMQ 与 php的结合使用
- 无需破解:Windows Server 2008 R2 至少免费使用 900天
- Java-获取系统的环境变量
- ora-16038
- hihocoder #24 Rikka with Subsequence
- 【JS】获取当前时间,时间与时间戳之间的转换
- exists比in高效?
- 软件工程之软件开发模型类型
- DuiLib 消息机制剖析