Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Open more actions menu
 
 

Repository files navigation

wechat-php-sdk

微信公众平台php开发包,细化各项接口操作,支持链式调用,欢迎Fork此项目
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,需要配置公众平台账户和密码,能实现对已关注用户的点对点微信,此方式不保证长期有效。
类方法里提及的用户id在接口返回结构里表述为FakeId是同一概念, 在下面wechatauth类里则表示为Uin, 用户id对应的微信号必须通过getInfo()方法通过返回数组的Username值获取, 但非关注关系用户资料不能获取.
调用下列方法前必须经过login()方法和checkValid()验证方法才能获得调用权限.

类主要方法:

  • send($id,$content) 向某用户id发送微信文字信息
  • 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) 获取用户头像图片数据

官方Wechat调用示例:

//test1.php
include "wechat.class.php"
$options = array(
		'token'=>'tokenaccesskey' //填写你设定的key
	);
$weObj = new Wechat($options);
$weObj->valid(); //注意, 应用验证通过后,可将此句注释掉, 但会降低网站安全性
$type = $weObj->getRev()->getRevType();
switch($type) {
	case Wechat::MSGTYPE_TEXT:
			$weObj->text("hello, I'm wechat")->reply();
			exit;
			break;
	case Wechat::MSGTYPE_EVENT:
			break;
	case Wechat::MSGTYPE_IMAGE:
			break;
	default:
			$weObj->text("help info")->reply();
}

扩展包Wechatext调用示例:

//test2.php 
	include("wechatext.class.php");
	
	function logdebug($text){
		file_put_contents('./data/log.txt',$text."\n",FILE_APPEND);		
	};
	
	$options = array(
		'account'=>'demo@domain.com',
		'password'=>'demo',
		'datapath'=>'./data/cookie_',
			'debug'=>true,
			'logcallback'=>'logdebug'	
	); 
	$wechat = new Wechatext($options);
	if ($wechat->checkValid()) {
		// 获取用户信息
		$data = $wechat->getInfo('3974255');
		var_dump($data);
		// 获取最新一条消息
		$topmsg = $wechat->getTopMsg();
		var_dump($topmsg);
		// 主动回复消息
		if ($topmsg && $topmsg['hasReply']==0)
		$wechat->send($topmsg['fakeId'],'hi '.$topmsg['nickName'].',rev:'.$topmsg['content']);	
	}

微信二维码Wechatauth登陆示例:

//test3.php
	include("../wechatauth.class.php");
	session_start();
	$sid  = session_id();
	$options = array(
		'account'=>$sid,
		'datapath'=>'../data/cookiecode_',
	); 
	$wechat = new Wechatauth($options);
	
	if (isset($_POST['code'])) {
		$logincode = $_POST['code'];
		$vres = $wechat->set_login_code($logincode)->verify_code();
		if ($vres===false) {
			$result = array('status'=>0);
		} else {
			$result = array('status'=>$vres);
			if ($vres==200) {
				$result['info'] = $wechat->get_login_info();
				$result['cookie'] = $wechat->get_login_cookie(true);
			}
		}
		
		die(json_encode($result));	
	}
	$logincode =  $wechat->get_login_code(); //获取授权码
	$qrimg = $wechat->get_code_image(); //待输出的二维码图片

HTML部分请看test/test3.php, 主要是定时ajax查询是否已经授权成功

License

This is licensed under the GNU LGPL, version 2.1 or later.
For details, see: http://creativecommons.org/licenses/LGPL/2.1/

About

微信公众平台php开发包, weixin developer SDK.

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Morty Proxy This is a proxified and sanitized view of the page, visit original site.