Laien's

成功 自在 健康 好状态

【原创】PHP飞信发送类的修正(感谢jjchibin)

without comments

  由于我测试验证不严谨,导致上次发布的PHP飞信短信发送类只能给自己发短信,无法给其他飞信好友发送短信。向各位表示抱歉,同时我今天修改了这个问题。关键是两个步骤:1、登录成功后GetContactList获取用户列表,2、GetContactsInfo获取用户详细信息,建立手机号码和飞信sip的uri之间的映射关系。
  多的不说了,直接贴代码。经测试程序可以群发,单发,给自己发!
  点击下载PHP飞信发送类修正版

?Download download.txt
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
<?php
/**
 *@desc 飞信短信发送类(Encoded:UTF-8)
 *使用方法:
 * * $myNewFetion = new myFetion('1381111111', 'password','SELF', '测试消息' ); // 给自己发
 * $myNewFetion = new myFetion('1381111111', 'password','1382222222', '测试消息' ); // 给一个人发
 * $myNewFetion = new myFetion('1381111111', 'password','1382222222,13833333333,13844444444', '测试消息' ); // 给很多人发
 * $myNewFetion = new myFetion('1381111111', 'password','ALL', '测试消息' ); // 给所有飞信好友发
 
 *非常感谢CSDN论坛ycTIN在MD5加密部分的帮助!
 *本程序未做容错处理,为防止诈骗短信乱发,程序不提供添加好友功能
 *测试URL:http://i.isclab.org/tools/fetion.php
 *
 *程序运行条件:
 *1.服务器能够访问飞信服务器nav.fetion.com.cn的443端口(https)
 *2.服务器端PHP程序能够创建socket访问221.176.31.4的8080端口
 *
 *关键技术:
 *1.CURL + SSL通讯
 *2.PHP Socket编程
 *3.PHP MD5函数的深入理解
 *4.PHP DOM处理XML
 *
 *@author shadu AT foxmail DOT com /CNOS(http://bbs.ouropen.org)
 *@version 2010-03-04
 *@copyright 任意拷贝和修改!
 *@update 修正了只能给自己发短信的bug,感谢jjchibin的提醒
 *@update 增加了群发功能
 **/
class myFetion{
    private $mobile_no   = '1381111111'; // 发送者手机号
    private $fetion_no   = '738713940' ; // 发送者飞信号,程序自动获取
    private $fetion_uri  = 'sip:123456789@fetion.com.cn;p=1234'; // 发送者的sip
    private $fetion_pwd  = 'mypassword' ; // 发送者飞信登录密码
    private $cookie_file = 'cookie.txt' ; // 临时存放的cookie文件
 
    public $SMS_RECEIVER = '1382222222' ; //  短信接收者手机号码 
    public $SMS_TEXT      = 'sms test' ; // 短信内容,支持中文
 
    private $fetion_ContactList = "" ;
    private $fetion_ContactMapping = array();
 
    private $NONCE       = 'AAB3238922BCC25A6F606EB525FFDC56' ; // SIPC服务器返回,每次不同
    private $C_NONCE     = 'AAB3238922BCC25A6F606EB525FFDC56' ; // 是随机的,但是固定值也没关系
    private $SSIC        = '' ; // cookie中提取的变量
    private $RESPONSE   = '' ; // 加密后的密钥串
 
 
    private $url_nav            = 'https://nav.fetion.com.cn/nav/getsystemconfig.aspx' ; // 443端口获取导航信息
    private $domain_fetion      = 'fetion.com.cn' ; // 飞信服务器的域名
 
    private $SIPC_PROXY         = '221.176.31.4:8080';  //  8080端口飞信通讯使用 
    private $SSI_PROXY_SIGN_IN  = 'https://uid.fetion.com.cn/ssiportal/SSIAppSignIn.aspx' ; // 登录URL
    private $SSI_PROXY_SIGH_OUT = 'http://ssi.fetion.com.cn/ssiportal/SSIAppSignOut.aspx' ; // 登出URL
 
    private $curl = NULL ;
    private $socket = NULL ;
 
    /**
     *从导航网站获取信息
     **/
    private $REQUEST_CONFIG = "<config><user mobile-no=\"%s\" /><client type=\"PC\" version=\"2.3.0230\" platform=\"W5.1\" /><servers version=\"0\" /><service-no version=\"12\" /><parameters version=\"15\" /><hints version=\"13\" /><http-applications version=\"14\" /><client-config version=\"17\" /></config>";
 
