Bläddra i källkod

fullfill README and comments.

dodgepudding 12 år sedan
förälder
incheckning
b318ee1221
6 ändrade filer med 58 tillägg och 9 borttagningar
  1. 29 4
      README.md
  2. 1 0
      Thinkphp/Wechatauth.class.php
  3. 11 2
      Thinkphp/Wechatext.class.php
  4. 6 2
      test/test2.php
  5. 1 0
      wechatauth.class.php
  6. 10 1
      wechatext.class.php

+ 29 - 4
README.md

@@ -10,8 +10,29 @@ weixin developer SDK.
 http://mp.weixin.qq.com/wiki/index.php?title=%E6%B6%88%E6%81%AF%E6%8E%A5%E5%8F%A3%E6%8C%87%E5%8D%97  
 
 wechat.class.php调用官方API, 支持链式调用操作 ;  
-wechatext.class.php为非官方API,需要配置公众平台账户和密码,能实现主动点对点微信,此方式不保证长期有效。  
-wechatauth.class.php为通过微信二维码登陆微信的API, 能同时实现在网站同步登陆.
+wechatext.class.php为非官方API,需要配置公众平台账户和密码,能实现对已关注用户的点对点微信,此方式不保证长期有效。   
+类方法里提及的用户id在接口返回结构里表述为FakeId是同一概念, 在下面wechatauth类里则表示为Uin, 用户id对应的微信号必须通过getInfo()方法通过返回数组的Username值获取, 但非关注关系用户资料不能获取.  
+调用下列方法前必须经过login()方法和checkValid()验证方法才能获得调用权限.  
+类主要方法:
+ *  send($id,$content) 向某用户id发送微信文字信息  
+ *  batch($ids,$content) 批量向一批用户发送微信文字信息  
+ *  sendNews($account,$title,$summary,$content,$pic,$srcurl='') 向一个微信账户发送图文信息  
+ *  getInfo($id) 根据id获取用户资料,注: 非关注关系用户资料不能获取  
+ *  getNewMsgNum($lastid) 获取从$lastid算起新消息的数目  
+ *  getTopMsg() 获取最新一条消息的数据, 此方法获取的消息id可以作为检测新消息的$lastid依据  
+ *  getMsg($lastid,$offset=0,$perpage=50,$day=0,$today=0,$star=0) 获取最新的消息列表, 列表将返回消息id, 用户id, 消息类型, 文字消息等参数  
+ *  消息返回结构:  {"id":"消息id","type":"类型号(1为文字,2为图片,3为语音)","fileId":"0","hasReply":"0","fakeId":"用户uid","nickName":"昵称","dateTime":"时间戳","content":"文字内容"}   
+ *  getMsgImage($msgid,$mode='large') 若消息type类型为2, 调用此方法获取图片数据  
+ *  getMsgVoice($msgid) 若消息type类型为3, 调用此方法获取语音数据  
+
+wechatauth.class.php为通过微信二维码登陆微信的API, 能实现第三方网站同步登陆, 验证步骤请看test3.php例子.  
+类主要方法:
+ *  get_login_code() 获取登陆授权码, 通过授权码才能获取二维码  
+ *  get_code_image($code='') 将上面获取的授权码转换为图片二维码  
+ *  verify_code() 鉴定是否登陆成功,返回200为最终授权成功.  
+ *  get_login_cookie() 鉴定成功后调用此方法即可获取用户基本信息  
+ *  sendNews($account,$title,$summary,$content,$pic,$srcurl='') 向一个微信账户发送图文信息  
+ *  get_avatar($url) 获取用户头像图片数据  
 
 * 基础API调用示例:  
 ```php
@@ -57,9 +78,13 @@ switch($type) {
 	if ($wechat->checkValid()) {
 		// 获取用户信息
 		$data = $wechat->getInfo('3974255');
-		// 主动发消息
-		//$wechat->send('3974255','hello '.time());
 		var_dump($data);
+		// 获取最新一条消息
+		$topmsg = $wechat->getTopMsg();
+		var_dump($topmsg);
+		// 主动回复消息
+		if ($topmsg && $topmsg['hasReply']==0)
+		$wechat->send($topmsg['fakeId'],'hi '.$topmsg['nickName'].',rev:'.$topmsg['content']);	
 	}
 ```
 

+ 1 - 0
Thinkphp/Wechatauth.class.php

@@ -9,6 +9,7 @@
  *  verify_code() 鉴定是否登陆成功,返回200为最终授权成功.
  *  get_login_cookie() 鉴定成功后调用此方法即可获取用户基本信息
  *  sendNews($account,$title,$summary,$content,$pic,$srcurl='') 向一个微信账户发送图文信息
+ *  get_avatar($url) 获取用户头像图片数据
  *  @author dodge <dodgepudding@gmail.com>
  *  @link https://github.com/dodgepudding/wechat-php-sdk
  *  @version 1.1

+ 11 - 2
Thinkphp/Wechatext.class.php

