From 018918d342317f91b06978cef8d532ee16840623 Mon Sep 17 00:00:00 2001 From: Anyon Date: Wed, 5 Dec 2018 16:58:37 +0800 Subject: [PATCH] =?UTF-8?q?[=E6=9B=B4=E6=96=B0]=E5=A2=9E=E5=8A=A0MyCurlFil?= =?UTF-8?q?e=E6=96=87=E4=BB=B6=E7=B1=BB=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- WeChat/Card.php | 4 +- WeChat/Contracts/BasicPushEvent.php | 5 +- WeChat/Contracts/BasicWeChat.php | 6 +- WeChat/Contracts/MyCurlFile.php | 30 +++++--- WeChat/Contracts/Tools.php | 112 ++++++++++++++++++---------- WeChat/Limit.php | 1 - WeChat/Oauth.php | 4 + WeChat/Pay.php | 3 +- WeChat/Product.php | 1 - WeChat/Wifi.php | 1 - 10 files changed, 103 insertions(+), 64 deletions(-) diff --git a/WeChat/Card.php b/WeChat/Card.php index 59cba2d..b392781 100644 --- a/WeChat/Card.php +++ b/WeChat/Card.php @@ -14,7 +14,6 @@ namespace WeChat; - use WeChat\Contracts\BasicWeChat; /** @@ -669,6 +668,5 @@ class Card extends BasicWeChat $this->registerApi($url, __FUNCTION__, func_get_args()); return $this->httpPostForJson($url, $data); } - - + } \ No newline at end of file diff --git a/WeChat/Contracts/BasicPushEvent.php b/WeChat/Contracts/BasicPushEvent.php index 205075e..a221f18 100644 --- a/WeChat/Contracts/BasicPushEvent.php +++ b/WeChat/Contracts/BasicPushEvent.php @@ -165,10 +165,7 @@ class BasicPushEvent $signature = empty($msg_signature) ? $this->params->get('signature') : $msg_signature; $tmpArr = [$this->config->get('token'), $timestamp, $nonce, $str]; sort($tmpArr, SORT_STRING); - if (sha1(implode($tmpArr)) == $signature) { - return true; - } - return false; + return sha1(implode($tmpArr)) === $signature; } /** diff --git a/WeChat/Contracts/BasicWeChat.php b/WeChat/Contracts/BasicWeChat.php index 3dd4eb0..da8e2f8 100644 --- a/WeChat/Contracts/BasicWeChat.php +++ b/WeChat/Contracts/BasicWeChat.php @@ -141,7 +141,8 @@ class BasicWeChat * 以GET获取接口数据并转为数组 * @param string $url 接口地址 * @return array - * @throws \WeChat\Exceptions\InvalidResponseException + * @throws InvalidResponseException + * @throws \WeChat\Exceptions\LocalCacheException */ protected function httpGetForJson($url) { @@ -165,7 +166,8 @@ class BasicWeChat * @param array $data 请求数据 * @param bool $buildToJson * @return array - * @throws \WeChat\Exceptions\InvalidResponseException + * @throws InvalidResponseException + * @throws \WeChat\Exceptions\LocalCacheException */ protected function httpPostForJson($url, array $data, $buildToJson = true) { diff --git a/WeChat/Contracts/MyCurlFile.php b/WeChat/Contracts/MyCurlFile.php index 291c27e..e81e3da 100644 --- a/WeChat/Contracts/MyCurlFile.php +++ b/WeChat/Contracts/MyCurlFile.php @@ -9,24 +9,33 @@ namespace WeChat\Contracts; */ class MyCurlFile extends \stdClass { + /** + * 当前数据类型 + * @var string + */ + public $datatype = 'MY_CURL_FILE'; /** * MyCurlFile constructor. - * @param $filename + * @param string|array $filename * @param string $mimetype * @param string $postname * @throws \WeChat\Exceptions\LocalCacheException */ public function __construct($filename, $mimetype = '', $postname = '') { - - $this->mimetype = $mimetype; - $this->postname = $postname; - $this->content = base64_encode(file_get_contents($filename)); - $this->extension = pathinfo($filename, PATHINFO_EXTENSION); - if (empty($this->extension)) $this->extension = 'tmp'; - if (empty($this->mimetype)) $this->mimetype = Tools::getExtMine($this->extension); - if (empty($this->postname)) $this->postname = pathinfo($filename, PATHINFO_BASENAME); + if (is_array($filename)) { + foreach ($filename as $k => $v) $this->{$k} = $v; + } else { + $this->mimetype = $mimetype; + $this->postname = $postname; + $this->extension = pathinfo($filename, PATHINFO_EXTENSION); + if (empty($this->extension)) $this->extension = 'tmp'; + 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() { - $this->tempname = rand(100000, 999999) . ".{$this->extension}"; $this->filename = Tools::pushFile($this->tempname, base64_decode($this->content)); if (class_exists('CURLFile')) { return new \CURLFile($this->filename, $this->mimetype, $this->postname); @@ -49,7 +57,7 @@ class MyCurlFile extends \stdClass */ public function __destruct() { - Tools::delCache($this->tempname); + // Tools::delCache($this->tempname); } } \ No newline at end of file diff --git a/WeChat/Contracts/Tools.php b/WeChat/Contracts/Tools.php index 1037836..90db7ac 100644 --- a/WeChat/Contracts/Tools.php +++ b/WeChat/Contracts/Tools.php @@ -31,6 +31,11 @@ class Tools */ public static $cache_path = null; + /** + * 网络缓存 + * @var array + */ + private static $cache_curl = []; /** * 产生随机字符串 @@ -49,7 +54,7 @@ class Tools /** - * 根据文件后缀获取文件MINE + * 根据文件后缀获取文件类型 * @param string|array $ext 文件后缀 * @param array $mine 文件后缀MINE信息 * @return string @@ -65,7 +70,7 @@ class Tools } /** - * 获取所有文件扩展的mine + * 获取所有文件扩展的类型 * @return array * @throws LocalCacheException */ @@ -75,9 +80,7 @@ class Tools if (empty($mines)) { $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); - foreach ($matches as $match) foreach (explode(" ", $match[2]) as $ext) { - $mines[$ext] = $match[1]; - } + foreach ($matches as $match) foreach (explode(" ", $match[2]) as $ext) $mines[$ext] = $match[1]; self::setCache('all_ext_mine', $mines); } return $mines; @@ -93,8 +96,8 @@ class Tools */ public static function createCurlFile($filename, $mimetype = null, $postname = null) { - is_null($postname) && $postname = basename($filename); - is_null($mimetype) && $mimetype = self::getExtMine(pathinfo($filename, 4)); + if (is_null($postname)) $postname = basename($filename); + if (is_null($mimetype)) $mimetype = self::getExtMine(pathinfo($filename, 4)); if (function_exists('curl_file_create')) { return curl_file_create($filename, $mimetype, $postname); } @@ -182,7 +185,8 @@ class Tools * @param string $url 访问URL * @param array $query GET数 * @param array $options - * @return bool|string + * @return boolean|string + * @throws LocalCacheException */ public static function get($url, $query = [], $options = []) { @@ -195,7 +199,8 @@ class Tools * @param string $url 访问URL * @param array $data POST数据 * @param array $options - * @return bool|string + * @return boolean|string + * @throws LocalCacheException */ public static function post($url, $data = [], $options = []) { @@ -208,9 +213,10 @@ class Tools * @param string $method 请求方法 * @param string $url 请求方法 * @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(); // GET参数设置 @@ -224,34 +230,58 @@ class Tools // POST数据设置 if (strtolower($method) === 'post') { 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 (file_exists($options['ssl_cer'])) { - curl_setopt($curl, CURLOPT_SSLCERTTYPE, 'PEM'); - curl_setopt($curl, CURLOPT_SSLCERT, $options['ssl_cer']); - } else { - throw new InvalidArgumentException("Certificate files that do not exist. --- [ssl_cer]"); - } - } + if (!empty($options['ssl_cer'])) if (file_exists($options['ssl_cer'])) { + curl_setopt($curl, CURLOPT_SSLCERTTYPE, 'PEM'); + curl_setopt($curl, CURLOPT_SSLCERT, $options['ssl_cer']); + } else throw new InvalidArgumentException("Certificate files that do not exist. --- [ssl_cer]"); // 证书文件设置 - if (!empty($options['ssl_key'])) { - if (file_exists($options['ssl_key'])) { - curl_setopt($curl, CURLOPT_SSLKEYTYPE, 'PEM'); - curl_setopt($curl, CURLOPT_SSLKEY, $options['ssl_key']); - } else { - throw new InvalidArgumentException("Certificate files that do not exist. --- [ssl_key]"); - } - } + if (!empty($options['ssl_key'])) if (file_exists($options['ssl_key'])) { + curl_setopt($curl, CURLOPT_SSLKEYTYPE, 'PEM'); + curl_setopt($curl, CURLOPT_SSLKEY, $options['ssl_key']); + } else throw new InvalidArgumentException("Certificate files that do not exist. --- [ssl_key]"); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_TIMEOUT, 60); curl_setopt($curl, CURLOPT_HEADER, false); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); - list($content, $status) = [curl_exec($curl), curl_getinfo($curl), curl_close($curl)]; - return (intval($status["http_code"]) === 200) ? $content : false; + list($content) = [curl_exec($curl), curl_close($curl)]; + // 清理 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) { - $file = self::getCacheName($name); - if (!file_put_contents($file, $content)) throw new LocalCacheException('local file write error.', '0'); + $file = self::_getCacheName($name); + if (!file_put_contents($file, $content)) { + throw new LocalCacheException('local file write error.', '0'); + } return $file; } @@ -278,9 +310,10 @@ class Tools */ public static function setCache($name, $value = '', $expired = 3600) { - $file = self::getCacheName($name); - $content = serialize(['name' => $name, 'value' => $value, 'expired' => time() + intval($expired)]); - if (!file_put_contents($file, $content)) throw new LocalCacheException('local cache error.', '0'); + $file = self::_getCacheName($name); + if (!file_put_contents($file, serialize(['name' => $name, 'value' => $value, 'expired' => time() + intval($expired)]))) { + throw new LocalCacheException('local cache error.', '0'); + } return $file; } @@ -291,8 +324,7 @@ class Tools */ public static function getCache($name) { - $file = self::getCacheName($name); - if (file_exists($file) && ($content = file_get_contents($file))) { + if (file_exists($file = self::_getCacheName($name)) && ($content = file_get_contents($file))) { $data = unserialize($content); if (isset($data['expired']) && (intval($data['expired']) === 0 || intval($data['expired']) >= time())) { return $data['value']; @@ -305,11 +337,11 @@ class Tools /** * 移除缓存文件 * @param string $name 缓存名称 - * @return bool + * @return boolean */ public static function delCache($name) { - $file = self::getCacheName($name); + $file = self::_getCacheName($name); return file_exists($file) ? unlink($file) : true; } @@ -318,7 +350,7 @@ class Tools * @param string $name * @return string */ - private static function getCacheName($name) + private static function _getCacheName($name) { if (empty(self::$cache_path)) { self::$cache_path = dirname(dirname(__DIR__)) . DIRECTORY_SEPARATOR . 'Cache' . DIRECTORY_SEPARATOR; diff --git a/WeChat/Limit.php b/WeChat/Limit.php index 49304b5..6ccc6f4 100644 --- a/WeChat/Limit.php +++ b/WeChat/Limit.php @@ -14,7 +14,6 @@ namespace WeChat; - use WeChat\Contracts\BasicWeChat; /** diff --git a/WeChat/Oauth.php b/WeChat/Oauth.php index 151a4ee..a8be272 100644 --- a/WeChat/Oauth.php +++ b/WeChat/Oauth.php @@ -42,6 +42,7 @@ class Oauth extends BasicWeChat * 通过 code 获取 AccessToken 和 openid * @return bool|array * @throws Exceptions\InvalidResponseException + * @throws Exceptions\LocalCacheException */ public function getOauthAccessToken() { @@ -57,6 +58,7 @@ class Oauth extends BasicWeChat * @param string $refresh_token * @return bool|array * @throws Exceptions\InvalidResponseException + * @throws Exceptions\LocalCacheException */ public function getOauthRefreshToken($refresh_token) { @@ -71,6 +73,7 @@ class Oauth extends BasicWeChat * @param string $openid 用户的唯一标识 * @return array * @throws Exceptions\InvalidResponseException + * @throws Exceptions\LocalCacheException */ public function checkOauthAccessToken($access_token, $openid) { @@ -85,6 +88,7 @@ class Oauth extends BasicWeChat * @param string $lang 返回国家地区语言版本,zh_CN 简体,zh_TW 繁体,en 英语 * @return array * @throws Exceptions\InvalidResponseException + * @throws Exceptions\LocalCacheException */ public function getUserInfo($access_token, $openid, $lang = 'zh_CN') { diff --git a/WeChat/Pay.php b/WeChat/Pay.php index 5030ab5..b063a43 100644 --- a/WeChat/Pay.php +++ b/WeChat/Pay.php @@ -162,12 +162,13 @@ class Pay extends BasicWePay * 拉取订单评价数据 * @param array $options * @return array + * @throws Exceptions\LocalCacheException * @throws InvalidResponseException */ public function billCommtent(array $options) { $pay = new Bill($this->config->get()); - return $pay->commtent($options); + return $pay->comment($options); } /** diff --git a/WeChat/Product.php b/WeChat/Product.php index b1add21..74eb3ee 100644 --- a/WeChat/Product.php +++ b/WeChat/Product.php @@ -14,7 +14,6 @@ namespace WeChat; - use WeChat\Contracts\BasicWeChat; /** diff --git a/WeChat/Wifi.php b/WeChat/Wifi.php index ecc45a4..ff22aff 100644 --- a/WeChat/Wifi.php +++ b/WeChat/Wifi.php @@ -14,7 +14,6 @@ namespace WeChat; - use WeChat\Contracts\BasicWeChat; /**