src
parent
0be0f0bb26
commit
e426c0f308
@ -0,0 +1,13 @@
|
|||||||
|
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
|
||||||
|
Version 2, December 2004
|
||||||
|
|
||||||
|
Copyright (C) 2014 Lane
|
||||||
|
|
||||||
|
Everyone is permitted to copy and distribute verbatim or modified
|
||||||
|
copies of this license document, and changing it is allowed as long
|
||||||
|
as the name is changed.
|
||||||
|
|
||||||
|
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
|
||||||
|
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
|
||||||
|
|
||||||
|
0. You just DO WHAT THE FUCK YOU WANT TO.
|
@ -1,6 +1,2 @@
|
|||||||
# git-test
|
第一次作业
|
||||||
jyp 12.16
|
|
||||||
duanzejia 12.16
|
|
||||||
jiajiayi 12.16
|
|
||||||
lihaoyang 12.16
|
|
||||||
hezhefeng 12.16 21:26
|
|
||||||
|
@ -0,0 +1,25 @@
|
|||||||
|
<?php
|
||||||
|
namespace LaneWeChat;
|
||||||
|
|
||||||
|
class Autoloader{
|
||||||
|
const NAMESPACE_PREFIX = 'LaneWeChat\\';
|
||||||
|
|
||||||
|
public static function register(){
|
||||||
|
spl_autoload_register(array(new self, 'autoload'));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static function autoload($className){
|
||||||
|
$namespacePrefixStrlen = strlen(self::NAMESPACE_PREFIX);
|
||||||
|
if(strncmp(self::NAMESPACE_PREFIX, $className, $namespacePrefixStrlen) === 0){
|
||||||
|
$className = strtolower($className);
|
||||||
|
$filePath = str_replace('\\', DIRECTORY_SEPARATOR, substr($className, $namespacePrefixStrlen));
|
||||||
|
$filePath = realpath(__DIR__ . (empty($filePath) ? '' : DIRECTORY_SEPARATOR) . $filePath . '.lib.php');
|
||||||
|
if(file_exists($filePath)){
|
||||||
|
require_once $filePath;
|
||||||
|
}else{
|
||||||
|
echo $filePath;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,47 @@
|
|||||||
|
<?php
|
||||||
|
namespace LaneWeChat;
|
||||||
|
|
||||||
|
|
||||||
|
define('LANEWECHAT_VERSION', '1.4');
|
||||||
|
define('LANEWECHAT_VERSION_DATE', '2014-11-05');
|
||||||
|
|
||||||
|
|
||||||
|
define("WECHAT_URL", 'http:
|
||||||
|
define('WECHAT_TOKEN', 'weixin');
|
||||||
|
define('ENCODING_AES_KEY', "MqAuKoex6FyT5No0OcpRyCicThGs0P1vz4mJ2gwvvkF");
|
||||||
|
|
||||||
|
|
||||||
|
define("WECHAT_APPID", 'wx5d57f64bb4804d90');
|
||||||
|
define("WECHAT_APPSECRET", '4b1fa6d9442351ec9268eff05e38f521');
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
?>
|
@ -0,0 +1,38 @@
|
|||||||
|
<?php
|
||||||
|
namespace LaneWeChat\Core;
|
||||||
|
|
||||||
|
class AccessToken{
|
||||||
|
public static function getAccessToken(){
|
||||||
|
$accessToken = self::_checkAccessToken();
|
||||||
|
if($accessToken === false){
|
||||||
|
$accessToken = self::_getAccessToken();
|
||||||
|
}
|
||||||
|
return $accessToken['access_token'];
|
||||||
|
}
|
||||||
|
|
||||||
|
private static function _getAccessToken(){
|
||||||
|
$url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.WECHAT_APPID.'&secret='.WECHAT_APPSECRET;
|
||||||
|
$accessToken = Curl::callWebServer($url, '', 'GET');
|
||||||
|
if(!isset($accessToken['access_token'])){
|
||||||
|
return Msg::returnErrMsg(MsgConstant::ERROR_GET_ACCESS_TOKEN, '获取ACCESS_TOKEN失败');
|
||||||
|
}
|
||||||
|
$accessToken['time'] = time();
|
||||||
|
$accessTokenJson = json_encode($accessToken);
|
||||||
|
$f = fopen('access_token', 'w+');
|
||||||
|
fwrite($f, $accessTokenJson);
|
||||||
|
fclose($f);
|
||||||
|
return $accessToken;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static function _checkAccessToken(){
|
||||||
|
$data = file_get_contents('access_token');
|
||||||
|
$accessToken['value'] = $data;
|
||||||
|
if(!empty($accessToken['value'])){
|
||||||
|
$accessToken = json_decode($accessToken['value'], true);
|
||||||
|
if(time() - $accessToken['time'] < $accessToken['expires_in']-10){
|
||||||
|
return $accessToken;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,79 @@
|
|||||||
|
<?php
|
||||||
|
namespace LaneWeChat\Core;
|
||||||
|
|
||||||
|
class AdvancedBroadcast{
|
||||||
|
public static function uploadNews($articles){
|
||||||
|
$queryUrl = 'https://api.weixin.qq.com/cgi-bin/media/uploadnews?access_token='.AccessToken::getAccessToken();
|
||||||
|
$queryAction = 'POST';
|
||||||
|
foreach($articles as &$article){
|
||||||
|
$article['author'] = urlencode($article['author']);
|
||||||
|
$article['title'] = urlencode($article['title']);
|
||||||
|
$article['content'] = urlencode($article['content']);
|
||||||
|
$article['digest'] = urlencode($article['digest']);
|
||||||
|
}
|
||||||
|
$template = array();
|
||||||
|
$template['articles'] = $articles;
|
||||||
|
$template = json_encode($template);
|
||||||
|
$template = urldecode($template);
|
||||||
|
$result = Curl::callWebServer($queryUrl, $template, $queryAction);
|
||||||
|
return empty($result['media_id']) ? false : $result['media_id'];
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function sentNewsByGroup($groupId, $mediaId, $isToAll=false){
|
||||||
|
$queryUrl = 'https://api.weixin.qq.com/cgi-bin/message/mass/sendall?access_token='.AccessToken::getAccessToken();
|
||||||
|
$queryAction = 'POST';
|
||||||
|
$template = array();
|
||||||
|
$template['filter']['group_id'] = $groupId;
|
||||||
|
$template['filter']['is_to_all'] = $isToAll;
|
||||||
|
$template['mpnews']['media_id'] = $mediaId;
|
||||||
|
$template['msgtype'] = 'mpnews';
|
||||||
|
$template = json_encode($template);
|
||||||
|
return Curl::callWebServer($queryUrl, $template, $queryAction);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function sentTextByGroup($groupId, $content, $isToAll=false){
|
||||||
|
$queryUrl = 'https://api.weixin.qq.com/cgi-bin/message/mass/sendall?access_token='.AccessToken::getAccessToken();
|
||||||
|
$queryAction = 'POST';
|
||||||
|
$template = array();
|
||||||
|
$template['filter']['group_id'] = $groupId;
|
||||||
|
$template['filter']['is_to_all'] = $isToAll;
|
||||||
|
$template['text']['content'] = $content;
|
||||||
|
$template['msgtype'] = 'text';
|
||||||
|
$template = json_encode($template);
|
||||||
|
return Curl::callWebServer($queryUrl, $template, $queryAction);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function sentVoiceByGroup($groupId, $mediaId, $isToAll=false){
|
||||||
|
$queryUrl = 'https://api.weixin.qq.com/cgi-bin/message/mass/sendall?access_token='.AccessToken::getAccessToken();
|
||||||
|
$queryAction = 'POST';
|
||||||
|
$template = array();
|
||||||
|
$template['filter']['group_id'] = $groupId;
|
||||||
|
$template['filter']['is_to_all'] = $isToAll;
|
||||||
|
$template['voice']['media_id'] = $mediaId;
|
||||||
|
$template['msgtype'] = 'voice';
|
||||||
|
$template = json_encode($template);
|
||||||
|
return Curl::callWebServer($queryUrl, $template, $queryAction);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function sentImageByGroup($groupId, $mediaId, $isToAll=false){
|
||||||
|
$queryUrl = 'https://api.weixin.qq.com/cgi-bin/message/mass/sendall?access_token='.AccessToken::getAccessToken();
|
||||||
|
$queryAction = 'POST';
|
||||||
|
$template = array();
|
||||||
|
$template['filter']['group_id'] = $groupId;
|
||||||
|
$template['filter']['is_to_all'] = $isToAll;
|
||||||
|
$template['image']['media_id'] = $mediaId;
|
||||||
|
$template['msgtype'] = 'image';
|
||||||
|
$template = json_encode($template);
|
||||||
|
return Curl::callWebServer($queryUrl, $template, $queryAction);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function sentVideoByGroup($mediaId, $title, $description, $groupId, $isToAll=false){
|
||||||
|
$queryUrl = 'https://file.api.weixin.qq.com/cgi-bin/media/uploadvideo?access_token='.AccessToken::getAccessToken();
|
||||||
|
$queryAction = 'POST';
|
||||||
|
$template = array();
|
||||||
|
$template['media_id'] = $mediaId;
|
||||||
|
$template['title'] = $title;
|
||||||
|
$template['description'] = $description;
|
||||||
|
$template = json_encode($template);
|
||||||
|
$result = Curl::callWebServer($queryUrl, $template, $queryAction);
|
||||||
|
if(empty($result['type']) || $result['type'] != 'video' || empty($result['media_id'])){
|
@ -0,0 +1,37 @@
|
|||||||
|
<?php
|
||||||
|
namespace LaneWeChat\Core\Aes;
|
||||||
|
include_once "wxBizMsgCrypt.php";
|
||||||
|
|
||||||
|
$encodingAesKey = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFG";
|
||||||
|
$token = "pamtest";
|
||||||
|
$timeStamp = "1409304348";
|
||||||
|
$nonce = "xxxxxx";
|
||||||
|
$appId = "wxb11529c136998cb6";
|
||||||
|
$text = "<xml><ToUserName><![CDATA[oia2Tj我是中文jewbmiOUlr6X-1crbLOvLw]]></ToUserName><FromUserName><![CDATA[gh_7f083739789a]]></FromUserName><CreateTime>1407743423</CreateTime><MsgType><![CDATA[video]]></MsgType><Video><MediaId><![CDATA[eYJ1MbwPRJtOvIEabaxHs7TX2D-HV71s79GUxqdUkjm6Gs2Ed1KF3ulAOA9H1xG0]]></MediaId><Title><![CDATA[testCallBackReplyVideo]]></Title><Description><![CDATA[testCallBackReplyVideo]]></Description></Video></xml>";
|
||||||
|
|
||||||
|
$pc = new WXBizMsgCrypt($token, $encodingAesKey, $appId);
|
||||||
|
$encryptMsg = '';
|
||||||
|
$errCode = $pc->encryptMsg($text, $timeStamp, $nonce, $encryptMsg);
|
||||||
|
if ($errCode == 0) {
|
||||||
|
print("加密后: " . $encryptMsg . "\n");
|
||||||
|
} else {
|
||||||
|
print($errCode . "\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
$xml_tree = new DOMDocument();
|
||||||
|
$xml_tree->loadXML($encryptMsg);
|
||||||
|
$array_e = $xml_tree->getElementsByTagName('Encrypt');
|
||||||
|
$array_s = $xml_tree->getElementsByTagName('MsgSignature');
|
||||||
|
$encrypt = $array_e->item(0)->nodeValue;
|
||||||
|
$msg_sign = $array_s->item(0)->nodeValue;
|
||||||
|
|
||||||
|
$format = "<xml><ToUserName><![CDATA[toUser]]></ToUserName><Encrypt><![CDATA[%s]]></Encrypt></xml>";
|
||||||
|
$from_xml = sprintf($format, $encrypt);
|
||||||
|
|
||||||
|
$msg = '';
|
||||||
|
$errCode = $pc->decryptMsg($msg_sign, $timeStamp, $nonce, $from_xml, $msg);
|
||||||
|
if ($errCode == 0) {
|
||||||
|
print("解密后: " . $msg . "\n");
|
||||||
|
} else {
|
||||||
|
print($errCode . "\n");
|
||||||
|
}
|
@ -0,0 +1,18 @@
|
|||||||
|
<?php
|
||||||
|
namespace LaneWeChat\Core\Aes;
|
||||||
|
|
||||||
|
class ErrorCode
|
||||||
|
{
|
||||||
|
public static $OK = 0;
|
||||||
|
public static $ValidateSignatureError = -40001;
|
||||||
|
public static $ParseXmlError = -40002;
|
||||||
|
public static $ComputeSignatureError = -40003;
|
||||||
|
public static $IllegalAesKey = -40004;
|
||||||
|
public static $ValidateAppidError = -40005;
|
||||||
|
public static $EncryptAESError = -40006;
|
||||||
|
public static $DecryptAESError = -40007;
|
||||||
|
public static $IllegalBuffer = -40008;
|
||||||
|
public static $EncodeBase64Error = -40009;
|
||||||
|
public static $DecodeBase64Error = -40010;
|
||||||
|
public static $GenReturnXmlError = -40011;
|
||||||
|
}
|
@ -0,0 +1,32 @@
|
|||||||
|
<?php
|
||||||
|
namespace LaneWeChat\Core\Aes;
|
||||||
|
|
||||||
|
class PKCS7Encoder
|
||||||
|
{
|
||||||
|
public static $block_size = 32;
|
||||||
|
|
||||||
|
function encode($text)
|
||||||
|
{
|
||||||
|
$block_size = PKCS7Encoder::$block_size;
|
||||||
|
$text_length = strlen($text);
|
||||||
|
$amount_to_pad = PKCS7Encoder::$block_size - ($text_length % PKCS7Encoder::$block_size);
|
||||||
|
if ($amount_to_pad == 0) {
|
||||||
|
$amount_to_pad = PKCS7Encoder::block_size;
|
||||||
|
}
|
||||||
|
$pad_chr = chr($amount_to_pad);
|
||||||
|
$tmp = "";
|
||||||
|
for ($index = 0; $index < $amount_to_pad; $index++) {
|
||||||
|
$tmp .= $pad_chr;
|
||||||
|
}
|
||||||
|
return $text . $tmp;
|
||||||
|
}
|
||||||
|
|
||||||
|
function decode($text)
|
||||||
|
{
|
||||||
|
$pad = ord(substr($text, -1));
|
||||||
|
if ($pad < 1 || $pad > 32) {
|
||||||
|
$pad = 0;
|
||||||
|
}
|
||||||
|
return substr($text, 0, (strlen($text) - $pad));
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,74 @@
|
|||||||
|
<?php
|
||||||
|
namespace LaneWeChat\Core\Aes;
|
||||||
|
|
||||||
|
class Prpcrypt
|
||||||
|
{
|
||||||
|
public $key;
|
||||||
|
|
||||||
|
function Prpcrypt($k)
|
||||||
|
{
|
||||||
|
$this->key = base64_decode($k . "=");
|
||||||
|
}
|
||||||
|
|
||||||
|
public function encrypt($text, $appid)
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
$random = $this->getRandomStr();
|
||||||
|
$text = $random . pack("N", strlen($text)) . $text . $appid;
|
||||||
|
$size = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
|
||||||
|
$module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
|
||||||
|
$iv = substr($this->key, 0, 16);
|
||||||
|
$pkc_encoder = new PKCS7Encoder;
|
||||||
|
$text = $pkc_encoder->encode($text);
|
||||||
|
mcrypt_generic_init($module, $this->key, $iv);
|
||||||
|
$encrypted = mcrypt_generic($module, $text);
|
||||||
|
mcrypt_generic_deinit($module);
|
||||||
|
mcrypt_module_close($module);
|
||||||
|
return array(ErrorCode::$OK, base64_encode($encrypted));
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
return array(ErrorCode::$EncryptAESError, null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function decrypt($encrypted, $appid)
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
$ciphertext_dec = base64_decode($encrypted);
|
||||||
|
$module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
|
||||||
|
$iv = substr($this->key, 0, 16);
|
||||||
|
mcrypt_generic_init($module, $this->key, $iv);
|
||||||
|
$decrypted = mdecrypt_generic($module, $ciphertext_dec);
|
||||||
|
mcrypt_generic_deinit($module);
|
||||||
|
mcrypt_module_close($module);
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
return array(ErrorCode::$DecryptAESError, null);
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
$pkc_encoder = new PKCS7Encoder;
|
||||||
|
$result = $pkc_encoder->decode($decrypted);
|
||||||
|
if (strlen($result) < 16)
|
||||||
|
return "";
|
||||||
|
$content = substr($result, 16, strlen($result));
|
||||||
|
$len_list = unpack("N", substr($content, 0, 4));
|
||||||
|
$xml_len = $len_list[1];
|
||||||
|
$xml_content = substr($content, 4, $xml_len);
|
||||||
|
$from_appid = substr($content, $xml_len + 4);
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
return array(ErrorCode::$IllegalBuffer, null);
|
||||||
|
}
|
||||||
|
if ($from_appid != $appid)
|
||||||
|
return array(ErrorCode::$ValidateAppidError, null);
|
||||||
|
return array(0, $xml_content);
|
||||||
|
}
|
||||||
|
|
||||||
|
function getRandomStr()
|
||||||
|
{
|
||||||
|
$str = "";
|
||||||
|
$str_pol = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";
|
||||||
|
$max = strlen($str_pol) - 1;
|
||||||
|
for ($i = 0; $i < 16; $i++) {
|
||||||
|
$str .= $str_pol[mt_rand(0, $max)];
|
||||||
|
}
|
||||||
|
return $str;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,17 @@
|
|||||||
|
<?php
|
||||||
|
namespace LaneWeChat\Core\Aes;
|
||||||
|
|
||||||
|
class SHA1
|
||||||
|
{
|
||||||
|
public function getSHA1($token, $timestamp, $nonce, $encrypt_msg)
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
$array = array($encrypt_msg, $token, $timestamp, $nonce);
|
||||||
|
sort($array, SORT_STRING);
|
||||||
|
$str = implode($array);
|
||||||
|
return array(ErrorCode::$OK, sha1($str));
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
return array(ErrorCode::$ComputeSignatureError, null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,24 @@
|
|||||||
|
<?php
|
||||||
|
namespace LaneWeChat\Core\Aes;
|
||||||
|
|
||||||
|
class WXBizMsgCrypt
|
||||||
|
{
|
||||||
|
private $token;
|
||||||
|
private $encodingAesKey;
|
||||||
|
private $appId;
|
||||||
|
|
||||||
|
public function __construct($token, $encodingAesKey, $appId)
|
||||||
|
{
|
||||||
|
$this->token = $token;
|
||||||
|
$this->encodingAesKey = $encodingAesKey;
|
||||||
|
$this->appId = $appId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function encryptMsg($replyMsg, $timeStamp, $nonce, &$encryptMsg)
|
||||||
|
{
|
||||||
|
$pc = new Prpcrypt($this->encodingAesKey);
|
||||||
|
$array = $pc->encrypt($replyMsg, $this->appId);
|
||||||
|
$ret = $array[0];
|
||||||
|
if ($ret != 0) {
|
||||||
|
return $ret;
|
||||||
|
|
@ -0,0 +1,32 @@
|
|||||||
|
<?php
|
||||||
|
namespace LaneWeChat\Core\Aes;
|
||||||
|
|
||||||
|
class XMLParse
|
||||||
|
{
|
||||||
|
public function extract($xmltext)
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
$xml = new \DOMDocument();
|
||||||
|
$xml->loadXML($xmltext);
|
||||||
|
$array_e = $xml->getElementsByTagName('Encrypt');
|
||||||
|
$array_a = $xml->getElementsByTagName('ToUserName');
|
||||||
|
$encrypt = $array_e->item(0)->nodeValue;
|
||||||
|
$tousername = $array_a->item(0)->nodeValue;
|
||||||
|
return array(0, $encrypt, $tousername);
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
return array(ErrorCode::$ParseXmlError, null, null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function generate($encrypt, $signature, $timestamp, $nonce)
|
||||||
|
{
|
||||||
|
$format = "<xml>
|
||||||
|
<Encrypt><![CDATA[%s]]></Encrypt>
|
||||||
|
<MsgSignature><![CDATA[%s]]></MsgSignature>
|
||||||
|
<TimeStamp>%s</TimeStamp>
|
||||||
|
<Nonce><![CDATA[%s]]></Nonce>
|
||||||
|
</xml>";
|
||||||
|
return sprintf($format, $encrypt, $signature, $timestamp, $nonce);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
?>
|
@ -0,0 +1,21 @@
|
|||||||
|
<?php
|
||||||
|
namespace LaneWeChat\Core;
|
||||||
|
/**
|
||||||
|
* Created by lixuan-it@360.cn
|
||||||
|
* User: lane
|
||||||
|
* Date: 15/4/29
|
||||||
|
* Time: 上午10:51
|
||||||
|
* E-mail: lixuan868686@163.com
|
||||||
|
* WebSite: http://www.lanecn.com
|
||||||
|
*/
|
||||||
|
class Auth {
|
||||||
|
/**
|
||||||
|
* 获取微信服务器IP列表
|
||||||
|
*/
|
||||||
|
public static function getWeChatIPList(){
|
||||||
|
//获取ACCESS_TOKEN
|
||||||
|
$accessToken = AccessToken::getAccessToken();
|
||||||
|
$url = 'https://api.weixin.qq.com/cgi-bin/getcallbackip?access_token='.$accessToken;
|
||||||
|
return Curl::callWebServer($url, '', 'GET');
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,17 @@
|
|||||||
|
<?php
|
||||||
|
namespace LaneWeChat\Core;
|
||||||
|
|
||||||
|
class Media {
|
||||||
|
public static function upload($filename, $type) {
|
||||||
|
$accessToken = AccessToken::getAccessToken();
|
||||||
|
$queryUrl = 'http://file.api.weixin.qq.com/cgi-bin/media/upload?access_token=' . $accessToken . '&type=' . $type;
|
||||||
|
$data = array('media' => '@' . $filename);
|
||||||
|
return Curl::callWebServer($queryUrl, $data, 'POST', 1, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function download($mediaId) {
|
||||||
|
$accessToken = AccessToken::getAccessToken();
|
||||||
|
$queryUrl = 'http://file.api.weixin.qq.com/cgi-bin/media/get?access_token=' . $accessToken . '&media_id=' . $mediaId;
|
||||||
|
return Curl::callWebServer($queryUrl, '', 'GET', 0);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,62 @@
|
|||||||
|
<?php
|
||||||
|
namespace LaneWeChat\Core;
|
||||||
|
|
||||||
|
class Menu{
|
||||||
|
public static function setMenu($menuList){
|
||||||
|
$menuList2 = $menuList;
|
||||||
|
foreach($menuList as $key=>$menu){
|
||||||
|
foreach($menuList2 as $k=>$menu2){
|
||||||
|
if($menu['id'] == $menu2['pid']){
|
||||||
|
$menuList[$key]['sub_button'][] = $menu2;
|
||||||
|
unset($menuList[$k]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
foreach($menuList as $key=>$menu){
|
||||||
|
if($menu['type'] == 'view'){
|
||||||
|
$menuList[$key]['url'] = urlencode($menu['code']);
|
||||||
|
}else if($menu['type'] == 'click'){
|
||||||
|
$menuList[$key]['key'] = $menu['code'];
|
||||||
|
}else if(!empty($menu['type'])){
|
||||||
|
$menuList[$key]['key'] = $menu['code'];
|
||||||
|
if(!isset($menu['sub_button'])) $menuList[$key]['sub_button'] = array();
|
||||||
|
}
|
||||||
|
unset($menuList[$key]['code'], $menuList[$key]['id'], $menuList[$key]['pid']);
|
||||||
|
$menuList[$key]['name'] = urlencode($menu['name']);
|
||||||
|
if(isset($menu['sub_button'])){
|
||||||
|
unset($menuList[$key]['type']);
|
||||||
|
foreach($menu['sub_button'] as $k=>$son){
|
||||||
|
if($son['type'] == 'view'){
|
||||||
|
$menuList[$key]['sub_button'][$k]['url'] = urlencode($son['code']);
|
||||||
|
}else if($son['type'] == 'click'){
|
||||||
|
$menuList[$key]['sub_button'][$k]['key'] = $son['code'];
|
||||||
|
}else{
|
||||||
|
$menuList[$key]['sub_button'][$k]['key'] = $son['code'];
|
||||||
|
$menuList[$key]['sub_button'][$k]['sub_button'] = array();
|
||||||
|
}
|
||||||
|
unset($menuList[$key]['sub_button'][$k]['code'], $menuList[$key]['sub_button'][$k]['id'], $menuList[$key]['sub_button'][$k]['pid']);
|
||||||
|
$menuList[$key]['sub_button'][$k]['name'] = urlencode($son['name']);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$data = array('button' => array_values($menuList));
|
||||||
|
$data = json_encode($data);
|
||||||
|
$data = urldecode($data);
|
||||||
|
$accessToken = AccessToken::getAccessToken();
|
||||||
|
$url = 'https://api.weixin.qq.com/cgi-bin/menu/create?access_token='.$accessToken;
|
||||||
|
$result = Curl::callWebServer($url, $data, 'POST');
|
||||||
|
return $result['errcode'] == 0 ? true : $result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function getMenu(){
|
||||||
|
$accessToken = AccessToken::getAccessToken();
|
||||||
|
$url = 'https://api.weixin.qq.com/cgi-bin/menu/get?access_token='.$accessToken;
|
||||||
|
return Curl::callWebServer($url, '', 'GET');
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function delMenu(){
|
||||||
|
$accessToken = AccessToken::getAccessToken();
|
||||||
|
$url = 'https://api.weixin.qq.com/cgi-bin/menu/delete?access_token='.$accessToken;
|
||||||
|
return Curl::callWebServer($url, '', 'GET');
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,19 @@
|
|||||||
|
<?php
|
||||||
|
namespace LaneWeChat\Core;
|
||||||
|
|
||||||
|
class Msg {
|
||||||
|
|
||||||
|
@param int
|
||||||
|
@param string
|
||||||
|
@return Ambigous
|
||||||
|
|
||||||
|
public static function returnErrMsg($code, $errorMsg = null) {
|
||||||
|
$returnMsg = array('error_code' => $code);
|
||||||
|
if (!empty($errorMsg)) {
|
||||||
|
$returnMsg['custom_msg'] = $errorMsg;
|
||||||
|
}
|
||||||
|
$returnMsg['custom_msg'] = '出错啦!'.$returnMsg['custom_msg'];
|
||||||
|
exit($returnMsg['custom_msg']);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
?>
|
@ -0,0 +1,20 @@
|
|||||||
|
<?php
|
||||||
|
namespace LaneWeChat\Core;
|
||||||
|
|
||||||
|
class MsgConstant {
|
||||||
|
const ERROR_SYSTEM = 101;
|
||||||
|
const ERROR_NEWS_ITEM_COUNT_MORE_TEN = 102;
|
||||||
|
const ERROR_MENU_CLICK = 103;
|
||||||
|
|
||||||
|
const ERROR_INPUT_ERROR = 1001;
|
||||||
|
const ERROR_UNKNOW_TYPE = 1002;
|
||||||
|
const ERROR_CAPTCHA_ERROR = 1003;
|
||||||
|
const ERROR_REQUIRED_FIELDS = 1004;
|
||||||
|
|
||||||
|
const ERROR_REMOTE_SERVER_NOT_RESPOND = 1201;
|
||||||
|
const ERROR_GET_ACCESS_TOKEN = 1202;
|
||||||
|
|
||||||
|
const ERROR_MENU_NOT_EXISTS = 1401;
|
||||||
|
|
||||||
|
const ERROR_NO_BINDING_TEXT = '对不起,您尚未绑定微信,轻松绑定微信,即可查询实时流量,享受便捷服务!';
|
||||||
|
}
|
@ -0,0 +1,37 @@
|
|||||||
|
<?php
|
||||||
|
namespace LaneWeChat\Core;
|
||||||
|
class Popularize{
|
||||||
|
public static function createTicket($type, $expireSeconds, $sceneId){
|
||||||
|
$queryUrl = 'https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token='.AccessToken::getAccessToken();
|
||||||
|
$queryAction = 'POST';
|
||||||
|
$template = array();
|
||||||
|
if($type == 1){
|
||||||
|
$template['expire_seconds'] = $expireSeconds;
|
||||||
|
$template['action_name'] = 'QR_SCENE';
|
||||||
|
}else{
|
||||||
|
$template['action_name'] = 'QR_LIMIT_SCENE';
|
||||||
|
}
|
||||||
|
$template['action_info']['scene']['scene_id'] = $sceneId;
|
||||||
|
$template = json_encode($template);
|
||||||
|
return Curl::callWebServer($queryUrl, $template, $queryAction);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function getQrcode($ticket, $filename=''){
|
||||||
|
$queryUrl = 'https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket='.urlencode($ticket);
|
||||||
|
$queryAction = 'GET';
|
||||||
|
$result = Curl::callWebServer($queryUrl, '', $queryAction, 0);
|
||||||
|
if(!empty($filename)){
|
||||||
|
file_put_contents($filename, $result);
|
||||||
|
}
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function long2short($longUrl){
|
||||||
|
$queryUrl = 'https://api.weixin.qq.com/cgi-bin/shorturl?access_token='.AccessToken::getAccessToken();
|
||||||
|
$queryAction = 'POST';
|
||||||
|
$template = array();
|
||||||
|
$template['long_url'] = $longUrl;
|
||||||
|
$template['action'] = 'long2short';
|
||||||
|
return Curl::callWebServer($queryUrl, '', $queryAction);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,128 @@
|
|||||||
|
<?php
|
||||||
|
namespace LaneWeChat\Core;
|
||||||
|
|
||||||
|
class ResponseInitiative{
|
||||||
|
|
||||||
|
protected static $queryUrl = 'https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=';
|
||||||
|
|
||||||
|
protected static $action = 'POST';
|
||||||
|
public static function text($tousername, $content){
|
||||||
|
|
||||||
|
$accessToken = AccessToken::getAccessToken();
|
||||||
|
self::$queryUrl = self::$queryUrl.$accessToken;
|
||||||
|
|
||||||
|
$template = array(
|
||||||
|
'touser'=>$tousername,
|
||||||
|
'msgtype'=>'text',
|
||||||
|
'text'=>array(
|
||||||
|
'content'=>$content,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
$template = json_encode($template);
|
||||||
|
|
||||||
|
return Curl::callWebServer(self::$queryUrl, $template, self::$action);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function image($tousername, $mediaId){
|
||||||
|
//获取ACCESS_TOKEN
|
||||||
|
$accessToken = AccessToken::getAccessToken();
|
||||||
|
self::$queryUrl = self::$queryUrl.$accessToken;
|
||||||
|
|
||||||
|
//开始
|
||||||
|
$template = array(
|
||||||
|
'touser'=>$tousername,
|
||||||
|
'msgtype'=>'image',
|
||||||
|
'image'=>array(
|
||||||
|
'media_id'=>$mediaId,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
$template = json_encode($template);
|
||||||
|
return Curl::callWebServer(self::$queryUrl, $template, self::$action);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function voice($tousername, $mediaId){
|
||||||
|
//获取ACCESS_TOKEN
|
||||||
|
$accessToken = AccessToken::getAccessToken();
|
||||||
|
self::$queryUrl = self::$queryUrl.$accessToken;
|
||||||
|
|
||||||
|
//开始
|
||||||
|
$template = array(
|
||||||
|
'touser'=>$tousername,
|
||||||
|
'msgtype'=>'voice',
|
||||||
|
'voice'=>array(
|
||||||
|
'media_id'=>$mediaId,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
$template = json_encode($template);
|
||||||
|
return Curl::callWebServer(self::$queryUrl, $template, self::$action);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function video($tousername, $mediaId, $title, $description){
|
||||||
|
//获取ACCESS_TOKEN
|
||||||
|
$accessToken = AccessToken::getAccessToken();
|
||||||
|
self::$queryUrl = self::$queryUrl.$accessToken;
|
||||||
|
|
||||||
|
//开始
|
||||||
|
$template = array(
|
||||||
|
'touser'=>$tousername,
|
||||||
|
'msgtype'=>'video',
|
||||||
|
'video'=>array(
|
||||||
|
'media_id'=>$mediaId,
|
||||||
|
'title'=>$title,
|
||||||
|
'description'=>$description,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
$template = json_encode($template);
|
||||||
|
return Curl::callWebServer(self::$queryUrl, $template, self::$action);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static function music($tousername, $title, $description, $musicUrl, $hqMusicUrl, $thumbMediaId){
|
||||||
|
//获取ACCESS_TOKEN
|
||||||
|
$accessToken = AccessToken::getAccessToken();
|
||||||
|
self::$queryUrl = self::$queryUrl.$accessToken;
|
||||||
|
|
||||||
|
//开始
|
||||||
|
$template = array(
|
||||||
|
'touser'=>$tousername,
|
||||||
|
'msgtype'=>'music',
|
||||||
|
'music'=>array(
|
||||||
|
'title'=>$title,
|
||||||
|
'description'=>$description,
|
||||||
|
'musicurl'=>$musicUrl,
|
||||||
|
'hqmusicurl'=>$hqMusicUrl,
|
||||||
|
'thumb_media_id'=>$thumbMediaId,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
$template = json_encode($template);
|
||||||
|
return Curl::callWebServer(self::$queryUrl, $template, self::$action);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function newsItem($title, $description, $picUrl, $url){
|
||||||
|
return $template = array(
|
||||||
|
'title'=>$title,
|
||||||
|
'description'=>$description,
|
||||||
|
'url'=>$picUrl,
|
||||||
|
'picurl'=>$url,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function news($tousername, $item){
|
||||||
|
|
||||||
|
$accessToken = AccessToken::getAccessToken();
|
||||||
|
self::$queryUrl = self::$queryUrl.$accessToken;
|
||||||
|
|
||||||
|
|
||||||
|
$template = array(
|
||||||
|
'touser'=>$tousername,
|
||||||
|
'msgtype'=>'news',
|
||||||
|
'news'=>array(
|
||||||
|
'articles'=>$item
|
||||||
|
),
|
||||||
|
);
|
||||||
|
$template = json_encode($template);
|
||||||
|
return Curl::callWebServer(self::$queryUrl, $template, self::$action);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,143 @@
|
|||||||
|
<?php
|
||||||
|
namespace LaneWeChat\Core;
|
||||||
|
|
||||||
|
class ResponsePassive{
|
||||||
|
|
||||||
|
public static function text($fromusername, $tousername, $content, $funcFlag=0){
|
||||||
|
$template = <<<XML
|
||||||
|
<xml>
|
||||||
|
<ToUserName><![CDATA[%s]]></ToUserName>
|
||||||
|
<FromUserName><![CDATA[%s]]></FromUserName>
|
||||||
|
<CreateTime>%s</CreateTime>
|
||||||
|
<MsgType><![CDATA[text]]></MsgType>
|
||||||
|
<Content><![CDATA[%s]]></Content>
|
||||||
|
<FuncFlag>%s</FuncFlag>
|
||||||
|
</xml>
|
||||||
|
XML;
|
||||||
|
return sprintf($template, $fromusername, $tousername, time(), $content, $funcFlag);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static function image($fromusername, $tousername, $mediaId, $funcFlag=0){
|
||||||
|
$template = <<<XML
|
||||||
|
<xml>
|
||||||
|
<ToUserName><![CDATA[%s]]></ToUserName>
|
||||||
|
<FromUserName><![CDATA[%s]]></FromUserName>
|
||||||
|
<CreateTime>%s</CreateTime>
|
||||||
|
<MsgType><![CDATA[image]]></MsgType>
|
||||||
|
<Image>
|
||||||
|
<MediaId><![CDATA[%s]]></MediaId>
|
||||||
|
</Image>
|
||||||
|
<FuncFlag>%s</FuncFlag>
|
||||||
|
</xml>
|
||||||
|
XML;
|
||||||
|
return sprintf($template, $fromusername, $tousername, time(), $mediaId, $funcFlag);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static function voice($fromusername, $tousername, $mediaId, $funcFlag=0){
|
||||||
|
$template = <<<XML
|
||||||
|
<xml>
|
||||||
|
<ToUserName><![CDATA[%s]]></ToUserName>
|
||||||
|
<FromUserName><![CDATA[%s]]></FromUserName>
|
||||||
|
<CreateTime>%s</CreateTime>
|
||||||
|
<MsgType><![CDATA[voice]]></MsgType>
|
||||||
|
<Voice>
|
||||||
|
<MediaId><![CDATA[%s]]></MediaId>
|
||||||
|
</Voice>
|
||||||
|
<FuncFlag>%s</FuncFlag>
|
||||||
|
</xml>
|
||||||
|
XML;
|
||||||
|
return sprintf($template, $fromusername, $tousername, time(), $mediaId, $funcFlag);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static function video($fromusername, $tousername, $mediaId, $title, $description, $funcFlag=0){
|
||||||
|
$template = <<<XML
|
||||||
|
<xml>
|
||||||
|
<ToUserName><![CDATA[%s]]></ToUserName>
|
||||||
|
<FromUserName><![CDATA[%s]]></FromUserName>
|
||||||
|
<CreateTime>%s</CreateTime>
|
||||||
|
<MsgType><![CDATA[video]]></MsgType>
|
||||||
|
<Video>
|
||||||
|
<MediaId><![CDATA[%s]]></MediaId>
|
||||||
|
<Title><![CDATA[%s]]></Title>
|
||||||
|
<Description><![CDATA[%s]]></Description>
|
||||||
|
</Video>
|
||||||
|
<FuncFlag>%s</FuncFlag>
|
||||||
|
</xml>
|
||||||
|
XML;
|
||||||
|
return sprintf($template, $fromusername, $tousername, time(), $mediaId, $title, $description, $funcFlag);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static function music($fromusername, $tousername, $title, $description, $musicUrl, $hqMusicUrl, $thumbMediaId, $funcFlag=0){
|
||||||
|
$template = <<<XML
|
||||||
|
<xml>
|
||||||
|
<ToUserName><![CDATA[%s]]></ToUserName>
|
||||||
|
<FromUserName><![CDATA[%s]]></FromUserName>
|
||||||
|
<CreateTime>%s</CreateTime>
|
||||||
|
<MsgType><![CDATA[music]]></MsgType>
|
||||||
|
<Music>
|
||||||
|
<Title><![CDATA[%s]]></Title>
|
||||||
|
<Description><![CDATA[%s]]></Description>
|
||||||
|
<MusicUrl><![CDATA[%s]]></MusicUrl>
|
||||||
|
<HQMusicUrl><![CDATA[%s]]></HQMusicUrl>
|
||||||
|
<ThumbMediaId><![CDATA[%s]]></ThumbMediaId>
|
||||||
|
</Music>
|
||||||
|
<FuncFlag>%s</FuncFlag>
|
||||||
|
</xml>
|
||||||
|
XML;
|
||||||
|
return sprintf($template, $fromusername, $tousername, time(), $title, $description, $musicUrl, $hqMusicUrl, $thumbMediaId, $funcFlag);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static function newsItem($title, $description, $picUrl, $url){
|
||||||
|
$template = <<<XML
|
||||||
|
<item>
|
||||||
|
<Title><![CDATA[%s]]></Title>
|
||||||
|
<Description><![CDATA[%s]]></Description>
|
||||||
|
<PicUrl><![CDATA[%s]]></PicUrl>
|
||||||
|
<Url><![CDATA[%s]]></Url>
|
||||||
|
</item>
|
||||||
|
XML;
|
||||||
|
return sprintf($template, $title, $description, $picUrl, $url);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static function news($fromusername, $tousername, $item, $funcFlag=0){
|
||||||
|
|
||||||
|
if(count($item) >= 10){
|
||||||
|
$request = array('fromusername'=>$fromusername, 'tousername'=>$tousername);
|
||||||
|
return Msg::returnErrMsg(MsgConstant::ERROR_NEWS_ITEM_COUNT_MORE_TEN, '图文消息的项数不能超过10条', $request);
|
||||||
|
|
||||||
|
}
|
||||||
|
$template = <<<XML
|
||||||
|
<xml>
|
||||||
|
<ToUserName><![CDATA[%s]]></ToUserName>
|
||||||
|
<FromUserName><![CDATA[%s]]></FromUserName>
|
||||||
|
<CreateTime>%s</CreateTime>
|
||||||
|
<MsgType><![CDATA[news]]></MsgType>
|
||||||
|
<ArticleCount>%s</ArticleCount>
|
||||||
|
<Articles>
|
||||||
|
%s
|
||||||
|
</Articles>
|
||||||
|
<FuncFlag>%s</FuncFlag>
|
||||||
|
</xml>
|
||||||
|
XML;
|
||||||
|
return sprintf($template, $fromusername, $tousername, time(), count($item), implode($item), $funcFlag);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static function forwardToCustomService($fromusername, $tousername){
|
||||||
|
$template = <<<XML
|
||||||
|
<xml>
|
||||||
|
<ToUserName><![CDATA[%s]]></ToUserName>
|
||||||
|
<FromUserName><![CDATA[%s]]></FromUserName>
|
||||||
|
<CreateTime>%s</CreateTime>
|
||||||
|
<MsgType><![CDATA[transfer_customer_service]]></MsgType>
|
||||||
|
</xml>
|
||||||
|
XML;
|
||||||
|
return sprintf($template, $fromusername, $tousername, time());
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,63 @@
|
|||||||
|
<?php
|
||||||
|
namespace LaneWeChat\Core;
|
||||||
|
class Wechat{
|
||||||
|
private $debug;
|
||||||
|
private $request;
|
||||||
|
|
||||||
|
public function __construct($token, $debug = FALSE) {
|
||||||
|
if ($this->isValid() && $this->validateSignature($token)) {
|
||||||
|
return $_GET['echostr'];
|
||||||
|
}
|
||||||
|
$this->debug = $debug;
|
||||||
|
$xml = (array) simplexml_load_string($GLOBALS['HTTP_RAW_POST_DATA'], 'SimpleXMLElement', LIBXML_NOCDATA);
|
||||||
|
$this->request = array_change_key_case($xml, CASE_LOWER);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function isValid() {
|
||||||
|
return isset($_GET['echostr']);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function validateSignature($token) {
|
||||||
|
$signature = $_GET['signature'];
|
||||||
|
$timestamp = $_GET['timestamp'];
|
||||||
|
$nonce = $_GET['nonce'];
|
||||||
|
$signatureArray = array($token, $timestamp, $nonce);
|
||||||
|
sort($signatureArray, SORT_STRING);
|
||||||
|
return sha1(implode($signatureArray)) == $signature;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function getRequest($param = FALSE) {
|
||||||
|
if ($param === FALSE) {
|
||||||
|
return $this->request;
|
||||||
|
}
|
||||||
|
$param = strtolower($param);
|
||||||
|
if (isset($this->request[$param])) {
|
||||||
|
return $this->request[$param];
|
||||||
|
}
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function run() {
|
||||||
|
return WechatRequest::switchType($this->request);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function checkSignature()
|
||||||
|
{
|
||||||
|
$signature = $_GET["signature"];
|
||||||
|
$timestamp = $_GET["timestamp"];
|
||||||
|
$nonce = $_GET["nonce"];
|
||||||
|
|
||||||
|
$token = WECHAT_TOKEN;
|
||||||
|
$tmpArr = array($token, $timestamp, $nonce);
|
||||||
|
sort($tmpArr, SORT_STRING);
|
||||||
|
$tmpStr = implode( $tmpArr );
|
||||||
|
$tmpStr = sha1( $tmpStr );
|
||||||
|
|
||||||
|
if( $tmpStr == $signature ){
|
||||||
|
echo $_GET['echostr'];
|
||||||
|
return true;
|
||||||
|
}else{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,47 @@
|
|||||||
|
<?php
|
||||||
|
namespace LaneWeChat\Core;
|
||||||
|
class WeChatOAuth{
|
||||||
|
public static function getCode($redirect_uri, $state=1, $scope='snsapi_base'){
|
||||||
|
if($redirect_uri[0] == '/'){
|
||||||
|
$redirect_uri = substr($redirect_uri, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
$appid = WECHAT_APPID;
|
||||||
|
|
||||||
|
$redirect_uri = WECHAT_URL . $redirect_uri;
|
||||||
|
$redirect_uri = urlencode($redirect_uri);
|
||||||
|
|
||||||
|
$response_type = 'code';
|
||||||
|
|
||||||
|
$url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid='.$appid.'&redirect_uri='.$redirect_uri.'&response_type='.$response_type.'&scope='.$scope.'&state='.$state.'#wechat_redirect';
|
||||||
|
header('Location: '.$url, true, 301);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static function getAccessTokenAndOpenId($code){
|
||||||
|
|
||||||
|
$grant_type = 'authorization_code';
|
||||||
|
|
||||||
|
$url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.WECHAT_APPID.'&secret='.WECHAT_APPSECRET.'&code='.$code.'&grant_type='.$grant_type.'';
|
||||||
|
|
||||||
|
return Curl::callWebServer($url);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function refreshToken($refreshToken){
|
||||||
|
$queryUrl = 'https://api.weixin.qq.com/sns/oauth2/refresh_token?appid='.WECHAT_APPID.'&grant_type=refresh_token&refresh_token='.$refreshToken;
|
||||||
|
$queryAction = 'GET';
|
||||||
|
return Curl::callWebServer($queryUrl, '', $queryAction);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function getUserInfo($accessToken, $openId, $lang='zh_CN'){
|
||||||
|
$queryUrl = 'https://api.weixin.qq.com/sns/userinfo?access_token='. $accessToken . '&openid='. $openId .'&lang=zh_CN';
|
||||||
|
$queryAction = 'GET';
|
||||||
|
return Curl::callWebServer($queryUrl, '', $queryAction);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function checkAccessToken($accessToken, $openId){
|
||||||
|
$queryUrl = 'https://api.weixin.qq.com/sns/auth?access_token='.$accessToken.'&openid='.$openId;
|
||||||
|
$queryAction = 'GET';
|
||||||
|
return Curl::callWebServer($queryUrl, '', $queryAction);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,10 @@
|
|||||||
|
<?php
|
||||||
|
namespace LaneWeChat;
|
||||||
|
|
||||||
|
session_start();
|
||||||
|
|
||||||
|
include_once __DIR__.'/config.php';
|
||||||
|
|
||||||
|
include_once __DIR__.'/autoloader.php';
|
||||||
|
|
||||||
|
AutoLoader::register();
|
@ -0,0 +1,20 @@
|
|||||||
|
<?php
|
||||||
|
namespace LaneWeChat;
|
||||||
|
|
||||||
|
use LaneWeChat\Core\Wechat;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
include_once __DIR__.'/config.php';
|
||||||
|
|
||||||
|
include_once __DIR__.'/autoloader.php';
|
||||||
|
|
||||||
|
AutoLoader::register();
|
||||||
|
|
||||||
|
$wechat = new WeChat(WECHAT_TOKEN, TRUE);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
echo $wechat->run();
|
||||||
|
|
Loading…
Reference in new issue