Home  >  Article  >  php教程  >  关于PHPFetion不能给好友发送飞信的问题

关于PHPFetion不能给好友发送飞信的问题

WBOY
WBOYOriginal
2016-06-06 20:07:211263browse

(最新版本下载:http://code.google.com/p/php-fetion/downloads/detail?name=phpfetion_v1.3.0.zip) 最近一直有网友反映,PHPFetion类不能给好友发送飞信了,但给自己还可以。通过网友的提示以及我自己的研究发现,在wap飞信中给好友发信息时,加了个csrf

(最新版本下载:http://code.google.com/p/php-fetion/downloads/detail?name=phpfetion_v1.3.0.zip)

最近一直有网友反映,PHPFetion类不能给好友发送飞信了,但给自己还可以。通过网友的提示以及我自己的研究发现,在wap飞信中给好友发信息时,加了个csrfToken字段。

不出意料的话,这个csrfToken应该就是起个防止表单跨站提交的作用。但由于这个csrfToken明文出现在了给好友发飞信的表单页面里,所以获取它也很容易。并且这个csrfToken在给其他任何好友发飞信时,都是不变的,所以只需要获取一次即可。

我写了如下代码来获取csrfToken:

/**
 * csrfToken
 * @param string
 */
protected $_csrfToten = null;
/**
 * 获取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;
}

另外也修改了给好友发飞信的_toUid()函数:

/**
 * 向好友发送飞信
 * @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;
}

写好的代码依然放在了Google Code中,并打包成了最新版本PHPFetion v1.3,下载地址为:http://code.google.com/p/php-fetion/downloads/list。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn