|
[PHP]发送飞信类公开代码
002 class PHPFetion {
003 protected $_mobile;
004 protected $_password;
005 protected $_cookie = '';
006 public function __construct($mobile, $password) {
007 if($mobile === '' || $password === '') {
008 return false;
009 }
010 $this->_mobile = $mobile;
011 $this->_password = $password;
012 $this->_login();
013 }
014 public function __destruct() {
015 $this->_logout();
016 }
017 /**
018 * 登录
019 * @return string
020 */
021 protected function _login() {
022 $uri = '/im/login/inputpasssubmit1.action';
023 $data = 'm='.$this->_mobile.'&pass='.urlencode($this->_password).'&loginstatus=4';
024 $result = $this->_postWithCookie($uri, $data);
025 // 解析Cookie
026 preg_match_all('/.*?\r\nSet-Cookie: (.*?);.*?/si', $result, $matches);
027 if(isset($matches[1])) {
028 $this->_cookie = implode('; ', $matches[1]);
029 }
030 return $result;
031 }
032 /**
033 * 向指定的手机号发送飞信
034 * @param string $mobile 手机号(接收者)
035 * @param string $message 短信内容
036 * @return string
037 */
038 public function send($mobile, $message) {
039 if($message === '') {
040 return '';
041 }
042 // 判断是给自己发还是给好友发
043 if($mobile == $this->_mobile) {
044 return $this->_toMyself($message);
045 } else {
046 $uid = $this->_getUid($mobile);
047 return $uid === '' ? '' : $this->_toUid($uid, $message);
048 }
049 }
050 /**
051 * 获取飞信ID
052 * @param string $mobile 手机号
053 * @return string
054 */
055 protected function _getUid($mobile) {
056 $uri = '/im/index/searchOtherInfoList.action';
057 $data = 'searchText='.$mobile;
058 $result = $this->_postWithCookie($uri, $data);
059 // 匹配
060 preg_match('/toinputMsg\.action\?touserid=(\d+)/si', $result, $matches);
061 return isset($matches[1]) ? $matches[1] : '';
062 }
063 /**
064 * 向好友发送飞信
065 * @param string $uid 飞信ID
066 * @param string $message 短信内容
067 * @return string
068 */
069 protected function _toUid($uid, $message) {
070 $uri = '/im/chat/sendMsg.action?touserid='.$uid;
071 $data = 'msg='.urlencode($message);
072 $result = $this->_postWithCookie($uri, $data);
073 return $result;
074 }
075 /**
076 * 给自己发飞信
077 * @param string $message
078 * @return string
079 */
080 protected function _toMyself($message) {
081 $uri = '/im/user/sendMsgToMyselfs.action';
082 $result = $this->_postWithCookie($uri, 'msg='.urlencode($message));
083 return $result;
084 }
085 /**
086 * 退出飞信
087 * @return string
088 */
089 protected function _logout() {
090 $uri = '/im/index/logoutsubmit.action';
091 $result = $this->_postWithCookie($uri, '');
092 return $result;
093 }
094 /**
095 * 携带Cookie向f.10086.cn发送POST请求
096 * @param string $uri
097 * @param string $data
098 */
099 protected function _postWithCookie($uri, $data) {
100 $fp = fsockopen('f.10086.cn', 80);
101 fputs($fp, "POST $uri HTTP/1.1\r\n");
102 fputs($fp, "Host: f.10086.cn\r\n");
103 fputs($fp, "Cookie: {$this->_cookie}\r\n");
104 fputs($fp, "Content-Type: application/x-www-form-urlencoded\r\n");
105 fputs($fp, "Content-Length: ".strlen($data)."\r\n");
106 fputs($fp, "Connection: close\r\n\r\n");
107 fputs($fp, $data);
108 $result = '';
109 while(!feof($fp)) {
110 $result .= fgets($fp);}
111 fclose($fp);
112 return $result; |
|