diff --git a/CONTRIBUTING.md b/.github/CONTRIBUTING.md similarity index 92% rename from CONTRIBUTING.md rename to .github/CONTRIBUTING.md index b0660179c..0816fbf51 100644 --- a/CONTRIBUTING.md +++ b/.github/CONTRIBUTING.md @@ -72,4 +72,4 @@ EasyWeChat 是一个开源的,社区驱动的项目。 ## 报告 Bug -当你在使用过程中遇到问题,请查阅 [疑难解答](troubleshooting.html) 或者在这里提问 [GitHub](https://github.com/overtrue/wechat/issues). 如果还是不能解决你的问题,请到 GitHub 联系我们。 +当你在使用过程中遇到问题,请查阅 [疑难解答](http://easywechat.org/docs/zh-cn/troubleshooting.html) 或者在这里提问 [GitHub](https://github.com/overtrue/wechat/issues). 如果还是不能解决你的问题,请到 GitHub 联系我们。 diff --git a/.github/ISSUE_TEMPLATE.md b/.github/ISSUE_TEMPLATE.md new file mode 100644 index 000000000..65ae6424c --- /dev/null +++ b/.github/ISSUE_TEMPLATE.md @@ -0,0 +1,9 @@ +## 我用的环境 + +PHP 版本: +overtrue/wechat 版本: + +## 问题及现象 + +描述你的问题现象,报错贴截图粘贴或者贴具体信息 + diff --git a/src/Material/Material.php b/src/Material/Material.php index 8a1bb36af..e92aea384 100644 --- a/src/Material/Material.php +++ b/src/Material/Material.php @@ -182,6 +182,11 @@ public function get($mediaId) $json = $this->getHttp()->parseJSON($response); + // XXX: 微信开发这帮混蛋,尼玛文件二进制输出不带header,简直日了!!! + if (!$json) { + return $response->getBody(); + } + $this->checkAndThrow($json); return $json; diff --git a/src/Material/Temporary.php b/src/Material/Temporary.php index 5cda75e3c..d6597a16a 100644 --- a/src/Material/Temporary.php +++ b/src/Material/Temporary.php @@ -22,7 +22,6 @@ use EasyWeChat\Core\AbstractAPI; use EasyWeChat\Core\Exceptions\InvalidArgumentException; -use EasyWeChat\Core\Exceptions\RuntimeException; use EasyWeChat\Support\File; /** @@ -81,15 +80,7 @@ public function getStream($mediaId) { $response = $this->getHttp()->get(self::API_GET, ['media_id' => $mediaId]); - foreach ($response->getHeader('Content-Type') as $mime) { - if (preg_match('/(image|video|audio)/i', $mime)) { - return $response->getBody(); - } - } - - $this->checkAndThrow($this->getHttp()->parseJSON($response)); - - throw new RuntimeException('Unknown API response.'.$response->getBody()); + return $response->getBody(); } /** diff --git a/src/Staff/MessageBuilder.php b/src/Staff/MessageBuilder.php index 98e18741e..2a5df3ef8 100644 --- a/src/Staff/MessageBuilder.php +++ b/src/Staff/MessageBuilder.php @@ -72,7 +72,7 @@ public function __construct(Staff $staff) /** * Set message to send. * - * @param AbstractMessage $message + * @param string|AbstractMessage $message * * @return MessageBuilder * diff --git a/src/Support/Collection.php b/src/Support/Collection.php index 8705f6270..63cd50680 100644 --- a/src/Support/Collection.php +++ b/src/Support/Collection.php @@ -30,12 +30,7 @@ /** * Class Collection. */ -class Collection implements - ArrayAccess, - Countable, - IteratorAggregate, - JsonSerializable, - Serializable +class Collection implements ArrayAccess, Countable, IteratorAggregate, JsonSerializable, Serializable { /** * The collection data. diff --git a/src/Support/File.php b/src/Support/File.php index b7d7eec33..f06f8d7f7 100644 --- a/src/Support/File.php +++ b/src/Support/File.php @@ -33,29 +33,6 @@ class File * @var array */ protected static $extensionMap = array( - 'application/msword' => '.doc', - 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' => '.docx', - 'application/rtf' => '.rtf', - 'application/vnd.ms-excel' => '.xls', - 'application/x-excel' => '.xls', - 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' => '.xlsx', - 'application/vnd.ms-powerpoint' => '.ppt', - 'application/vnd.openxmlformats-officedocument.presentationml.presentation' => '.pptx', - 'application/vnd.ms-powerpoint' => '.pps', - 'application/vnd.openxmlformats-officedocument.presentationml.slideshow' => '.ppsx', - 'application/pdf' => '.pdf', - 'application/x-shockwave-flash' => '.swf', - 'application/x-msdownload' => '.dll', - 'application/octet-stream' => '.exe', - 'application/octet-stream' => '.msi', - 'application/octet-stream' => '.chm', - 'application/octet-stream' => '.cab', - 'application/octet-stream' => '.ocx', - 'application/octet-stream' => '.rar', - 'application/x-tar' => '.tar', - 'application/x-compressed' => '.tgz', - 'application/x-zip-compressed' => '.zip', - 'application/x-compress' => '.z', 'audio/wav' => '.wav', 'audio/x-ms-wma' => '.wma', 'video/x-ms-wmv' => '.wmv', @@ -69,14 +46,30 @@ class File 'image/png' => '.png', 'image/tiff' => '.tiff', 'image/jpeg' => '.jpg', - 'text/plain' => '.txt', - 'text/xml' => '.xml', - 'text/html' => '.html', - 'text/css' => '.css', - 'text/javascript' => '.js', - 'message/rfc822' => '.mhtml', ); + /** + * File header signatures. + * + * @var array + */ + protected static $signatures = [ + 'ffd8ff' => '.jpg', + '424d' => '.bmp', + '47494638' => '.gif', + '89504e47' => '.png', + '494433' => '.mp3', + 'fffb' => '.mp3', + 'fff3' => '.mp3', + '3026b2758e66cf11' => '.wma', + '52494646' => '.wav', + '57415645' => '.wav', + '41564920' => '.avi', + '000001ba' => '.mpg', + '000001b3' => '.mpg', + '2321414d52' => '.amr', + ]; + /** * Return steam extension. * @@ -90,6 +83,26 @@ public static function getStreamExt($stream) $mime = strstr($finfo->buffer($stream), ';', true); - return isset(self::$extensionMap[$mime]) ? self::$extensionMap[$mime] : ''; + return isset(self::$extensionMap[$mime]) ? self::$extensionMap[$mime] : self::getExtBySignature($stream); + } + + /** + * Get file extension by file header signature. + * + * @param string $stream + * + * @return string + */ + public static function getExtBySignature($stream) + { + $prefix = strval(bin2hex(mb_strcut($stream, 0, 10))); + + foreach (self::$signatures as $signature => $extension) { + if (0 === strpos($prefix, strval($signature))) { + return $extension; + } + } + + return ''; } }