欢迎使用金字塔普通技术服务论坛,您可以在相关区域发表技术支持贴。
我司技术服务人员将优先处理 VIP客服论坛 服务贴,普通区问题处理速度慢,请耐心等待。谢谢您对我们的支持与理解。


金字塔客服中心 - 专业程序化交易软件提供商金字塔软件高级功能研发区 → [转帖]飞信api源码

   

欢迎使用金字塔普通技术服务论坛,您可以在相关区域发表技术支持贴。
我司技术服务人员将优先处理 VIP客服论坛 服务贴,普通区问题处理速度慢,请耐心等待。谢谢您对我们的支持与理解。    


  共有6458人关注过本帖平板打印复制链接

主题:[转帖]飞信api源码

帅哥哟,离线,有人找我吗?
z7c9
  1楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:小飞侠 帖子:1882 积分:3310 威望:0 精华:15 注册:2010/3/15 13:11:56
  发帖心情 Post By:2012/1/26 10:24:01 [只看该作者]

/* Copyright 2010 princehaku
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Created on : 2010-11-18
* Last Update: 2011-12-06
* Author : princehaku
* Site : http://3haku.net
*
* 注意 : 请不要将此脚本用于商业用途..
* 此脚本完全是模拟了一个浏览器让用户自主操作,不存在破解或重打包客户端等行为。
* 此脚本以学习为目的,不涉及任何商业利益。任何企业和个人与此接口有关的商业行为,请与移动公司联系。
* 任何人使用此脚本而造成的不良后果,均由使用者承担,与此脚本的作者没有任何关系。
*
*
* 1.4fix
* 修复了移动修改jsessionid发生错误的问题
* 修复了cookie获取代码,按照rfc规范进行解析
* 修复了部分warning
*
* 1.3fix
* 感谢:
* 七子狴犴 指出的一个bug,修复了添加好友的时候姓名错误
* 追赶太阳 指出的一个bug,修复了cookie获取的代码
* 一路狂飙 提供的给自己发信的函数
*/
define("DEBUG", 0);

if (DEBUG == 1) {
error_reporting(9);
} else {
error_reporting(0);
}

header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Cache-Control: no-cache, must-revalidate");
header("Pragma: no-cache");

