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
2
3
4
|
header( "content-type:text/html;charset=gbk" ); $msg = iconv( "utf-" , "gbk" , $msg ); $title = iconv( "utf-" , "gbk" , $title ); |
如果你的所有安装都是在本地的,那么只需要访问(默认端口8012)即可:
http://127.0.0.1:8012/sendnotify.cgi?msg=消息内容&receiver=rtx的账号(多个用逗号格开)&title=通知的标题
以下是修改后的sendnotify.cgi代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
|
<?php /** * created by phpstorm. * user: steven * date: // * time: : */ header( "content-type:text/html;charset=gbk" ); require_once "iplimit.php" ; $receiver = $_get [ "receiver" ]; $msg = $_get [ "msg" ]; $title = $_get [ "title" ]; $delaytime = $_get [ "delaytime" ]; $okurl = $_get [ "okurl" ]; $errurl = $_get [ "errurl" ]; if (( strlen ( $receiver ) == ) && ( strlen ( $msg ) == ) && ( strlen ( $title ) == ) && ( strlen ( $delaytime ) == ) && ( strlen ( $okurl ) == ) && ( strlen ( $errurl ) == ) ) { $receiver = $_post [ "receiver" ]; $msg = $_post [ "msg" ]; $title = $_post [ "title" ]; $delaytime = $_post [ "delaytime" ]; $okurl = $_post [ "okurl" ]; $errurl = $_post [ "errurl" ]; } if ( strlen ( $receiver ) == ) { $receiver = "" ; } if ( strlen ( $msg ) == ) { $msg = "" ; } if ( strlen ( $title ) == ) { $title = "" ; } if ( strlen ( $delaytime ) == ) { $delaytime = ; } if ( strlen ( $okurl ) == ) { $okurl = "" ; } if ( strlen ( $errurl ) == ) { $errurl = "" ; } $msg = iconv( "utf-" , "gbk" , $msg ); $title = iconv( "utf-" , "gbk" , $title ); $php_errormsg = null; $objapi = new com( "rtxserver.rtxobj" ); $objprop = new com( "rtxserver.collection" ); $name = "exttools" ; $objapi ->name = $name ; $objprop ->add( "msginfo" , $msg ); $objprop ->add( "msgid" , "" ); $objprop ->add( "type" , "" ); $objprop ->add( "asstype" , "" ); if ( strlen ( $title ) == ) { $objprop ->add( "title" , "֪ͨ" ); } else { $objprop ->add( "title" , $title ); } $objprop ->add( "delaytime" , $delaytime ); if ( strtolower ( $receiver ) == "all" ) { $objprop ->add( "username" , $receiver ); $objprop ->add( "sendmode" , "" ); } else { $objprop ->add( "username" , $receiver ); } $result = @ $objapi ->call(x, $objprop ); $errstr = $php_errormsg ; if ( strcmp ( $nullstr , $errstr ) == ) { if ( strlen ( $okurl ) > ) { header( "location: " . $okurl ); } else { // echo "<script language=\"javascript\">\r\n"; // echo "alert(\"success\")"; //echo "</script>\r\n"; return true; } } else { //send err if ( strlen ( $errurl ) > ) { header( "location: " . $errurl ); } else { echo $errstr . "<br>" ; } } ?> |
3、需要注意的是代码中引入了iplimit.php文件,所以,访问的时候可能会提示ip受限,这就需要在rtx服务端安装文件下找到一个叫sdkproperty.xml的文件,加入你发送请求的服务器ip保存即可
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<?xml version= "." ?> <property> <apiclient> <iplimit enabled= "" > <ip>...</ip> <ip>这里是ip地址</ip> </iplimit> </apiclient> <sdkhttp> <iplimit enabled= "" > </iplimit> </sdkhttp> </property> |
4、开发者只需要封装一个发送get或者post的方法,例如:
1
2
3
4
5
6
|
$arr = array ( 'title' => $title , //消息的标题 'receiver' => $receiver_list , //消息的接受者 'msg' => $msg , //消息内容 ); $this ->httpspost( 'http://...:/sendnotify.cgi' , $arr ); |
即可向rtx发送消息通知,以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:http://www.cnblogs.com/Steven-shi/p/6243008.html