@@ -2,15 +2,24 @@
 /**
  *	微信公众平台PHP-SDK
  *  Wechatext为非官方微信发送API
+ *  注: 用户id为通过getMsg()方法获取的FakeId值
  *  主要实现如下功能:
  *  send($id,$content) 向某用户id发送微信文字信息
  *  batch($ids,$content) 批量向一批用户发送微信文字信息
  *  sendNews($account,$title,$summary,$content,$pic,$srcurl='') 向一个微信账户发送图文信息
+ *  getInfo($id) 根据id获取用户资料
+ *  getNewMsgNum($lastid) 获取从$lastid算起新消息的数目
+ *  getTopMsg() 获取最新一条消息的数据, 此方法获取的消息id可以作为检测新消息的$lastid依据
+ *  getMsg($lastid,$offset=0,$perpage=50,$day=0,$today=0,$star=0) 获取最新的消息列表, 列表将返回消息id, 用户id, 消息类型, 文字消息等参数
+ *  消息返回结构:  {"id":"消息id","type":"类型号(1为文字,2为图片,3为语音)","fileId":"0","hasReply":"0","fakeId":"用户uid","nickName":"昵称","dateTime":"时间戳","content":"文字内容"} 
+ *  getMsgImage($msgid,$mode='large') 若消息type类型为2, 调用此方法获取图片数据
+ *  getMsgVoice($msgid) 若消息type类型为3, 调用此方法获取语音数据
  *  @author dodge <dodgepudding@gmail.com>
  *  @link https://github.com/dodgepudding/wechat-php-sdk
  *  @version 1.1
  *  
  */
+
 include "Snoopy.class.php";
 class Wechatext
 {
@@ -37,7 +46,7 @@ class Wechatext
 
 	/**
 	 * 主动发消息
-	 * @param  string $id      用户的uid
+	 * @param  string $id      用户的uid(即FakeId)
 	 * @param  string $content 发送的内容
 	 */
 	public function send($id,$content)
@@ -139,7 +148,7 @@ class Wechatext
 	
 	/**
 	 * 获取用户的信息
-	 * @param  string $id 用户的uid
+	 * @param  string $id 用户的uid(即FakeId)
 	 * @return [type]     [description]
 	 */
 	public function getInfo($id)

+ 6 - 2
test/test2.php

@@ -19,7 +19,11 @@
 	if ($wechat->checkValid()) {
 		// 获取用户信息
 		$data = $wechat->getInfo('3974255');
-		// 主动发消息
-		//$wechat->send('3974255','hello '.time());
 		var_dump($data);
+		// 获取最新一条消息
+		$topmsg = $wechat->getTopMsg();
+		var_dump($topmsg);
+		// 主动回复消息
+		if ($topmsg && $topmsg['hasReply']==0)
+		$wechat->send($topmsg['fakeId'],'hi '.$topmsg['nickName'].',rev:'.$topmsg['content']);	
 	}

+ 1 - 0
wechatauth.class.php

@@ -9,6 +9,7 @@
  *  verify_code() 鉴定是否登陆成功,返回200为最终授权成功.
  *  get_login_cookie() 鉴定成功后调用此方法即可获取用户基本信息
  *  sendNews($account,$title,$summary,$content,$pic,$srcurl='') 向一个微信账户发送图文信息
+ *  get_avatar($url) 获取用户头像图片数据
  *  @author dodge <dodgepudding@gmail.com>
  *  @link https://github.com/dodgepudding/wechat-php-sdk
  *  @version 1.1

+ 10 - 1
wechatext.class.php

@@ -2,10 +2,18 @@
 /**
  *	微信公众平台PHP-SDK
  *  Wechatext为非官方微信发送API
+ *  注: 用户id为通过getMsg()方法获取的FakeId值
  *  主要实现如下功能:
  *  send($id,$content) 向某用户id发送微信文字信息
  *  batch($ids,$content) 批量向一批用户发送微信文字信息
  *  sendNews($account,$title,$summary,$content,$pic,$srcurl='') 向一个微信账户发送图文信息
+ *  getInfo($id) 根据id获取用户资料
+ *  getNewMsgNum($lastid) 获取从$lastid算起新消息的数目
+ *  getTopMsg() 获取最新一条消息的数据, 此方法获取的消息id可以作为检测新消息的$lastid依据
+ *  getMsg($lastid,$offset=0,$perpage=50,$day=0,$today=0,$star=0) 获取最新的消息列表, 列表将返回消息id, 用户id, 消息类型, 文字消息等参数
+ *  消息返回结构:  {"id":"消息id","type":"类型号(1为文字,2为图片,3为语音)","fileId":"0","hasReply":"0","fakeId":"用户uid","nickName":"昵称","dateTime":"时间戳","content":"文字内容"} 
+ *  getMsgImage($msgid,$mode='large') 若消息type类型为2, 调用此方法获取图片数据
+ *  getMsgVoice($msgid) 若消息type类型为3, 调用此方法获取语音数据
  *  @author dodge <dodgepudding@gmail.com>
  *  @link https://github.com/dodgepudding/wechat-php-sdk
  *  @version 1.1
@@ -38,7 +46,7 @@ class Wechatext
 
 	/**
 	 * 主动发消息
-	 * @param  string $id      用户的uid
+	 * @param  string $id      用户的uid(即FakeId)
 	 * @param  string $content 发送的内容
 	 */
 	public function send($id,$content)
@@ -207,6 +215,7 @@ class Wechatext
 	 * @param $day 最近几天消息(1:昨天,2:前天,3:五天内)
 	 * @param $today 是否只显示今天的消息, 与$day参数不能同时大于0
 	 * @param $star 是否星标组信息
+	 * @return array[] 同getTopMsg()返回的字段结构相同
 	 */
 	public function getMsg($lastid=0,$offset=0,$perpage=50,$day=0,$today=0,$star=0){
 		$send_snoopy = new Snoopy;