[更新]增加MyCurlFile文件类型

This commit is contained in:
Anyon 2018-12-05 16:58:37 +08:00
parent f4c08f4c81
commit 018918d342
10 changed files with 103 additions and 64 deletions

View File

@ -14,7 +14,6 @@
namespace WeChat; namespace WeChat;
use WeChat\Contracts\BasicWeChat; use WeChat\Contracts\BasicWeChat;
/** /**
@ -670,5 +669,4 @@ class Card extends BasicWeChat
return $this->httpPostForJson($url, $data); return $this->httpPostForJson($url, $data);
} }
} }

View File

@ -165,10 +165,7 @@ class BasicPushEvent
$signature = empty($msg_signature) ? $this->params->get('signature') : $msg_signature; $signature = empty($msg_signature) ? $this->params->get('signature') : $msg_signature;
$tmpArr = [$this->config->get('token'), $timestamp, $nonce, $str]; $tmpArr = [$this->config->get('token'), $timestamp, $nonce, $str];
sort($tmpArr, SORT_STRING); sort($tmpArr, SORT_STRING);
if (sha1(implode($tmpArr)) == $signature) { return sha1(implode($tmpArr)) === $signature;
return true;
}
return false;
} }
/** /**

View File

@ -141,7 +141,8 @@ class BasicWeChat
* 以GET获取接口数据并转为数组 * 以GET获取接口数据并转为数组
* @param string $url 接口地址 * @param string $url 接口地址
* @return array * @return array
* @throws \WeChat\Exceptions\InvalidResponseException * @throws InvalidResponseException
* @throws \WeChat\Exceptions\LocalCacheException
*/ */
protected function httpGetForJson($url) protected function httpGetForJson($url)
{ {
@ -165,7 +166,8 @@ class BasicWeChat
* @param array $data 请求数据 * @param array $data 请求数据
* @param bool $buildToJson * @param bool $buildToJson
* @return array * @return array
* @throws \WeChat\Exceptions\InvalidResponseException * @throws InvalidResponseException
* @throws \WeChat\Exceptions\LocalCacheException
*/ */
protected function httpPostForJson($url, array $data, $buildToJson = true) protected function httpPostForJson($url, array $data, $buildToJson = true)
{ {

View File

@ -9,24 +9,33 @@ namespace WeChat\Contracts;
*/ */
class MyCurlFile extends \stdClass class MyCurlFile extends \stdClass
{ {
/**
* 当前数据类型
* @var string
*/
public $datatype = 'MY_CURL_FILE';
/** /**
* MyCurlFile constructor. * MyCurlFile constructor.
* @param $filename * @param string|array $filename
* @param string $mimetype * @param string $mimetype
* @param string $postname * @param string $postname
* @throws \WeChat\Exceptions\LocalCacheException * @throws \WeChat\Exceptions\LocalCacheException
*/ */
public function __construct($filename, $mimetype = '', $postname = '') public function __construct($filename, $mimetype = '', $postname = '')
{ {
if (is_array($filename)) {
$this->mimetype = $mimetype; foreach ($filename as $k => $v) $this->{$k} = $v;
$this->postname = $postname; } else {
$this->content = base64_encode(file_get_contents($filename)); $this->mimetype = $mimetype;
$this->extension = pathinfo($filename, PATHINFO_EXTENSION); $this->postname = $postname;
if (empty($this->extension)) $this->extension = 'tmp'; $this->extension = pathinfo($filename, PATHINFO_EXTENSION);
if (empty($this->mimetype)) $this->mimetype = Tools::getExtMine($this->extension); if (empty($this->extension)) $this->extension = 'tmp';
if (empty($this->postname)) $this->postname = pathinfo($filename, PATHINFO_BASENAME); if (empty($this->mimetype)) $this->mimetype = Tools::getExtMine($this->extension);
if (empty($this->postname)) $this->postname = pathinfo($filename, PATHINFO_BASENAME);
$this->content = base64_encode(file_get_contents($filename));
$this->tempname = md5($this->content) . ".{$this->extension}";
}
} }
/** /**
@ -36,7 +45,6 @@ class MyCurlFile extends \stdClass
*/ */
public function get() public function get()
{ {
$this->tempname = rand(100000, 999999) . ".{$this->extension}";
$this->filename = Tools::pushFile($this->tempname, base64_decode($this->content)); $this->filename = Tools::pushFile($this->tempname, base64_decode($this->content));
if (class_exists('CURLFile')) { if (class_exists('CURLFile')) {
return new \CURLFile($this->filename, $this->mimetype, $this->postname); return new \CURLFile($this->filename, $this->mimetype, $this->postname);
@ -49,7 +57,7 @@ class MyCurlFile extends \stdClass
*/ */
public function __destruct() public function __destruct()
{ {
Tools::delCache($this->tempname); // Tools::delCache($this->tempname);
} }
} }

View File

@ -31,6 +31,11 @@ class Tools
*/ */
public static $cache_path = null; public static $cache_path = null;
/**
* 网络缓存
* @var array
*/
private static $cache_curl = [];
/** /**
* 产生随机字符串 * 产生随机字符串
@ -49,7 +54,7 @@ class Tools
/** /**
* 根据文件后缀获取文件MINE * 根据文件后缀获取文件类型
* @param string|array $ext 文件后缀 * @param string|array $ext 文件后缀
* @param array $mine 文件后缀MINE信息 * @param array $mine 文件后缀MINE信息
* @return string * @return string
@ -65,7 +70,7 @@ class Tools
} }
/** /**
* 获取所有文件扩展的mine * 获取所有文件扩展的类型
* @return array * @return array
* @throws LocalCacheException * @throws LocalCacheException
*/ */
@ -75,9 +80,7 @@ class Tools
if (empty($mines)) { if (empty($mines)) {
$content = file_get_contents('http://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types'); $content = file_get_contents('http://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types');
preg_match_all('#^([^\s]{2,}?)\s+(.+?)$#ism', $content, $matches, PREG_SET_ORDER); preg_match_all('#^([^\s]{2,}?)\s+(.+?)$#ism', $content, $matches, PREG_SET_ORDER);
foreach ($matches as $match) foreach (explode(" ", $match[2]) as $ext) { foreach ($matches as $match) foreach (explode(" ", $match[2]) as $ext) $mines[$ext] = $match[1];
$mines[$ext] = $match[1];
}
self::setCache('all_ext_mine', $mines); self::setCache('all_ext_mine', $mines);
} }
return $mines; return $mines;
@ -93,8 +96,8 @@ class Tools
*/ */
public static function createCurlFile($filename, $mimetype = null, $postname = null) public static function createCurlFile($filename, $mimetype = null, $postname = null)
{ {
is_null($postname) && $postname = basename($filename); if (is_null($postname)) $postname = basename($filename);
is_null($mimetype) && $mimetype = self::getExtMine(pathinfo($filename, 4)); if (is_null($mimetype)) $mimetype = self::getExtMine(pathinfo($filename, 4));
if (function_exists('curl_file_create')) { if (function_exists('curl_file_create')) {
return curl_file_create($filename, $mimetype, $postname); return curl_file_create($filename, $mimetype, $postname);
} }
@ -182,7 +185,8 @@ class Tools
* @param string $url 访问URL * @param string $url 访问URL
* @param array $query GET数 * @param array $query GET数
* @param array $options * @param array $options
* @return bool|string * @return boolean|string
* @throws LocalCacheException
*/ */
public static function get($url, $query = [], $options = []) public static function get($url, $query = [], $options = [])
{ {
@ -195,7 +199,8 @@ class Tools
* @param string $url 访问URL * @param string $url 访问URL
* @param array $data POST数据 * @param array $data POST数据
* @param array $options * @param array $options
* @return bool|string * @return boolean|string
* @throws LocalCacheException
*/ */
public static function post($url, $data = [], $options = []) public static function post($url, $data = [], $options = [])
{ {
@ -208,9 +213,10 @@ class Tools
* @param string $method 请求方法 * @param string $method 请求方法
* @param string $url 请求方法 * @param string $url 请求方法
* @param array $options 请求参数[headers,data,ssl_cer,ssl_key] * @param array $options 请求参数[headers,data,ssl_cer,ssl_key]
* @return bool|string * @return boolean|string
* @throws LocalCacheException
*/ */
protected static function doRequest($method, $url, $options = []) public static function doRequest($method, $url, $options = [])
{ {
$curl = curl_init(); $curl = curl_init();
// GET参数设置 // GET参数设置
@ -224,34 +230,58 @@ class Tools
// POST数据设置 // POST数据设置
if (strtolower($method) === 'post') { if (strtolower($method) === 'post') {
curl_setopt($curl, CURLOPT_POST, true); curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $options['data']); curl_setopt($curl, CURLOPT_POSTFIELDS, self::_buildHttpData($options['data']));
} }
// 证书文件设置 // 证书文件设置
if (!empty($options['ssl_cer'])) { if (!empty($options['ssl_cer'])) if (file_exists($options['ssl_cer'])) {
if (file_exists($options['ssl_cer'])) { curl_setopt($curl, CURLOPT_SSLCERTTYPE, 'PEM');
curl_setopt($curl, CURLOPT_SSLCERTTYPE, 'PEM'); curl_setopt($curl, CURLOPT_SSLCERT, $options['ssl_cer']);
curl_setopt($curl, CURLOPT_SSLCERT, $options['ssl_cer']); } else throw new InvalidArgumentException("Certificate files that do not exist. --- [ssl_cer]");
} else {
throw new InvalidArgumentException("Certificate files that do not exist. --- [ssl_cer]");
}
}
// 证书文件设置 // 证书文件设置
if (!empty($options['ssl_key'])) { if (!empty($options['ssl_key'])) if (file_exists($options['ssl_key'])) {
if (file_exists($options['ssl_key'])) { curl_setopt($curl, CURLOPT_SSLKEYTYPE, 'PEM');
curl_setopt($curl, CURLOPT_SSLKEYTYPE, 'PEM'); curl_setopt($curl, CURLOPT_SSLKEY, $options['ssl_key']);
curl_setopt($curl, CURLOPT_SSLKEY, $options['ssl_key']); } else throw new InvalidArgumentException("Certificate files that do not exist. --- [ssl_key]");
} else {
throw new InvalidArgumentException("Certificate files that do not exist. --- [ssl_key]");
}
}
curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_TIMEOUT, 60); curl_setopt($curl, CURLOPT_TIMEOUT, 60);
curl_setopt($curl, CURLOPT_HEADER, false); curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
list($content, $status) = [curl_exec($curl), curl_getinfo($curl), curl_close($curl)]; list($content) = [curl_exec($curl), curl_close($curl)];
return (intval($status["http_code"]) === 200) ? $content : false; // 清理 CURL 缓存文件
if (!empty(self::$cache_curl)) foreach (self::$cache_curl as $key => $file) {
Tools::delCache($file);
unset(self::$cache_curl[$key]);
}
return $content;
}
/**
* POST数据过滤处理
* @param array $data 需要处理的数据
* @param boolean $build 是否编译数据
* @return array|string
* @throws \WeChat\Exceptions\LocalCacheException
*/
private static function _buildHttpData($data, $build = true)
{
if (!is_array($data)) return $data;
foreach ($data as $key => $value) {
if (is_object($value) && $value instanceof \CURLFile) {
$build = false;
} elseif (is_object($value) && isset($value->datatype) && $value->datatype === 'MY_CURL_FILE') {
$build = false;
$data[$key] = ($myCurlFile = new MyCurlFile((array)$value))->get();
array_push(self::$cache_curl, $myCurlFile->tempname);
} elseif (is_string($value) && class_exists('CURLFile', false) && stripos($value, '@') === 0) {
if (($filename = realpath(trim($value, '@'))) && file_exists($filename)) {
$build = false;
$data[$key] = self::createCurlFile($filename);
}
}
}
return $build ? http_build_query($data) : $data;
} }
/** /**
@ -263,8 +293,10 @@ class Tools
*/ */
public static function pushFile($name, $content) public static function pushFile($name, $content)
{ {
$file = self::getCacheName($name); $file = self::_getCacheName($name);
if (!file_put_contents($file, $content)) throw new LocalCacheException('local file write error.', '0'); if (!file_put_contents($file, $content)) {
throw new LocalCacheException('local file write error.', '0');
}
return $file; return $file;
} }
@ -278,9 +310,10 @@ class Tools
*/ */
public static function setCache($name, $value = '', $expired = 3600) public static function setCache($name, $value = '', $expired = 3600)
{ {
$file = self::getCacheName($name); $file = self::_getCacheName($name);
$content = serialize(['name' => $name, 'value' => $value, 'expired' => time() + intval($expired)]); if (!file_put_contents($file, serialize(['name' => $name, 'value' => $value, 'expired' => time() + intval($expired)]))) {
if (!file_put_contents($file, $content)) throw new LocalCacheException('local cache error.', '0'); throw new LocalCacheException('local cache error.', '0');
}
return $file; return $file;
} }
@ -291,8 +324,7 @@ class Tools
*/ */
public static function getCache($name) public static function getCache($name)
{ {
$file = self::getCacheName($name); if (file_exists($file = self::_getCacheName($name)) && ($content = file_get_contents($file))) {
if (file_exists($file) && ($content = file_get_contents($file))) {
$data = unserialize($content); $data = unserialize($content);
if (isset($data['expired']) && (intval($data['expired']) === 0 || intval($data['expired']) >= time())) { if (isset($data['expired']) && (intval($data['expired']) === 0 || intval($data['expired']) >= time())) {
return $data['value']; return $data['value'];
@ -305,11 +337,11 @@ class Tools
/** /**
* 移除缓存文件 * 移除缓存文件
* @param string $name 缓存名称 * @param string $name 缓存名称
* @return bool * @return boolean
*/ */
public static function delCache($name) public static function delCache($name)
{ {
$file = self::getCacheName($name); $file = self::_getCacheName($name);
return file_exists($file) ? unlink($file) : true; return file_exists($file) ? unlink($file) : true;
} }
@ -318,7 +350,7 @@ class Tools
* @param string $name * @param string $name
* @return string * @return string
*/ */
private static function getCacheName($name) private static function _getCacheName($name)
{ {
if (empty(self::$cache_path)) { if (empty(self::$cache_path)) {
self::$cache_path = dirname(dirname(__DIR__)) . DIRECTORY_SEPARATOR . 'Cache' . DIRECTORY_SEPARATOR; self::$cache_path = dirname(dirname(__DIR__)) . DIRECTORY_SEPARATOR . 'Cache' . DIRECTORY_SEPARATOR;

View File

@ -14,7 +14,6 @@
namespace WeChat; namespace WeChat;
use WeChat\Contracts\BasicWeChat; use WeChat\Contracts\BasicWeChat;
/** /**

View File

@ -42,6 +42,7 @@ class Oauth extends BasicWeChat
* 通过 code 获取 AccessToken openid * 通过 code 获取 AccessToken openid
* @return bool|array * @return bool|array
* @throws Exceptions\InvalidResponseException * @throws Exceptions\InvalidResponseException
* @throws Exceptions\LocalCacheException
*/ */
public function getOauthAccessToken() public function getOauthAccessToken()
{ {
@ -57,6 +58,7 @@ class Oauth extends BasicWeChat
* @param string $refresh_token * @param string $refresh_token
* @return bool|array * @return bool|array
* @throws Exceptions\InvalidResponseException * @throws Exceptions\InvalidResponseException
* @throws Exceptions\LocalCacheException
*/ */
public function getOauthRefreshToken($refresh_token) public function getOauthRefreshToken($refresh_token)
{ {
@ -71,6 +73,7 @@ class Oauth extends BasicWeChat
* @param string $openid 用户的唯一标识 * @param string $openid 用户的唯一标识
* @return array * @return array
* @throws Exceptions\InvalidResponseException * @throws Exceptions\InvalidResponseException
* @throws Exceptions\LocalCacheException
*/ */
public function checkOauthAccessToken($access_token, $openid) public function checkOauthAccessToken($access_token, $openid)
{ {
@ -85,6 +88,7 @@ class Oauth extends BasicWeChat
* @param string $lang 返回国家地区语言版本zh_CN 简体zh_TW 繁体en 英语 * @param string $lang 返回国家地区语言版本zh_CN 简体zh_TW 繁体en 英语
* @return array * @return array
* @throws Exceptions\InvalidResponseException * @throws Exceptions\InvalidResponseException
* @throws Exceptions\LocalCacheException
*/ */
public function getUserInfo($access_token, $openid, $lang = 'zh_CN') public function getUserInfo($access_token, $openid, $lang = 'zh_CN')
{ {

View File

@ -162,12 +162,13 @@ class Pay extends BasicWePay
* 拉取订单评价数据 * 拉取订单评价数据
* @param array $options * @param array $options
* @return array * @return array
* @throws Exceptions\LocalCacheException
* @throws InvalidResponseException * @throws InvalidResponseException
*/ */
public function billCommtent(array $options) public function billCommtent(array $options)
{ {
$pay = new Bill($this->config->get()); $pay = new Bill($this->config->get());
return $pay->commtent($options); return $pay->comment($options);
} }
/** /**

View File

@ -14,7 +14,6 @@
namespace WeChat; namespace WeChat;
use WeChat\Contracts\BasicWeChat; use WeChat\Contracts\BasicWeChat;
/** /**

View File

@ -14,7 +14,6 @@
namespace WeChat; namespace WeChat;
use WeChat\Contracts\BasicWeChat; use WeChat\Contracts\BasicWeChat;
/** /**