1、已知bug修复

2、微信接口更新
This commit is contained in:
2017-08-26 17:57:10 +08:00
parent cfcd6250ff
commit 356cccd1d4
25 changed files with 1353 additions and 695 deletions

View File

@@ -1,5 +1,18 @@
<?php
// +----------------------------------------------------------------------
// | wechat-php-sdk
// +----------------------------------------------------------------------
// | 版权所有 2014~2017 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
// +----------------------------------------------------------------------
// | 官方文档: https://www.kancloud.cn/zoujingli/wechat-php-sdk
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// +----------------------------------------------------------------------
// | github开源项目https://github.com/zoujingli/wechat-php-sdk
// +----------------------------------------------------------------------
namespace Wechat\Lib;
use Wechat\Loader;
@@ -10,7 +23,8 @@ use Wechat\Loader;
* @author Anyon <zoujingli@qq.com>
* @date 2016-08-20 17:50
*/
class Cache {
class Cache
{
/**
* 缓存位置
@@ -25,7 +39,8 @@ class Cache {
* @param int $expired
* @return mixed
*/
static public function set($name, $value, $expired = 0) {
static public function set($name, $value, $expired = 0)
{
if (isset(Loader::$callback['CacheSet'])) {
return call_user_func_array(Loader::$callback['CacheSet'], func_get_args());
}
@@ -38,7 +53,8 @@ class Cache {
* @param string $name
* @return mixed
*/
static public function get($name) {
static public function get($name)
{
if (isset(Loader::$callback['CacheGet'])) {
return call_user_func_array(Loader::$callback['CacheGet'], func_get_args());
}
@@ -56,7 +72,8 @@ class Cache {
* @param string $name
* @return mixed
*/
static public function del($name) {
static public function del($name)
{
if (isset(Loader::$callback['CacheDel'])) {
return call_user_func_array(Loader::$callback['CacheDel'], func_get_args());
}
@@ -69,7 +86,8 @@ class Cache {
* @param string $filename
* @return mixed
*/
static public function put($line, $filename = '') {
static public function put($line, $filename = '')
{
if (isset(Loader::$callback['CachePut'])) {
return call_user_func_array(Loader::$callback['CachePut'], func_get_args());
}
@@ -81,13 +99,48 @@ class Cache {
* 检查缓存目录
* @return bool
*/
static protected function check() {
static protected function check()
{
empty(self::$cachepath) && self::$cachepath = dirname(__DIR__) . DIRECTORY_SEPARATOR . 'Cache' . DIRECTORY_SEPARATOR;
self::$cachepath = rtrim(self::$cachepath, '/\\') . DIRECTORY_SEPARATOR;
if (!is_dir(self::$cachepath) && !mkdir(self::$cachepath, 0755, TRUE)) {
return FALSE;
if (!is_dir(self::$cachepath) && !mkdir(self::$cachepath, 0755, true)) {
return false;
}
return TRUE;
return true;
}
/**
* 文件缓存,成功返回文件路径
* @param string $content 文件内容
* @param string $filename 文件名称
* @return bool|string
*/
static public function file($content, $filename = '')
{
if (isset(Loader::$callback['CacheFile'])) {
return call_user_func_array(Loader::$callback['CacheFile'], func_get_args());
}
empty($filename) && $filename = md5($content) . '.' . self::getFileExt($content);
if (self::check() && file_put_contents(self::$cachepath . $filename, $content)) {
return self::$cachepath . $filename;
}
return false;
}
/**
* 根据文件流读取文件后缀
* @param string $content
* @return string
*/
static public function getFileExt($content)
{
$types = [
255216 => 'jpg', 7173 => 'gif', 6677 => 'bmp', 13780 => 'png',
7368 => 'mp3', 4838 => 'wma', 7784 => 'mid', 6063 => 'xml',
];
$typeInfo = @unpack("C2chars", substr($content, 0, 2));
$typeCode = intval($typeInfo['chars1'] . $typeInfo['chars2']);
return isset($types[$typeCode]) ? $types[$typeCode] : 'mp4';
}
}

View File

@@ -1,5 +1,17 @@
<?php
// +----------------------------------------------------------------------
// | wechat-php-sdk
// +----------------------------------------------------------------------
// | 版权所有 2014~2017 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
// +----------------------------------------------------------------------
// | 官方文档: https://www.kancloud.cn/zoujingli/wechat-php-sdk
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// +----------------------------------------------------------------------
// | github开源项目https://github.com/zoujingli/wechat-php-sdk
// +----------------------------------------------------------------------
namespace Wechat\Lib;
use Prpcrypt;
@@ -13,7 +25,8 @@ use Wechat\Loader;
* @author Anyon <zoujingli@qq.com>
* @date 2016/05/28 11:55
*/
class Common {
class Common
{
/** API接口URL需要使用此前缀 */
const API_BASE_URL_PREFIX = 'https://api.weixin.qq.com';
@@ -31,13 +44,14 @@ class Common {
public $errCode = 0;
public $errMsg = "";
public $config = array();
private $_retry = FALSE;
private $_retry = false;
/**
* 构造方法
* @param array $options
*/
public function __construct($options = array()) {
public function __construct($options = array())
{
$config = Loader::config($options);
$this->token = isset($config['token']) ? $config['token'] : '';
$this->appid = isset($config['appid']) ? $config['appid'] : '';
@@ -46,11 +60,49 @@ class Common {
$this->config = $config;
}
/**
* 当前当前错误代码
* @return int
*/
public function getErrorCode()
{
return $this->errCode;
}
/**
* 获取当前错误内容
* @return string
*/
public function getError()
{
return $this->errMsg;
}
/**
* 获取当前操作公众号APPID
* @return string
*/
public function getAppid()
{
return $this->appid;
}
/**
* 获取SDK配置参数
* @return array
*/
public function getConfig()
{
return $this->config;
}
/**
* 接口验证
* @return bool
*/
public function valid() {
public function valid()
{
$encryptStr = "";
if ($_SERVER['REQUEST_METHOD'] == "POST") {
$postStr = file_get_contents("php://input");
@@ -58,7 +110,7 @@ class Common {
$this->encrypt_type = isset($_GET["encrypt_type"]) ? $_GET["encrypt_type"] : '';
if ($this->encrypt_type == 'aes') {
$encryptStr = $array['Encrypt'];
!class_exists('Prpcrypt', FALSE) && require __DIR__ . '/Prpcrypt.php';
!class_exists('Prpcrypt', false) && require __DIR__ . '/Prpcrypt.php';
$pc = new Prpcrypt($this->encodingAesKey);
$array = $pc->decrypt($encryptStr, $this->appid);
if (!isset($array[0]) || intval($array[0]) > 0) {
@@ -75,9 +127,8 @@ class Common {
} elseif (isset($_GET["echostr"])) {
if ($this->checkSignature()) {
exit($_GET["echostr"]);
} else {
return false;
}
return false;
}
if (!$this->checkSignature($encryptStr)) {
$this->errMsg = 'Interface authentication failed, please use the correct method to call.';
@@ -91,8 +142,8 @@ class Common {
* @param string $str
* @return bool
*/
private function checkSignature($str = '') {
// 如果存在加密验证则用加密验证段
private function checkSignature($str = '')
{
$signature = isset($_GET["msg_signature"]) ? $_GET["msg_signature"] : (isset($_GET["signature"]) ? $_GET["signature"] : '');
$timestamp = isset($_GET["timestamp"]) ? $_GET["timestamp"] : '';
$nonce = isset($_GET["nonce"]) ? $_GET["nonce"] : '';
@@ -100,9 +151,8 @@ class Common {
sort($tmpArr, SORT_STRING);
if (sha1(implode($tmpArr)) == $signature) {
return true;
} else {
return false;
}
return false;
}
/**
@@ -112,7 +162,8 @@ class Common {
* @param string $token 手动指定access_token非必要情况不建议用
* @return bool|string
*/
public function getAccessToken($appid = '', $appsecret = '', $token = '') {
public function getAccessToken($appid = '', $appsecret = '', $token = '')
{
if (!$appid || !$appsecret) {
$appid = $this->appid;
$appsecret = $this->appsecret;
@@ -151,7 +202,8 @@ class Common {
* @param array $arguments SDK方法参数
* @return bool|mixed
*/
protected function checkRetry($method, $arguments = array()) {
protected function checkRetry($method, $arguments = array())
{
if (!$this->_retry && in_array($this->errCode, array('40014', '40001', '41001', '42001'))) {
Tools::log("Run {$method} Faild. {$this->errMsg}[{$this->errCode}]", 'ERR');
($this->_retry = true) && $this->resetAuth();
@@ -168,7 +220,8 @@ class Common {
* @param string $appid 如在类初始化时已提供,则可为空
* @return bool
*/
public function resetAuth($appid = '') {
public function resetAuth($appid = '')
{
$authname = 'wechat_access_token_' . (empty($appid) ? $this->appid : $appid);
Tools::log("Reset Auth And Remove Old AccessToken.");
$this->access_token = '';

View File

@@ -1,12 +1,25 @@
<?php
// +----------------------------------------------------------------------
// | wechat-php-sdk
// +----------------------------------------------------------------------
// | 版权所有 2014~2017 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
// +----------------------------------------------------------------------
// | 官方文档: https://www.kancloud.cn/zoujingli/wechat-php-sdk
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// +----------------------------------------------------------------------
// | github开源项目https://github.com/zoujingli/wechat-php-sdk
// +----------------------------------------------------------------------
/**
* PKCS7算法 加解密
* @category WechatSDK
* @subpackage library
* @date 2016/06/28 11:59
*/
class PKCS7Encoder {
class PKCS7Encoder
{
public static $block_size = 32;
@@ -15,7 +28,8 @@ class PKCS7Encoder {
* @param string $text 需要进行填充补位操作的明文
* @return string 补齐明文字符串
*/
function encode($text) {
function encode($text)
{
$amount_to_pad = PKCS7Encoder::$block_size - (strlen($text) % PKCS7Encoder::$block_size);
if ($amount_to_pad == 0) {
$amount_to_pad = PKCS7Encoder::$block_size;
@@ -33,7 +47,8 @@ class PKCS7Encoder {
* @param string $text 解密后的明文
* @return string 删除填充补位后的明文
*/
function decode($text) {
function decode($text)
{
$pad = ord(substr($text, -1));
if ($pad < 1 || $pad > PKCS7Encoder::$block_size) {
$pad = 0;
@@ -49,11 +64,13 @@ class PKCS7Encoder {
* @subpackage library
* @date 2016/06/28 11:59
*/
class Prpcrypt {
class Prpcrypt
{
public $key;
function __construct($k) {
function __construct($k)
{
$this->key = base64_decode($k . "=");
}
@@ -61,9 +78,10 @@ class Prpcrypt {
* 对明文进行加密
* @param string $text 需要加密的明文
* @param string $appid 公众号APPID
* @return string 加密后的密文
* @return array
*/
public function encrypt($text, $appid) {
public function encrypt($text, $appid)
{
try {
//获得16位随机字符串填充到明文之前
$random = $this->getRandomStr();//"aaaabbbbccccdddd";
@@ -82,9 +100,10 @@ class Prpcrypt {
* 对密文进行解密
* @param string $encrypted 需要解密的密文
* @param string $appid 公众号APPID
* @return string 解密得到的明文
* @return array
*/
public function decrypt($encrypted, $appid) {
public function decrypt($encrypted, $appid)
{
try {
$iv = substr($this->key, 0, 16);
$decrypted = openssl_decrypt($encrypted, 'AES-256-CBC', substr($this->key, 0, 32), OPENSSL_ZERO_PADDING, $iv);
@@ -95,27 +114,26 @@ class Prpcrypt {
$pkc_encoder = new PKCS7Encoder;
$result = $pkc_encoder->decode($decrypted);
if (strlen($result) < 16) {
return "";
return array(ErrorCode::$DecryptAESError, null);
}
$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);
if (!$appid) {
$appid = $from_appid;
}
return array(0, $xml_content, $from_appid);
} catch (Exception $e) {
return array(ErrorCode::$IllegalBuffer, null);
}
return array(0, $xml_content, $from_appid);
}
/**
* 随机生成16位字符串
* @return string 生成的字符串
*/
function getRandomStr() {
function getRandomStr()
{
$str = "";
$str_pol = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";
$max = strlen($str_pol) - 1;
@@ -132,7 +150,8 @@ class Prpcrypt {
* 不用于官方API接口的errCode码
* Class ErrorCode
*/
class ErrorCode {
class ErrorCode
{
public static $OK = 0;
public static $ValidateSignatureError = 40001;
@@ -166,7 +185,8 @@ class ErrorCode {
* @param string $err
* @return bool
*/
public static function getErrText($err) {
public static function getErrText($err)
{
if (isset(self::$errCode[$err])) {
return self::$errCode[$err];
}

View File

@@ -1,5 +1,17 @@
<?php
// +----------------------------------------------------------------------
// | wechat-php-sdk
// +----------------------------------------------------------------------
// | 版权所有 2014~2017 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
// +----------------------------------------------------------------------
// | 官方文档: https://www.kancloud.cn/zoujingli/wechat-php-sdk
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// +----------------------------------------------------------------------
// | github开源项目https://github.com/zoujingli/wechat-php-sdk
// +----------------------------------------------------------------------
namespace Wechat\Lib;
use CURLFile;
@@ -12,7 +24,8 @@ use CURLFile;
* @author Anyon <zoujingli@qq.com>
* @date 2016/05/28 11:55
*/
class Tools {
class Tools
{
/**
* 产生随机字符串
@@ -20,7 +33,8 @@ class Tools {
* @param string $str
* @return string
*/
static public function createNoncestr($length = 32, $str = "") {
static public function createNoncestr($length = 32, $str = "")
{
$chars = "abcdefghijklmnopqrstuvwxyz0123456789";
for ($i = 0; $i < $length; $i++) {
$str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
@@ -34,7 +48,8 @@ class Tools {
* @param string $method 签名方法
* @return bool|string 签名值
*/
static public function getSignature($arrdata, $method = "sha1") {
static public function getSignature($arrdata, $method = "sha1")
{
if (!function_exists($method)) {
return false;
}
@@ -52,7 +67,8 @@ class Tools {
* @param string $partnerKey
* @return string
*/
static public function getPaySign($option, $partnerKey) {
static public function getPaySign($option, $partnerKey)
{
ksort($option);
$buff = '';
foreach ($option as $k => $v) {
@@ -69,11 +85,21 @@ class Tools {
* @param string $id 数字索引子节点key转换的属性名
* @return string
*/
static public function arr2xml($data, $root = 'xml', $item = 'item', $id = 'id') {
static public function arr2xml($data, $root = 'xml', $item = 'item', $id = 'id')
{
return "<{$root}>" . self::_data_to_xml($data, $item, $id) . "</{$root}>";
}
static private function _data_to_xml($data, $item = 'item', $id = 'id', $content = '') {
/**
* XML内容生成
* @param array $data 数据
* @param string $item 子节点
* @param string $id 节点ID
* @param string $content 节点内容
* @return string
*/
static private function _data_to_xml($data, $item = 'item', $id = 'id', $content = '')
{
foreach ($data as $key => $val) {
is_numeric($key) && $key = "{$item} {$id}=\"{$key}\"";
$content .= "<{$key}>";
@@ -96,7 +122,8 @@ class Tools {
* @param string $xml
* @return array
*/
static public function xml2arr($xml) {
static public function xml2arr($xml)
{
return json_decode(Tools::json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
}
@@ -105,7 +132,8 @@ class Tools {
* @param array $array
* @return string
*/
static public function json_encode($array) {
static public function json_encode($array)
{
return preg_replace_callback('/\\\\u([0-9a-f]{4})/i', create_function('$matches', 'return mb_convert_encoding(pack("H*", $matches[1]), "UTF-8", "UCS-2BE");'), json_encode($array));
}
@@ -114,11 +142,12 @@ class Tools {
* @param $url
* @return bool|mixed
*/
static public function httpGet($url) {
static public function httpGet($url)
{
$oCurl = curl_init();
if (stripos($url, "https://") !== FALSE) {
curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, FALSE);
if (stripos($url, "https://") !== false) {
curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($oCurl, CURLOPT_SSLVERSION, 1);
}
curl_setopt($oCurl, CURLOPT_URL, $url);
@@ -128,9 +157,8 @@ class Tools {
curl_close($oCurl);
if (intval($aStatus["http_code"]) == 200) {
return $sContent;
} else {
return false;
}
return false;
}
/**
@@ -139,17 +167,18 @@ class Tools {
* @param array|string $data
* @return bool|mixed
*/
static public function httpPost($url, $data) {
static public function httpPost($url, $data)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_POST, true);
if (is_array($data)) {
foreach ($data as &$value) {
if (is_string($value) && stripos($value, '@') === 0 && class_exists('CURLFile', FALSE)) {
if (is_string($value) && stripos($value, '@') === 0 && class_exists('CURLFile', false)) {
$value = new CURLFile(realpath(trim($value, '@')));
}
}
@@ -172,16 +201,15 @@ class Tools {
* @param int $second 设置请求超时时间
* @return bool|mixed
*/
static public function httpsPost($url, $postdata, $ssl_cer = null, $ssl_key = null, $second = 30) {
static public function httpsPost($url, $postdata, $ssl_cer = null, $ssl_key = null, $second = 30)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_TIMEOUT, $second);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
/* 要求结果为字符串且输出到屏幕上 */
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
/* 设置证书 */
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
if (!is_null($ssl_cer) && file_exists($ssl_cer) && is_file($ssl_cer)) {
curl_setopt($ch, CURLOPT_SSLCERTTYPE, 'PEM');
curl_setopt($ch, CURLOPT_SSLCERT, $ssl_cer);
@@ -193,7 +221,7 @@ class Tools {
curl_setopt($ch, CURLOPT_POST, true);
if (is_array($postdata)) {
foreach ($postdata as &$data) {
if (is_string($data) && stripos($data, '@') === 0 && class_exists('CURLFile', FALSE)) {
if (is_string($data) && stripos($data, '@') === 0 && class_exists('CURLFile', false)) {
$data = new CURLFile(realpath(trim($data, '@')));
}
}
@@ -203,23 +231,23 @@ class Tools {
curl_close($ch);
if ($result) {
return $result;
} else {
return false;
}
return false;
}
/**
* 读取微信客户端IP
* @return null|string
*/
static public function getAddress() {
static public function getAddress()
{
foreach (array('HTTP_X_FORWARDED_FOR', 'HTTP_CLIENT_IP', 'HTTP_X_CLIENT_IP', 'HTTP_X_CLUSTER_CLIENT_IP', 'REMOTE_ADDR') as $header) {
if (!isset($_SERVER[$header]) || ($spoof = $_SERVER[$header]) === NULL) {
if (!isset($_SERVER[$header]) || ($spoof = $_SERVER[$header]) === null) {
continue;
}
sscanf($spoof, '%[^,]', $spoof);
if (!filter_var($spoof, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
$spoof = NULL;
$spoof = null;
} else {
return $spoof;
}
@@ -234,7 +262,8 @@ class Tools {
* @param int $expired
* @return bool
*/
static public function setCache($cachename, $value, $expired = 0) {
static public function setCache($cachename, $value, $expired = 0)
{
return Cache::set($cachename, $value, $expired);
}
@@ -243,7 +272,8 @@ class Tools {
* @param string $cachename
* @return mixed
*/
static public function getCache($cachename) {
static public function getCache($cachename)
{
return Cache::get($cachename);
}
@@ -252,7 +282,8 @@ class Tools {
* @param string $cachename
* @return bool
*/
static public function removeCache($cachename) {
static public function removeCache($cachename)
{
return Cache::del($cachename);
}
@@ -261,7 +292,8 @@ class Tools {
* @param string $msg 日志行内容
* @param string $type 日志级别
*/
static public function log($msg, $type = 'MSG') {
static public function log($msg, $type = 'MSG')
{
Cache::put($type . ' - ' . $msg);
}