    /**
     *使用手机号码和密码向服务器获取对应的飞信号码信息
     **/
    private $REQUEST_SSI_SIGN  = "mobileno=%s&pwd=%s" ;
 
    /**
     *使用飞信号码向SIPC服务器注册,获取临时变量NONCE和SSIC的值
     **/
    private $REQUEST_SIPC_SIGN_NONCE      = "R %s SIP-C/2.0\r\nF: %s\r\nI: 1\r\nQ: 1 R\r\nL: %d\r\n\r\n%s" ;
    private $REQUEST_SIPC_SIGN_NONCE_BODY = "<args><device type=\"PC\" version=\"6\" client-version=\"2.3.0230\" /><caps value=\"simple-im;im-session;temp-group\" /><events value=\"contact;permission;system-message\" /><user-info attributes=\"all\" /><presence><basic value=\"400\" desc=\"\" /></presence></args>";
 
    /**
     *使用飞信号码和加密的密码登录飞信SIPC服务器
     **/
    private $REQUEST_SIPC_LOGIN           = "R %s SIP-C/2.0\r\nF: %s\r\nI: 1\r\nQ: 2 R\r\nA: Digest response=\"%s\",cnonce=\"%s\"\r\nL: %d\r\n\r\n%s";
    private $REQUEST_SIPC_LOGIN_BODY      = "<args><device type=\"PC\" version=\"6\" client-version=\"2.3.0230\" /><caps value=\"simple-im;im-session;temp-group\" /><events value=\"contact;permission;system-message\" /><user-info attributes=\"all\" /><presence><basic value=\"400\" desc=\"\" /></presence></args>";
 
 
    /**
     * 发短信
     */
    private $REQUEST_SIPC_SENDSMS         = "M %s SIP-C/2.0\r\nF: %s\r\nI: 2\r\nQ: 1 M\r\nT: %s\r\nN: SendSMS\r\nL: %d\r\n\r\n%s";
	/**
	 * 退出
	 **/
    private $REQUEST_SIPC_LOGOUT          = "R %s SIP-C/2.0\r\nF: %s\r\nI: 1 \r\nQ: 3 R\r\nX: 0\r\n\r\n";
 
    /**
	 * 获取好友列表
	 **/
    private $REQUEST_SIPC_GetContactList      = "S %s SIP-C/2.0\r\nF: %s\r\nI: 3\r\nQ: 1 S\r\nN: GetContactList\r\nL: %d\r\n\r\n%s" ;
    private $REQUEST_SIPC_GetContactListBody = "<args><contacts><buddy-lists /><buddies attributes=\"all\" /><mobile-buddies attributes=\"all\" /><chat-friends /><blacklist /></contacts></args>";
 
    /**
     * 获取好友信息
     **/
    private $REQUESR_SIPC_GetContactsInfo        = "S %s SIP-C/2.0\r\nF: %s\r\nI: 9\r\nQ: 1 S\r\nN: GetContactsInfo\r\nL: %d\r\n\r\n%s";
    private $REQUESR_SIPC_GetContactsInfo_body  = "<args><contacts attributes=\"provisioning;impresa;mobile-no;nickname;name;gender;portrait-crc;ivr-enabled\" extended-attributes=\"score-level\">%s</contacts></args>" ;
 
