diff --git a/Slack/Client.php b/Slack/Client.php index 414c877..5f3fe62 100644 --- a/Slack/Client.php +++ b/Slack/Client.php @@ -27,11 +27,12 @@ public function __construct(Connection $connection) /** * @param string $action - * @param array $parameter + * @param array $parameter + * @param bool $multipart Whether to use multipart/form-data or the default application/x-www-form-urlencoded * * @return Response|bool */ - public function send($action, array $parameter = []) + public function send($action, array $parameter = [], $multipart = false) { if (!$this->connection->isValid()) { return false; @@ -51,7 +52,8 @@ public function send($action, array $parameter = []) do { $response = $this->executeRequest( $url, - $this->connection->getHttpMethod() === Request::METHOD_POST ? $parsedRequestParams : null + $this->connection->getHttpMethod() === Request::METHOD_POST ? $parsedRequestParams : null, + $multipart ); $response = Response::parseGuzzleResponse($response, $action); @@ -89,18 +91,36 @@ protected function buildUri(Actions\ActionsInterface $action, array $requestPara /** * @param Uri $uri - * @param array $params form post values + * @param array $params form post values + * @param bool $multipart Whether to use multipart/form-data or the default application/x-www-form-urlencoded * * @return \GuzzleHttp\Psr7\Response */ - protected function executeRequest(Uri $uri, array $params = null) + protected function executeRequest(Uri $uri, array $params = null, $multipart = false) { $guzzle = new GuzzleClient(['verify' => $this->connection->getVerifySsl()]); + if (null !== $params) { + if ($multipart) { + $multipartParams = []; + foreach ($params as $paramName => $paramValue) { + $multipartParams[] = [ + 'name' => $paramName, + 'contents' => $paramValue, + ]; + } + $postParams = ['multipart' => $multipartParams]; + } else { + $postParams = ['form_params' => $params]; + } + } else { + $postParams = []; + } + return $guzzle->request( $this->connection->getHttpMethod(), $uri, - null !== $params ? ['form_params' => $params] : [] + $postParams ); } } diff --git a/Slack/Client/Actions/FilesUpload.php b/Slack/Client/Actions/FilesUpload.php index 661d795..0b83f2b 100644 --- a/Slack/Client/Actions/FilesUpload.php +++ b/Slack/Client/Actions/FilesUpload.php @@ -14,7 +14,7 @@ class FilesUpload implements ActionsInterface * @var array */ protected $parameter = [ - 'content' => null, + 'file' => null, 'filetype' => null, 'filename' => null, 'title' => null, diff --git a/Slack/Messaging.php b/Slack/Messaging.php index 291348e..24ad269 100644 --- a/Slack/Messaging.php +++ b/Slack/Messaging.php @@ -72,16 +72,16 @@ public function message($channel, $message, $identity, array $attachments = [], ); } - /** * @param string|array $channel # String is DEPRECATED scince v1.3 - Deliver Array * @param string $title * @param string $file * @param string|null $comment + * @param string|null $threadTs * * @return Client\Response|false */ - public function upload($channel, $title, $file, $comment = null) + public function upload($channel, $title, $file, $comment = null, $threadTs = null) { if (!file_exists($file)) { return false; @@ -96,13 +96,14 @@ public function upload($channel, $title, $file, $comment = null) $params['title'] = $title; $params['initial_comment'] = $comment; $params['channels'] = implode(',', $channel); - $params['content'] = file_get_contents($file); - $params['fileType'] = mime_content_type($file); $params['filename'] = basename($file); + $params['file'] = fopen($file, 'r'); + $params['thread_ts'] = $threadTs; return $this->client->send( Actions::ACTION_FILES_UPLOAD, - $params + $params, + true ); } }