PHP code example of yangjy15033 / wechat-php-sdk
1. Go to this page and download the library: Download yangjy15033/wechat-php-sdk library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
yangjy15033 / wechat-php-sdk example snippets
$options = array(
'token'=>'tokenaccesskey', //填写你设定的key
'encodingaeskey'=>'encodingaeskey', //填写加密用的EncodingAESKey
'appid'=>'wxdk1234567890', //填写高级调用功能的app id, 请在微信开发模式后台查询
'appsecret'=>'xxxxxxxxxxxxxxxxxxx' //填写高级调用功能的密钥
);
$weObj = new Wechat($options); //创建实例对象
//TODO:调用$weObj各实例方法
////消息类型,使用实例调用getRevType()方法取得
const MSGTYPE_TEXT = 'text';
const MSGTYPE_IMAGE = 'image';
const MSGTYPE_LOCATION = 'location';
const MSGTYPE_LINK = 'link';
const MSGTYPE_EVENT = 'event';
const MSGTYPE_MUSIC = 'music';
const MSGTYPE_NEWS = 'news';
const MSGTYPE_VOICE = 'voice';
const MSGTYPE_VIDEO = 'video';
////事件类型,使用实例调用getRevEvent()方法取得
const EVENT_SUBSCRIBE = 'subscribe'; //订阅
const EVENT_UNSUBSCRIBE = 'unsubscribe'; //取消订阅
const EVENT_SCAN = 'SCAN'; //扫描带参数二维码
const EVENT_LOCATION = 'LOCATION'; //上报地理位置
const EVENT_MENU_VIEW = 'VIEW'; //菜单 - 点击菜单跳转链接
const EVENT_MENU_CLICK = 'CLICK'; //菜单 - 点击菜单拉取消息
const EVENT_MENU_SCAN_PUSH = 'scancode_push'; //菜单 - 扫码推事件(客户端跳URL)
const EVENT_MENU_SCAN_WAITMSG = 'scancode_waitmsg'; //菜单 - 扫码推事件(客户端不跳URL)
const EVENT_MENU_PIC_SYS = 'pic_sysphoto'; //菜单 - 弹出系统拍照发图
const EVENT_MENU_PIC_PHOTO = 'pic_photo_or_album'; //菜单 - 弹出拍照或者相册发图
const EVENT_MENU_PIC_WEIXIN = 'pic_weixin'; //菜单 - 弹出微信相册发图器
const EVENT_MENU_LOCATION = 'location_select'; //菜单 - 弹出地理位置选择器
const EVENT_SEND_MASS = 'MASSSENDJOBFINISH'; //发送结果 - 高级群发完成
const EVENT_SEND_TEMPLATE = 'TEMPLATESENDJOBFINISH';//发送结果 - 模板消息发送结果
const EVENT_KF_SEESION_CREATE = 'kfcreatesession'; //多客服 - 接入会话
const EVENT_KF_SEESION_CLOSE = 'kfclosesession'; //多客服 - 关闭会话
const EVENT_KF_SEESION_SWITCH = 'kfswitchsession'; //多客服 - 转接会话
const EVENT_CARD_PASS = 'card_pass_check'; //卡券 - 审核通过
const EVENT_CARD_NOTPASS = 'card_not_pass_check'; //卡券 - 审核未通过
const EVENT_CARD_USER_GET = 'user_get_card'; //卡券 - 用户领取卡券
const EVENT_CARD_USER_DEL = 'user_del_card'; //卡券 - 用户删除卡券
$ret=ErrCode::getErrText(48001); //错误码可以通过公众号类库的公开变量errCode得到
if ($ret)
echo $ret;
else
echo "未找到对应的内容";
$options = array(
'token'=>'tokenaccesskey', //填写应用接口的Token
'encodingaeskey'=>'encodingaeskey', //填写加密用的EncodingAESKey
'appid'=>'wxdk1234567890', //填写高级调用功能的app id
'appsecret'=>'xxxxxxxxxxxxxxxxxxx', //填写高级调用功能的密钥
'agentid'=>'1', //应用的id
'debug'=>false, //调试开关
'_logcallback'=>'logg', //调试输出方法,需要有一个string类型的参数
);
$weObj = new Wechat($options); //创建实例对象
//TODO:调用$weObj各实例方法
////消息类型,使用实例调用getRevType()方法取得
const MSGTYPE_TEXT = 'text';
const MSGTYPE_IMAGE = 'image';
const MSGTYPE_LOCATION = 'location';
const MSGTYPE_LINK = 'link'; //暂不支持
const MSGTYPE_EVENT = 'event';
const MSGTYPE_MUSIC = 'music'; //暂不支持
const MSGTYPE_NEWS = 'news';
const MSGTYPE_VOICE = 'voice';
const MSGTYPE_VIDEO = 'video';
////事件类型,使用实例调用getRevEvent()方法取得
const EVENT_SUBSCRIBE = 'subscribe'; //订阅
const EVENT_UNSUBSCRIBE = 'unsubscribe'; //取消订阅
const EVENT_LOCATION = 'LOCATION'; //上报地理位置
const EVENT_ENTER_AGENT = 'enter_agent'; //用户进入应用
const EVENT_MENU_VIEW = 'VIEW'; //菜单 - 点击菜单跳转链接
const EVENT_MENU_CLICK = 'CLICK'; //菜单 - 点击菜单拉取消息
const EVENT_MENU_SCAN_PUSH = 'scancode_push'; //菜单 - 扫码推事件(客户端跳URL)
const EVENT_MENU_SCAN_WAITMSG = 'scancode_waitmsg'; //菜单 - 扫码推事件(客户端不跳URL)
const EVENT_MENU_PIC_SYS = 'pic_sysphoto'; //菜单 - 弹出系统拍照发图
const EVENT_MENU_PIC_PHOTO = 'pic_photo_or_album'; //菜单 - 弹出拍照或者相册发图
const EVENT_MENU_PIC_WEIXIN = 'pic_weixin'; //菜单 - 弹出微信相册发图器
const EVENT_MENU_LOCATION = 'location_select'; //菜单 - 弹出地理位置选择器
const EVENT_SEND_MASS = 'MASSSENDJOBFINISH'; //发送结果 - 高级群发完成
const EVENT_SEND_TEMPLATE = 'TEMPLATESENDJOBFINISH';//发送结果 - 模板消息发送结果
$options = array(
'appid'=>'wxdk1234567890', //填写高级调用功能的app id, 请在微信开发模式后台查询
'appsecret'=>'xxxxxxxxxxxxxxxxxxx', //填写高级调用功能的密钥
'partnerid'=>'88888888', //财付通商户身份标识,支付权限专用,没有可不填
'partnerkey'=>'', //财付通商户权限密钥Key,支付权限专用
'paysignkey'=>'' //商户签名密钥Key,支付权限专用
);
$weObj = new Wechat($options); //创建实例对象
//TODO:调用$weObj各实例方法
/**
* 微信公众平台PHP-SDK, ThinkPHP实例
* @author [email protected]
* @link https://github.com/dodgepudding/wechat-php-sdk
* @version 1.2
* usage:
* $options = array(
* 'token'=>'tokenaccesskey', //填写你设定的key
* 'encodingaeskey'=>'encodingaeskey', //填写加密用的EncodingAESKey
* 'appid'=>'wxdk1234567890', //填写高级调用功能的app id
* 'appsecret'=>'xxxxxxxxxxxxxxxxxxx' //填写高级调用功能的密钥
* );
* $weObj = new TPWechat($options);
* $weObj->valid();
* ...
*
*/
class TPWechat extends Wechat
{
/**
* log overwrite
* @see Wechat::log()
*/
protected function log($log){
if ($this->debug) {
if (function_exists($this->logcallback)) {
if (is_array($log)) $log = print_r($log,true);
return call_user_func($this->logcallback,$log);
}elseif (class_exists('Log')) {
Log::write('wechat:'.$log, Log::DEBUG);
}
}
return false;
}
/**
* 重载设置缓存
* @param string $cachename
* @param mixed $value
* @param int $expired
* @return boolean
*/
protected function setCache($cachename,$value,$expired){
return S($cachename,$value,$expired);
}
/**
* 重载获取缓存
* @param string $cachename
* @return mixed
*/
protected function getCache($cachename){
return S($cachename);
}
/**
* 重载清除缓存
* @param string $cachename
* @return boolean
*/
protected function removeCache($cachename){
return S($cachename,null);
}
}
//test1.php
array(
'token'=>'tokenaccesskey', //填写你设定的key
'encodingaeskey'=>'encodingaeskey' //填写加密用的EncodingAESKey,如接口为明文模式可忽略
);
$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();
}
$options = array(
'token'=>'9Ixxxxxxx', //填写应用接口的Token
'encodingaeskey'=>'d4o9WVg8sxxxxxxxxxxxxxxxxxxxxxx',//填写加密用的EncodingAESKey
'appid'=>'wxa07979baxxxxxxxx', //填写高级调用功能的appid
);
$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();
}
// old_version/test/test2.php
file_put_contents('./data/log.txt',$text."\n",FILE_APPEND);
};
$options = array(
'account'=>'[email protected] ',
'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['has_reply']==0)
$wechat->send($topmsg['fakeid'],'hi '.$topmsg['nick_name'].',rev:'.$topmsg['content']);
}
// old_version/test/test3.php
sion_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(); //待输出的二维码图片