    /**
     *@param $sender 短信发送者手机号
     *@param $passwd 短信发送者密码
     *@param $receiver 短信接收者手机号
     *@param $msg 短信内容
     **/
    public function __construct($sender, $passwd, $receiver, $msg){
        $this->mobile_no    = $sender ;
        $this->fetion_pwd   = $passwd;
        $this->SMS_RECEIVER = $receiver;
        $this->SMS_TEXT     = $msg;
        $this->cookie_file  = $this->mobile_no . $this->cookie_file ;
 
		$this->FetionStart();
    }
 
 
	/**
	 * 开始Fetion短信发送处理流程
	 *
	 */
	private function FetionStart(){
		$this->FetionGetConfig(); // 从导航网站443端口获取登录信息
		if($this->FetionSocektInit()) // 初始化到SIPC的8080端口socket连接
		{
			$this->FetionGetSIPCNonce(); // 向服务器注册飞信号,获取关键变量值
			if($this->FetionLogin()){    // 发送登录认证命令
				if(!$this->FetionGetContactLists()){ // 取飞信用户列表
					$this->SMS_RECEIVER == "SELF";
				}; 
				$this->FetionGetContactsInfo(); // 建立电话号码到飞信号码的映射
				//$this->FetionSendSMS(); // 发短信!
				if("ALL" == $this->SMS_RECEIVER){  // 给飞信上所有好友发
					foreach($this->fetion_ContactMapping as $sip){
						$this->FetionSendSMS($sip);
					}
				}
				elseif("SELF" == $this->SMS_RECEIVER){ // 给自己发
					$this->FetionSendSMS($this->fetion_uri);
				}
				else{ // 发给指定的手机号集合"13811111111,13822222222,13833333333"
					$mobile_array = split("," , $this->SMS_RECEIVER);
					foreach($mobile_array as $mobile){
						$this->FetionSendSMS($this->fetion_ContactMapping["$mobile"]);
					}
				}
				$this->FetionLogout();
			}
			else
			{
				echo "登录失败\n";
			}
		}
		else
		{
			echo "Socket 初始化失败\n";
		}
	}
 