/**用curl做的联网类
*/
class httpconnector {
/**Curl类
*
*/
private $curl;
/**cookie字符串
*/
private $cookie;
/**源(用于最后结果调试)
*/
private $sourceWmlStack = array();
/**得到源wml栈
*/
public function getSource() {
return $this->sourceWmlStack;
}
/**get方式下载网页内容
*@param $url
*@return web conntent
*/
public function get($url) {

$this->curl = curl_init();

curl_setopt($this->curl, CURLOPT_URL, $url);

// 设置header
curl_setopt($this->curl, CURLOPT_HEADER, 1);
curl_setopt($this->curl, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
curl_setopt($this->curl, CURLOPT_COOKIE, $this->cookie);

// 设置cURL 参数,要求结果保存到字符串中还是输出到屏幕上。
curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, 1);

// 运行cURL,请求网页
$data = curl_exec($this->curl);
// 关闭URL请求
curl_close($this->curl);
// 找到cookie 放入cookiestring
preg_match_all("/Set-Cookie:(.*?);/", $data, $match, PREG_SET_ORDER);
foreach ($match as $r) {
if ($this->cookie != '') {
$this->cookie = $this->cookie . ';';
}
if (isset($r[1])) {
$this->cookie .= trim(str_replace("\r\n", "", $r[1]));
}
}
//放入调试栈
array_push($this->sourceWmlStack, " [$url] " . $data);

return $data;

}

/**POST方式下载网页内容
*@param $url
*@param $params post的信息串
*@return web conntent
*/
public function post($url, $params) {

$this->curl = curl_init();

curl_setopt($this->curl, CURLOPT_URL, $url);

// 设置header
curl_setopt($this->curl, CURLOPT_HEADER, 1);
curl_setopt($this->curl, CURLOPT_COOKIE, $this->cookie);
curl_setopt($this->curl, CURLOPT_POST, 1);
curl_setopt($this->curl, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
curl_setopt($this->curl, CURLOPT_POSTFIELDS, $params);

// 设置cURL 参数,要求结果保存到字符串中还是输出到屏幕上。
curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, 1);

// 运行cURL,请求网页
$data = curl_exec($this->curl);

// 关闭URL请求
curl_close($this->curl);
// 找到cookie 放入cookiestring
preg_match_all("/Set-Cookie:(.*?);/", $data, $match, PREG_SET_ORDER);

foreach ($match as $r) {
if ($this->cookie != '') {
$this->cookie = $this->cookie . ';';
}
if (isset($r[1])) {
$this->cookie .= trim(str_replace("\r\n", "", $r[1]));
}
}

//放入调试栈
array_push($this->sourceWmlStack, " [$url] " . $data);

return $data;

}
}
/**飞信类
*/
class fection {

private $jsession;

private $r;

private $httpconnector;
/**发送方手机号码
*/
private $phonenum;
/**发送方飞信密码
*/
private $pwd;
/**是否成功登陆
*/
private $islogin = 0;
/**操作记录
*/
private $status = array();

function track($action, $statu, $ph") {
$phone = $this->phonenum;
}
$r = array("action" => $action, "statu" => $statu, "phone" => $phone);
array_push($this->status, $r);
}
function __construct($phonenum, $pwd) {
$this->phonenum = $phonenum;
$this->pwd = $pwd;
$this->httpconnector = new httpconnector();
}
/**登陆飞信
*/
function login() {
// 得到ssid
$data = $this->httpconnector->get('http://f.10086.cn/im/login/login.action');
preg_match("/jsessionid=(.*?)[\"\?]/", $data, $r);
if (empty($r[1])) {
$this->track("login", "faild");
$this->islogin = 0;
return;
}
$this->jsession = $r[1];
// 效验是否需要验证码
preg_match("/verifycode.jpg\?code=(.*?)\"/", $data, $r);
if (isset($r[1])) {
$this->track("login", "need verify");
$this->islogin = 0;
return;
}
// 开始登陆
$data = $this->httpconnector->post('http://f.10086.cn/im/login/inputpasssubmit1.action;jsessiquery'])) {
$this->track("login", "faild");
$this->islogin = 0;
return;
}
$r = explode("=", $r['query']);
$this->t = $r['1'];
if ($this->t == "") {
$this->track("login", "faild");
$this->islogin = 0;
return;
} else {
$this->track("login", "success");
$this->islogin = 1;
}

}
/**退出飞信
*/
function logout() {
if ($this->islogin) {
$this->httpconnector->get("http://f.10086.cn/im/index/logoutsubmit.action;jsessionid=$this->jsession?t=$this->t");
}
}
/** 给自己发送短信
*
* @param mixed $msg
*/
function sendToSelf($msg) {
if (!$this->islogin) {
return;
}

$data = $this->httpconnector->post("http://f.10086.cn/im/user/sendMsgToMyselfs.action?t=$this->t", "msg=$msg");

if ((int)strpos($data, "成功") > 10) {
$this->track("sendmessage", "success");
return;
}

$this->track("sendmessage", "faild");

}
/**
* 给某飞信id发送消息
*
* @param mixed $fid
* @param mixed $msg
* @param mixed $towho
*/
function sendTofid($fid, $msg, $towho) {
if (!$this->islogin) {
return;
}
if ($towho == "") {
$towho = "0";
}
$data = $this->httpconnector->post("http://f.10086.cn/im/chat/sendMsg.action?t=$this->t&touserid=$fid", "msg=$msg&touchTextLength=&touchTitle=");

if ((int)strpos($data, "成功") > 10) {
$this->track("sendmessage", "success", $towho);
$this->savefidLocal($towho, $fid);
return;
}

$this->track("sendmessage", "faild", $towho);
}
/**发送飞信短信
*@param $fid 接收方的手机号码或者昵称
*@param $msg 消息正文
*/
function send($towho, $msg) {
if (!$this->islogin) {
return;
}
$fid = $this->phonenum;
//如果不是给自己发送的转换uid
if ($towho != $this->phonenum) {
$fid = $this->getfidLocal($towho);
if ($fid == "") {
$fid = $this->getfidNet($towho);
}
}
if ($fid != $this->phonenum && $fid == "") {
$this->track("tofid", "faild", $towho);
return;
}
$data = "";

if ($towho == $this->phonenum) {
$this->sendToSelf($msg);
} else {
$this->sendTofid($fid, $msg, $towho);
}
}
/**添加用户为好友
*/
function addfriend($phone, $name) {
if (!$this->islogin) {
return;
}

if ($name == "") {
$name = "haku";
}

$data = $this->httpconnector->post("http://f.10086.cn/im/user/insertfriendsubmit.action?t=$this->t", "nickname=$name&number=$phone&type=0");

if ((int)strpos($data, "对不起,") > 0) {
$this->track("addfriend", "faild", $phone);
return;
}

$this->track("addfriend", "success", $phone);
}
/**用于将手机号或者飞信号或者昵称转换为飞信id
* param $val
*/
function getfidNet($val) {
if (!$this->islogin) {
return;
}
$data = $this->httpconnector->post("http://f.10086.cn/im/index/searchOtherInfoList.action?t=$this->t", "searchText=$val");

$pos1 = (int)strpos($data, "touserid=");
$pos2 = (int)strpos($data, "&", $pos1);
$uid = "";
if ($pos1 > 0 && $pos2 > 0) {
$uid = substr($data, $pos1 + 9, $pos2 - $pos1 - 9);
}

return $uid;
}
/**用于从本地将手机号或者飞信号或者昵称转换为飞信id
* param $val
*/
function getfidLocal($val) {
$data = file_get_contents("pafetion.cache");
preg_match("/$val==####==(\d*)/", $data, $r);

if (@$r[1] == null) {
$r[1] = "";
}
return $r[1];
}
function savefidLocal($val, $fid) {
if ($this->getfidLocal($val) != "") {
return;
}
$data = file_get_contents("pafetion.cache");
$data = $data . "$val==####==$fid\n";
file_put_contents("pafetion.cache", $data);
return;
}
/**输出结果至xml
*
*/
function toXml() {
header("Content-Type: text/xml");
echo "\r\n";
echo "";
echo "";
foreach ($this->status as $i => $j) {
echo "" . $j['action'] . "";
echo "" . $j['statu'] . "";
echo "";
}
echo "
";
if (DEBUG == 1) {
echo "";
foreach ($this->httpconnector->getSource() as $i => $j) {
echo "<![CDATA[";
echo ($j);
echo "]]>";
}
echo "
";
}
echo "
";
}
}

$phone = @$_GET["phone"];
$pwd = @$_GET["pwd"];
$to = @$_GET["to"];
$msg = @$_GET['msg'];
$add = @$_GET['add'];
if (@$_GET['u'] == "") {
@$msg = iconv("gbk", "UTF-8", $msg);
}
if (!($phone && $pwd && ($msg && $to || $add))) {
die("wrong parameters, please refer to princehaku");
}

$fection = new fection($phone, $pwd);
$fection->login();
//添加好友的操作
if ($add != "") {
$res = explode(",", $add);
foreach ($res as $i => $adds) {
$fection->addfriend($adds, @$_GET['name']);
}
} else {
//发送短信的操作
$res = explode(",", $to);
foreach ($res as $i => $tos) {
$fection->send($tos, $msg);
}
}

$fection->logout();
//打印结果
$fection->toXml();
?>

 回到顶部