实例结构:

1. demo.php

// 获取天气数据

$w = json_decode( file_get_contents(‘http://www.weather.com.cn/data/cityinfo/101280601.html‘) ) ;

$wi = $w->weatherinfo;

$str = "【{$wi->city}】".date(‘Y-m-d‘).‘ 天气: ‘;

$str.= "{$wi->weather}, {$wi->temp1}-{$wi->temp2}";

// 开始发送飞信

require_once(‘PHPFetion.class.php‘);

header ( "Content-Type:text/html; charset=utf-8" );

$fetion = new PHPFetion(‘18898927320‘, ‘c*****00‘);

$rs = $fetion->send(‘18898927320‘, $str);

if(strpos($rs,‘200‘)){

echo ‘发送成功‘;

}else{

echo ‘发送失败‘;

}

2.  PHPFetion.class.php

/**

* PHP飞信发送类

*

* @author quanhengzhuang

* @version 1.5.0

*/

class PHPFetion

{

/**

* 发送者手机号

* @var string

*/

protected $_mobile;

/**

* 飞信密码

* @var string

*/

protected $_password;

/**

* Cookie字符串

* @var string

*/

protected $_cookie = ‘‘;

/**

* Uid缓存

* @var array

*/

protected $_uids = array();

/**

* csrfToken

* @var 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(‘/.*?\r\nSet-Cookie: (.*?);.*?/si‘, $result, $matches);

if (isset($matches[1]))

{

$this->_cookie = implode(‘; ‘, $matches[1]);

}

$result = $this->_postWithCookie(‘/im/login/cklogin.action‘, ‘‘);

return $result;

}

/**

* 向指定的手机号发送飞信

* @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

{

$uid = $this->_getUid($mobile);

return $uid === ‘‘ ? ‘‘ : $this->_toUid($uid, $message);

}

}

/**

* 获取飞信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];

}

/**

* 获取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 $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;

}

/**

* 携带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.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, "User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:14.0) Gecko/20100101 Firefox/14.0.1\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提交飞信,php发送飞信消息相关推荐

  1. 微信小程序发送模版消息获取 formID 方法

    微信小程序开发交流qq群   173683895    承接微信小程序开发.扫码加微信. 想要发送模板消息,首先需要获取到用户的 formId , 因为用到获取用户 formId 的请求很大可能会复用 ...

  2. 程序调用飞信API发送免费短信(JAVA例子,其他语言一样用)

    网上看到有网页版的飞信,http://fetionlib.appspot.com/ 可以添加好友,群发和定时发送短信给飞信好友,还开放了API接口供程序调用,可以用它来监控机器是否正常服务定期给管理员 ...

  3. 使用ASP发送飞信的免费短信

    要实现在asp网站上发送飞信,要求是中国移动的手机用户,并且开通移动飞信功能. 使用以下asp代码,修改参数为你的手机号及飞信密码,就可以实现在网站发免费短信功能. fsend.asp文件源代码如下: ...

  4. 发送飞信免费短信API

    通过分析网页版飞信(地址:https://webim.feixin.10086.cn/),封装成自己的通用API,通过该API可以给自己和飞信好友的手机发送免费短信. 这是官网登录界面截图: 我们可以 ...

  5. Python调用飞信接口发送短信

    主要参考了两篇文章:(对原作者表示感谢~) 发送飞信的Python脚本:http://www.cnblogs.com/fatway/archive/2009/08/07/1693813.html 中国 ...

  6. rabbitmq 手动提交_RabbitMQ系列(四)RabbitMQ事务和Confirm发送方消息确认——深入解读 - 王磊的博客 - 博客园...

    RabbitMQ事务和Confirm发送方消息确认--深入解读 RabbitMQ系列文章 引言 根据前面的知识( 深入了解RabbitMQ工作原理及简单使用 . Rabbit的几种工作模式介绍与实践 ...

  7. 微信公众账号开发-发送模板消息

    内容概要 本篇文章主要叙述如何在微信公众帐号上实现"发送模板消息开发"功能.包含json的封装与解析. 何谓模板消息 为了保证用户不受到骚扰,在开发者出现需要主动提醒.通知用户时, ...

  8. CEMAPI实战攻略(四)——发送短消息

    CEMAPI实战攻略(四)--发送短消息 By 吴春雷 QQ:819543772 EMail:wuchunlei@163.com 四.发送短消息 发送短信是一个相对比较简单的过程,之所以拿出来一节来讨 ...

  9. java发送小程序模板消息,记录_小程序发送模板消息

    package com.mj.frame.entity.resp; import java.io.Serializable; import java.util.List; import java.ut ...

最新文章

  1. iOS的一些常用性能优化,和内存优化的方法
  2. MySQL主键(PRIMARY KEY)
  3. Matlab学习笔记:画图多重设置
  4. js中当等于最小值是让代码不执行_JavaScript中最最基础的知识点
  5. 【Python基础知识-pycharm版】第三节-列表
  6. pip指定源安装_几种python安装简单方法
  7. 3-34Pytorch与nn库
  8. no module named google.protobuf.internal
  9. java面向对象测试题二_JAVA面向对象-测试题
  10. MATLAB基本运算
  11. MyBatis3与Spring3的整合配置(初级篇)
  12. outlook vba html语言,在VBA中更改HTML电子邮件正文字体类型和大小
  13. 问答社区php源码,cpf开源SNS问答社区源码 php版 v0.7.1
  14. Spring boot应用【tailf】服务启动停止管理脚本
  15. “我不是不在乎钱,我只是不在乎这点钱。”
  16. 知道三个金,三个火,三水~~都念什么吗?
  17. C++:实现一些简单的方法来 布莱克-斯科尔斯期权估值理论(附完整源码)
  18. 让阿里再次伟大--钉钉如何长成独角兽的?
  19. 物理学陷入困境:接下来该怎么办?
  20. scrapy微博反爬虫_Scrapy 爬取新浪微博(解析api)

热门文章

  1. 编译linux内核预备,Linux内核预备知识(1)
  2. php怎么实现md5加密,php如何进行md5加密
  3. Flink 与 TiDB 联合发布实时数仓最佳实践白皮书
  4. 浅谈如何成为技术一号位
  5. PolarDB-X 2.0:使用一个透明的分布式数据库是一种什么体验?
  6. 打破重重阻碍,Flutter 和 Web 生态如何对接?
  7. 百万TPS高吞吐、秒级低延迟,阿里​搜索离线平台如何实现?
  8. oracle语句加减,oracle时间加减的语句写法
  9. 砖家:游戏账号交易属违法行为 相关部门应严厉打击
  10. 神经网络训练中,错误数据集对模型结果的影响有多大