    /**
     *从导航地址获取配置信息
     **/
    private function FetionGetConfig(){
        $this->REQUEST_CONFIG = sprintf($this->REQUEST_CONFIG,
                                        $this->mobile_no);
        $this->curl = curl_init();
        curl_setopt($this->curl, CURLOPT_URL, $this->url_nav);
        curl_setopt($this->curl, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($this->curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($this->curl, CURLOPT_COOKIEJAR, $this->cookie_file);
        curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($this->curl, CURLOPT_POST, 1);
        curl_setopt($this->curl, CURLOPT_POSTFIELDS, $this->REQUEST_CONFIG);
        $xml_config = curl_exec($this->curl);
 
        // 以下是从导航页面返回的XML里取相关信息
        file_put_contents("test3.xml", $xml_config) ;
        $xmlDom = new DOMDocument() ;
        $xmlDom->loadXML($xml_config);
        $fetion_server = $xmlDom->getElementsByTagName('servers');
        $fetion_server->item(0)->getElementsByTagName('sipc-proxy')->item(0)->nodeValue;
        $this->SSI_PROXY_SIGN_IN  = $fetion_server->item(0)->getElementsByTagName('ssi-app-sign-in')->item(0)->nodeValue;
        $this->SSI_PROXY_SIGH_OUT = $fetion_server->item(0)->getElementsByTagName('ssi-app-sign-out')->item(0)->nodeValue;
        $this->SSI_PROXY_SIGN_IN;
 
        // 以下获取手机号对应的飞信号
        sprintf($this->REQUEST_SSI_SIGN, $this->mobile_no, $this->fetion_pwd) ;
        curl_setopt($this->curl, CURLOPT_URL, $this->SSI_PROXY_SIGN_IN);
        curl_setopt($this->curl, CURLOPT_POSTFIELDS, sprintf($this->REQUEST_SSI_SIGN, $this->mobile_no, $this->fetion_pwd));
        $Result = curl_exec($this->curl);
        curl_close($this->curl);
        file_put_contents("test4.xml", $Result) ;
        $xmlDom->loadXML($Result);
        $uri = $xmlDom->getElementsByTagName("user")->item(0)->getAttribute("uri");
        //"sip:738713940@fetion.com.cn;p=5914"
		$this->fetion_uri = $uri ; // 发送者自己的sip
        if(preg_match('/^sip:(\d+)@(\S+);.*$/', $uri, $matches)){
            $this->fetion_no = $matches[1] ;
            $this->domain_fetion = $matches[2] ;
        }
    }
 
    /**
     *初始化Fetion通讯Socket
     **/
    private function FetionSocektInit(){
		$this->SIPC_PROXY;
        $this->socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
        list($ip_fetion, $port_fetion) = split(':', $this->SIPC_PROXY) ; // "221.176.31.4:8080"
        return socket_connect($this->socket, $ip_fetion, $port_fetion) ;
    }
 
    /**
     *注册飞信号码并获取临时变量NONCE和SSIC
     **/
    private function FetionGetSIPCNonce(){
        $REQUEST_SIPC_SIGN_NONCE = sprintf($this->REQUEST_SIPC_SIGN_NONCE, $this->domain_fetion,
                                           $this->fetion_no, strlen($this->REQUEST_SIPC_SIGN_NONCE_BODY),
                                           $this->REQUEST_SIPC_SIGN_NONCE_BODY) ;
        $sock_data = socket_write($this->socket, $REQUEST_SIPC_SIGN_NONCE);
        $buf = '' ;
        if (false == ($buf = socket_read($this->socket, 10000))) {
            echo "Line:" . __LINE__ . "socket_read() failed; reason: " . socket_strerror(socket_last_error($this->socket)) . "\n";
        }
        $regex_ssic = '/.*nonce=\"(\\w+)\".*/s' ;
        if(!preg_match($regex_ssic, $buf, $matches)){
            echo "Fetion Error: No nonce found in socket\n";
        }
        $this->NONCE = strtoupper(trim($matches[1]));
 
        $regex_ssic = '/ssic\s+(.*)/s';
        if (!preg_match($regex_ssic, file_get_contents($this->cookie_file), $matches)) {
            echo "Fetion Error: No ssic found in cookie\n";
        }
        echo $this->SSIC = trim($matches[1]);
    }
 
    /**
     *登录飞信服务器
     **/
    private function FetionLogin(){
        $this->RESPONSE = $this->FetionEncryptPassWD() ;
        $REQUEST_SIPC_LOGIN = sprintf($this->REQUEST_SIPC_LOGIN,
                                      $this->domain_fetion, $this->fetion_no,
                                      $this->RESPONSE, $this->C_NONCE,
                                      strlen($this->REQUEST_SIPC_LOGIN_BODY),
                                      $this->REQUEST_SIPC_LOGIN_BODY);
        $buf = '' ;
        $sock_data = socket_write($this->socket, $REQUEST_SIPC_LOGIN);
        if (false == ($buf = socket_read($this->socket, 10000))) {
            echo "Line:" . __LINE__ . "socket_read() failed; reason: " . socket_strerror(socket_last_error($this->socket)) . "\n";
        }
		//echo $buf,"\n";
        if(preg_match_all('/200/s', $buf, $matches)){
            return True;
        }else{
            return False;
        }
    }
 
 
	/**
	 * 获取飞信用户列表
	 * */
	private function FetionGetContactLists(){
		echo $REQUEST_GETCONTACTLISTS = sprintf($this->REQUEST_SIPC_GetContactList,
				$this->domain_fetion,
				$this->fetion_no,
				strlen($this->REQUEST_SIPC_GetContactListBody),
				$this->REQUEST_SIPC_GetContactListBody);
		$buf = '' ;
		$sock_data = socket_write($this->socket, $REQUEST_GETCONTACTLISTS);
		if (false == ($buf = socket_read($this->socket, 10000))) {
			echo "\nLine:" . __LINE__ . " socket_read() failed; reason: " . socket_strerror(socket_last_error($this->socket)) . "\n";
		}
		//echo $buf,"\n\n\n";
		file_put_contents("ContactList.txt",$buf);
		if(preg_match_all('/(sip:\\d+@fetion\.com\.cn;p=\\d+)/s',$buf,$matches)){
			$fetion_ContactList = $matches[1];
			foreach($fetion_ContactList as $sip){
				$this->fetion_ContactList .= "<contact uri=\"$sip\" />";
			}
			return true;
		}
		return false;
	}
 
 
	/**
	 * 获取用户信息,并建立手机号与SIP号映射关系
	 *
	 */
	private function FetionGetContactsInfo(){
		$this->REQUESR_SIPC_GetContactsInfo_body = sprintf($this->REQUESR_SIPC_GetContactsInfo_body, $this->fetion_ContactList);
		$REQUESR_SIPC_GetContactsInfo = sprintf($this->REQUESR_SIPC_GetContactsInfo,
				$this->domain_fetion,
				$this->fetion_no,
				strlen($this->REQUESR_SIPC_GetContactsInfo_body),
				$this->REQUESR_SIPC_GetContactsInfo_body
				);
		$sock_data = socket_write($this->socket, $REQUESR_SIPC_GetContactsInfo);
		if (false == ($buf = socket_read($this->socket, 1024*10))) {
			echo "\nLine:" . __LINE__ . " socket_read() failed; reason: " . socket_strerror(socket_last_error($this->socket)) . "\n";
		}
		//echo $buf,"\n\n";
		file_put_contents("GetContactsInfo.txt",$buf);
		if(preg_match_all('/<contact uri=\"(.*?)\".*?mobile\-no=\"(.*?)\".*?\/contact>/s',$buf,$matches)){
			$total = count($matches[1]);
			for($i=0; $i<$total; $i++){
				$uri = $matches[1][$i];
				$mobile = $matches[2][$i];
				$this->fetion_ContactMapping["$mobile"] = $uri ; // "13811111111" => "sip:912534014@fetion.com.cn;p=9272"
			}
			return true;
		}
		else{
			return false;
		}
	}
    /**
     *发短信
     **/
    public function FetionSendSMS($receiver_sip){
        //"M %s SIP-C/2.0\r\nF: %s\r\nI: 2\r\nQ: 1 M\r\nT: tel:%s\r\nN: SendSMS\r\nL: %d\r\n\r\n%s";
//		$receiver_mobile = $this->SMS_RECEIVER ;
//		if(!array_key_exists($receiver_mobile,$this->fetion_ContactMapping)) return false;
//		$receiver_sip = $this->fetion_ContactMapping[$receiver_mobile] ; // 转换手机号为"sip:1212@fetion.com.cn;p=1212"格式
        $REQUEST_SENDSMS = sprintf($this->REQUEST_SIPC_SENDSMS,
                                   $this->domain_fetion, $this->fetion_no,
                                   // "tel:" . $this->SMS_RECEIVER, // "tel:13822222222" 方式目前已经不支持
				                   $receiver_sip,  
                                   strlen($this->SMS_TEXT),
                                   $this->SMS_TEXT) ;
        $buf = '' ;
        $sock_data = socket_write($this->socket, $REQUEST_SENDSMS);
        if (false == ($buf = socket_read($this->socket, 1000))) {
            echo "\nLine:" . __LINE__ . " socket_read() failed; reason: " . socket_strerror(socket_last_error($this->socket)) . "\n";
        }
		//echo $buf,"\n";
        if(preg_match_all('/200/s', $buf, $matches)){
            return True;
        }else{
            return False;
        }
    }
 
    /**
     *登出飞信服务器
     **/
    private function FetionLogout(){
        //string Logout = String.Format(FETION_SIPC_LOGOUT, FETION_DOMAIN_URL, Fetion_Number);
        $FETION_SIPC_LOGOUT   = "R %s SIP-C/2.0\r\nF: %s\r\nI: 1 \r\nQ: 3 R\r\nX: 0\r\n\r\n";
        $REQUEST_SIPC_LOGOUT = sprintf($this->REQUEST_SIPC_LOGOUT,
                                       $this->domain_fetion,
                                       $this->fetion_no) ;
        @socket_write($this->socket, $REQUEST_SIPC_LOGOUT);
        socket_close($this->socket) ;
    }
 
    /**
     *生成加密串,感谢CSND ycTIN的帮助!
     *@return string 加密的密码串     *
     **/
 
    private function FetionEncryptPassWD() {
        $key = md5($this->fetion_no . ':' . $this->domain_fetion . ':' . $this->fetion_pwd, true);
        $h1 = strtoupper(md5($key . ':' . $this->NONCE . ':' . $this->C_NONCE));
 
        $h2 = "REGISTER:" . $this->fetion_no ;
        $h2 = strtoupper(md5($h2));
 
        $response = "$h1:" . $this->NONCE . ":" . $h2;
        $response = strtoupper(md5($response));
 
        return $response ;
    }
 
    /**
     *打印一下临时变量
     **/
    public function printVar(){
        print "\nCNONCE:" . $this->C_NONCE;
        print "\nDomain:" . $this->domain_fetion;
        print "\nNONCE:" . $this->NONCE;
        print "\nRESPONSE:" . $this->RESPONSE;
    }
    public function __destruct(){
        //$this->FetionLogout() ;
        @unlink($this->cookie_file); // 删除cookie文件
    }
}
 
$myNewFetion = new myFetion('13800000000', 'abcde12345', 'SELF', '给我自己发');
$myNewFetion = new myFetion('13800000000', 'abcde12345', '13811111111,13822222222,13833333333', '大家注意了:这是一条PHP飞信短信发送类测试短信。如果你能收到,说明我的群发功能已经实现!收到请回复,Thanks!');
$myNewFetion = new myFetion('13800000000', 'abcde12345', 'ALL', 'PHP短信拜年了!');
 
 
//$myNewFetion->printVar() ;
?>

Written by Laien

March 3rd, 2010 at 2:47 pm

Posted in PHP, Programming

Tagged with , ,