HeQee

Feed Rss

目前可用的PHP飞信发送类

08.04.2011, PHP, Share, by .

此类来自blog.quanhz.com,这个是基于f.10086.cn wap站点,目前还可以使用。分享给大家。

我可能会更新不及时,如果一下代码运行错误,请到官方去下载:

http://code.google.com/p/php-fetion/

<?php
/**
 * PHP飞信发送类
 */
class fetion {
  //发送者手机号
  protected $_mobile;
  //飞信密码
  protected $_password;
   //Cookie字符串
  protected $_cookie = '';
  //构造函数
  public function __construct($mobile,$password) {
    if($mobile === '' || $password === '') {
      return false;
    }
    $this->_mobile = $mobile;
    $this->_password = $password;
    $this->_login();
  }
   //析构函数
  public function __destruct() {
    $this->_logout();
  }
  //登录
  protected function _login() {
    $uri = '/im/login/inputpasssubmit1.action';
    $data = 'm='.$this->_mobile.'&pass='.urlencode($this->_password).'&loginstatus=1';
    $result = $this->_postWithCookie($uri, $data);
    // 解析Cookie
    preg_match_all('/.*?\r\nSet-Cookie: (.*?);.*?/si', $result, $matches);
    if(isset($matches[1])) {
     $this->_cookie = implode('; ', $matches[1]);
    }
    return $result;
  }
  //向指定的手机号发送飞信
  public function send($mobile, $message) {
    if($message === '') {
      return '';
    }
    // 判断是给自己发还是给好友发
    if($mobile == $this->_mobile) {
      return $this->_toMyself($message);
    } else {
      $uid = $this->_getUid($mobile);
      return $uid === '' ? '' : $this->_toUid($uid, $message);
    }
  }
  //获取飞信ID
  protected function _getUid($mobile) {
    $uri = '/im/index/searchOtherInfoList.action';
    $data = 'searchText='.$mobile;
    $result = $this->_postWithCookie($uri, $data);
    // 匹配
    preg_match('/toinputMsg\.action\?touserid=(\d+)/si', $result, $matches);
    return isset($matches[1]) ? $matches[1] : '';
  }
  //向好友发送飞信
  protected function _toUid($uid, $message) {
    $uri = '/im/chat/sendMsg.action?touserid='.$uid;
    $data = 'msg='.urlencode($message);
    $result = $this->_postWithCookie($uri, $data);
    return $result;
  }
  //给自己发飞信
  protected function _toMyself($message) {
    $uri = '/im/user/sendMsgToMyselfs.action';
    $result = $this->_postWithCookie($uri, 'msg='.urlencode($message));
    return $result;
  }
  //退出飞信
  protected function _logout() {
    $uri = '/im/index/logoutsubmit.action';
    $result = $this->_postWithCookie($uri, '');
    return $result;
  }
  //携带Cookie向f.10086.cn发送POST请求
  protected function _postWithCookie($uri, $data) {
    $fp = fsockopen('f.10086.cn', 80);
    fputs($fp, "POST $uri HTTP/1.1\r\n");
    fputs($fp, "Host: f.10086.cn\r\n");
    fputs($fp, "Cookie: {$this->_cookie}\r\n");
    fputs($fp, "Content-Type: application/x-www-form-urlencoded\r\n");
    fputs($fp, "Content-Length: ".strlen($data)."\r\n");
    fputs($fp, "Connection: close\r\n\r\n");
    fputs($fp, $data);
    $result = '';
    while(!feof($fp)) {
      $result .= fgets($fp);
    }
    fclose($fp);
    return $result;
  }
}
?>

使用实例

 <?php
//实例化 登录
$fetion = new fetion("手机号码","飞信密码");
//发送信息
$fetion -> send("信息内容","接收人的号码");
?>

声明: 本站遵循 署名-非商业性使用-相同方式共享 3.0 共享协议.转载请注明转自 HeQee

发表评论

电子邮件地址不会被公开。 必填项已用 * 标注

*

您可以使用这些 HTML 标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>


无觅相关文章插件,快速提升流量