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的方法,例如:
1 $arr = array( 2 'title' => $title, //消息的標題 3 'receiver' => $receiver_list, //消息的接受者 4 'msg' => $msg, //消息內容 5 ); 6 $this->httpsPost('http://180.168.4.58:8012/SendNotify.cgi', $arr);
即可向RTX發送消息通知,如有疑問歡迎溝通交流(Q:2384834530)