header('Content-Type: text/html; charset=utf-8');
/**
* PHP飞信发送类
* @author quanhengzhuang
* @version 1.5.0
*/
class PHPFetion {
/**
* 发送者手机号
* @var string
*/
protected $_mobile;
/**
* 飞信密码
* @param string
*/
protected $_password;
/**
* Cookie字符串
* @param string
*/
protected $_cookie = '';
/**
* Uid缓存
* @var array
*/
protected $_uids = array();
/**
* csrfToken
* @param string
*/
protected $_csrfToten = null;
/**
* 构造函数
* @param string $mobile 手机号(登录者)
* @param string $password 飞信密码
*/
public function __construct($mobile, $password)
{
if ($mobile === '' || $password === '')
{
return;
}
$this->_mobile = $mobile;
$this->_password = $password;
$this->_login();
}
/**
* 析构函数
*/
public function __destruct() {
$this->_logout();
}
/**
* 登录
* @return string
*/
protected function _login()
{
$uri = '/huc/user/space/login.do?m=submit&fr=space';
$data = 'mobilenum='.$this->_mobile.'&password='.urlencode($this->_password);
$result = $this->_postWithCookie($uri, $data);
//解析Cookie
preg_match_all('/.*?rnSet-Cookie: (.*?);.*?/si', $result, $matches);
if (isset($matches[1]))
{
$this->_cookie = implode('; ', $matches[1]);
}
$result = $this->_postWithCookie('/im/login/cklogin.action', '');
return $result;
}
/**
* 获取csrfToken,给好友发飞信时需要这个字段
* @param string $uid 飞信ID
* @return string
*/
protected function _getCsrfToken($uid)
{
if ($this->_csrfToten === null)
{
$uri = '/im/chat/toinputMsg.action?touserid='.$uid;
$result = $this->_postWithCookie($uri, '');
preg_match('/name="csrfToken".*?value="(.*?)"/', $result, $matches);
$this->_csrfToten = isset($matches[1]) ? $matches[1] : '';
}
return $this->_csrfToten;
}
/**
* 向指定的手机号发送飞信
* @param string $mobile 手机号(接收者)
* @param string $message 短信内容
* @return string
*/
public function send($mobile, $message) {
if($message === '') {
return '';
}
// 判断是给自己发还是给好友发
if($mobile === $this->_mobile) {
return $this->_toMyself($message);
}
else if(strlen($mobile)===11){
$uid = $this->_getUid($mobile);
}
else {
$uid=$mobile;
}
return $uid === '' ? $this->_addFriend($mobile) : $this->_toUid($uid, $message);
}
protected function _getname() {
$uri = '/im/index/index.action';
$result = $this->_postWithCookie($uri, '#');
// 匹配
preg_match('/(.*?)/si', $result, $matches);
return $matches[2];
}
/*
* 通过手机号增加好友
* @param string $number 手机号(要加的好友手机)
* @param string $nickname 你的名字,出现在对方的验证短信里
* @param string $buddylist 分组,默认为空
* @param string $localName 好友屏显名
* @return string
*/
protected function _addFriend($number) {
$uri = '/im/user/insertfriendsubmit.action';
$data = 'nickname='. urlencode($this->_getname()).'&buddylist=1&localName=&number='. $number .'&type=0';
$result = $this->_postWithCookie($uri, $data);
return $result;
}
/**
* 获取飞信ID
* @param string $mobile 手机号
* @return string
*/
protected function _getUid($mobile)
{
if (empty($this->_uids[$mobile]))
{
$uri = '/im/index/searchOtherInfoList.action';
$data = 'searchText='.$mobile;
$result = $this->_postWithCookie($uri, $data);
//匹配
preg_match('/toinputMsg.action?touserid=(d+)/si', $result, $matches);
$this->_uids[$mobile] = isset($matches[1]) ? $matches[1] : '';
}
return $this->_uids[$mobile];
}
/**
* 向好友发送飞信
* @param string $uid 飞信ID
* @param string $message 短信内容
* @return string
*/
protected function _toUid($uid, $message) {
$uri = '/im/chat/sendMsg.action?touserid='.$uid;
$csrfToken = $this->_getCsrfToken($uid);
$data = 'msg='.urlencode($message).'&csrfToken='.$csrfToken;
$result = $this->_postWithCookie($uri, $data);
return $result;
}
/**
* 给自己发飞信
* @param string $message
* @return string
*/
protected function _toMyself($message) {
$uri = '/im/user/sendMsgToMyselfs.action';
$result = $this->_postWithCookie($uri, 'msg='.urlencode($message));
return $result;
}
/**
* 退出飞信
* @return string
*/
protected function _logout() {
$uri = '/im/index/logoutsubmit.action';
$result = $this->_postWithCookie($uri, '');
return $result;
}
protected function getgroup() {
$uri = '/im/index/index.action';
$data = 'type=group';
$result = $this->_postWithCookie($uri, $data);
// 匹配
preg_match_all('/contactlistView.action?idContactList=(d+)/si', $result, $matches);
foreach($matches[1] as $k=>$v){
if( $k== 0 ){
$min = $v;
$max = $v;
}else if($v!=9998&&$v!=9999){
$min = min($min,$v);
$max = max($max,$v);
}
}
return $max;
}
public function getyou1() {
$list=$this->getgroup();
for($i=0;$i<=$list;$i++){
$uri = '/im/index/contactlistView.action';
$data = 'idContactList='.$i.'&type=group';
$result = $this->_postWithCookie($uri, $data);
preg_match('/(.*?)|(.*?)((.*?)/(.*?))/si', $result, $listn);
if(!$listn[2]){continue;}
$shuchu.=str_replace(" ","",$listn[2])."(".$listn[4].")n";
preg_match('/共(d+)页/si', $result, $zpage);
preg_match('/共(d+)页/si', $result, $dpage);
isset($zpage[1]) ? $page=$zpage[1] : $page=$dpage[4];
for($j=1;$j<=$page;$j++){
$uri = '/im/index/contactlistView.action';
$data = 'idContactList='.$i.'&page='.$j;
$result = $this->_postWithCookie($uri, $data);
preg_match_all('/(.*?)/si', $result, $matches);
if(!$matches[1][0]){break;}
for($x=0;$x<=9;$x++){
if(!$matches[1][$x]){continue;}
$shuchu.=$matches[1][$x]." ".str_replace(" ","",$matches[3][$x])."n";
}
}
}
return $shuchu;
}
public function getyou() {
$list=$this->getgroup();
for($i=0;$i<=$list;$i++){
$uri = '/im/index/contactlistView.action';
$data = 'idContactList='.$i.'&type=group';
$result = $this->_postWithCookie($uri, $data);
preg_match('/(.*?)|(.*?)((.*?)/(.*?))/si', $result, $listn);
if(!$listn[2]){continue;}
$shuchu.=str_replace(" ","",$listn[2])."(".$listn[4].")n";
preg_match('/共(d+)页/si', $result, $zpage);
preg_match('/共(d+)页/si', $result, $dpage);
isset($zpage[1]) ? $page=$zpage[1] : $page=$dpage[4];
for($j=1;$j<=$page;$j++){
$uri = '/im/index/contactlistView.action';
$data = 'idContactList='.$i.'&page='.$j;
$result = $this->_postWithCookie($uri, $data);
preg_match_all('/(.*?)/si', $result, $matches);
if(!$matches[1][0]){break;}
for($x=0;$x<=9;$x++){
if(!$matches[1][$x]){continue;}
$shuchu.=$matches[1][$x]." ".str_replace(" ","",$matches[3][$x])."n";
}
}
}
return $shuchu;
}
/**
* 携带Cookie向f.10086.cn发送POST请求
* @param string $uri
* @param string $data
*/
protected function _postWithCookie($uri, $data)
{
$fp = fsockopen('f.10086.cn', 80);
fputs($fp, "POST $uri HTTP/1.1rn");
fputs($fp, "Host: f.10086.cnrn");
fputs($fp, "Cookie: {$this->_cookie}rn");
fputs($fp, "Content-Type: application/x-www-form-urlencodedrn");
fputs($fp, "User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:14.0) Gecko/20100101 Firefox/14.0.1rn");
fputs($fp, "Content-Length: ".strlen($data)."rn");
fputs($fp, "Connection: closernrn");
fputs($fp, $data);
$result = '';
while (!feof($fp))
{
$result .= fgets($fp);
}
fclose($fp);
return $result;
}
}
|