PHP code example of bolechen / wechat-php-sdk
1. Go to this page and download the library: Download bolechen/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' );
bolechen / wechat-php-sdk example snippets
$options = array (
'token' =>'tokenaccesskey' ,
'encodingaeskey' =>'encodingaeskey' ,
'appid' =>'wxdk1234567890' ,
'appsecret' =>'xxxxxxxxxxxxxxxxxxx'
);
$weObj = new Wechat($options);
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' ;
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' ;
const EVENT_MENU_SCAN_WAITMSG = 'scancode_waitmsg' ;
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 );
if ($ret)
echo $ret;
else
echo "未找到对应的内容" ;
$options = array (
'token' =>'tokenaccesskey' ,
'encodingaeskey' =>'encodingaeskey' ,
'appid' =>'wxdk1234567890' ,
'appsecret' =>'xxxxxxxxxxxxxxxxxxx' ,
'agentid' =>'1' ,
'debug' =>false ,
'_logcallback' =>'logg' ,
);
$weObj = new Wechat($options);
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' ;
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' ;
const EVENT_MENU_SCAN_WAITMSG = 'scancode_waitmsg' ;
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' ,
'appsecret' =>'xxxxxxxxxxxxxxxxxxx' ,
'partnerid' =>'88888888' ,
'partnerkey' =>'' ,
'paysignkey' =>''
);
$weObj = new Wechat($options);
class TPWechat extends Wechat
{
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 ;
}
protected function setCache ($cachename,$value,$expired) {
return S($cachename,$value,$expired);
}
protected function getCache ($cachename) {
return S($cachename);
}
protected function removeCache ($cachename) {
return S($cachename,null );
}
}
array (
'token' =>'tokenaccesskey' ,
'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' ,
'encodingaeskey' =>'d4o9WVg8sxxxxxxxxxxxxxxxxxxxxxx' ,
'appid' =>'wxa07979baxxxxxxxx' ,
);
$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();
}
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['has_reply' ]==0 )
$wechat->send($topmsg['fakeid' ],'hi ' .$topmsg['nick_name' ].',rev:' .$topmsg['content' ]);
}
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();