PHP实现RTX发送消息提醒
RTX是腾讯公司推出的企业级即时通信平台,大多数公司都在使用它,但是我们很多时候需要将自己系统或者产品的一些通知实时推送给RTX,这就需要用到RTX的服务端SDK,建议先去看看RTX的SDK开发文档(客户端,服务器),我们先看看功能效果:
当然,现在很多公司都已经在RTX的基础上升级成了企业微信,没关系,这个API同样可以使用,还是同样的接口,只是展示效果不一样而已:
下面是用PHP实现RTX发送消息提醒:
1、首先在服务器端安装RTX的服务端和客户端,再安装SDK开发包(对于发送消息提醒的功能,只需要安装服务端sdk即可)(下载地址:http://rtx.tencent.com/rtx/download/index.shtml)
2、然后再RTX服务器的安装路径下有个WebRoot文件夹,文件夹下有个SendNotify.cgi文件,这就是RTX发送消息的接口文件,打开文件是懂PHP的都能看懂的代码,不过需要在头部加入header头,以及需要处理一些参数的编码
1 header("Content-Type:text/html;charset=GBK");
1 $msg = iconv("utf-8", "GBK", $msg); 2 $title = iconv("utf-8", "GBK", $title);
如果你的所有安装都是在本地的,那么只需要访问(默认端口8012)即可:
http://127.0.0.1:8012/SendNotify.cgi?msg=消息内容&receiver=RTX的账号(多个用逗号格开)&title=通知的标题
以下是修改后的SendNotify.cgi代码:
1 <?php 2 /** 3 * Created by PhpStorm. 4 * User: Steven 5 * Date: 2016/12/29 6 * Time: 19:22 7 */ 8 9 header("Content-Type:text/html;charset=GBK"); 10 require_once "IPLimit.php"; 11 12 $receiver = $_GET["receiver"]; 13 $msg = $_GET["msg"]; 14 $title = $_GET["title"]; 15 16 17 $delaytime = $_GET["delaytime"]; 18 $okurl = $_GET["okurl"]; 19 $errurl = $_GET["errurl"]; 20 21 if ((strlen($receiver) == 0) 22 && (strlen($msg) == 0) 23 && (strlen($title) == 0) 24 && (strlen($delaytime) == 0) 25 && (strlen($okurl) == 0) 26 && (strlen($errurl) == 0) 27 ) { 28 $receiver = $_POST["receiver"]; 29 $msg = $_POST["msg"]; 30 $title = $_POST["title"]; 31 $delaytime = $_POST["delaytime"]; 32 $okurl = $_POST["okurl"]; 33 $errurl = $_POST["errurl"]; 34 } 35 36 if (strlen($receiver) == 0) { 37 $receiver = ""; 38 } 39 if (strlen($msg) == 0) { 40 $msg = ""; 41 } 42 if (strlen($title) == 0) { 43 $title = ""; 44 } 45 if (strlen($delaytime) == 0) { 46 $delaytime = 0; 47 } 48 if (strlen($okurl) == 0) { 49 $okurl = ""; 50 } 51 if (strlen($errurl) == 0) { 52 $errurl = ""; 53 } 54 55 $msg = iconv("utf-8", "GBK", $msg); 56 $title = iconv("utf-8", "GBK", $title); 57 $php_errormsg = NULL; 58 59 $ObjApi = new COM("Rtxserver.rtxobj"); 60 $objProp = new COM("Rtxserver.collection"); 61 $Name = "ExtTools"; 62 $ObjApi->Name = $Name; 63 64 $objProp->Add("msgInfo", $msg); 65 $objProp->Add("MsgID", "1"); 66 $objProp->Add("Type", "0"); 67 $objProp->Add("AssType", "0"); 68 if (strlen($title) == 0) { 69 $objProp->Add("Title", "֪ͨ"); 70 } else { 71 $objProp->Add("Title", $title); 72 } 73 $objProp->Add("DelayTime", $delaytime); 74 if (strtolower($receiver) == "all") { 75 $objProp->Add("Username", $receiver); 76 $objProp->Add("SendMode", "1"); 77 } else { 78 $objProp->Add("Username", $receiver); 79 } 80 81 82 $Result = @$ObjApi->Call2(0x2100, $objProp); 83 84 $errstr = $php_errormsg; 85 if (strcmp($nullstr, $errstr) == 0) { 86 if (strlen($okurl) > 0) { 87 header("Location: " . $okurl); 88 } else { 89 // echo "<script language=\"JavaScript\">\r\n"; 90 // echo "alert(\"success\")"; 91 //echo "</script>\r\n"; 92 return true; 93 94 } 95 } else { 96 //send err 97 if (strlen($errurl) > 0) { 98 header("Location: " . $errurl); 99 } else { 100 echo $errstr . "<br>"; 101 102 } 103 } 104 ?>
3、需要注意的是代码中引入了IPLimit.php文件,所以,访问的时候可能会提示IP受限,这就需要在RTX服务端安装文件下找到一个叫SDKProperty.xml的文件,加入你发送请求的服务器IP保存即可
1 <?xml version="1.0"?> 2 <Property> 3 <APIClient> 4 <IPLimit Enabled="1"> 5 <IP>127.0.0.1</IP> 6 <IP>这里是IP地址</IP> 7 </IPLimit> 8 9 </APIClient> 10 <sdkhttp> 11 <IPLimit Enabled="1"> 12 </IPLimit> 13 </sdkhttp> 14 </Property>
4、开发者只需要封装一个发送GET或者POST的方法,例如: