Skip to content

Commit

Permalink
Merge pull request #9 from qbhy/master
Browse files Browse the repository at this point in the history
修复文件上传问题
  • Loading branch information
Hanson authored Sep 12, 2019
2 parents b81e458 + 8af743d commit 665decc
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 17 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
composer.phar
/vendor/
/.idea/
composer.lock

# Commit your application's lock file http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file
# You may choose to ignore a library lock file http://getcomposer.org/doc/02-libraries.md#lock-file
Expand Down
47 changes: 30 additions & 17 deletions src/Http.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public function get($url, array $options = [])
* POST request.
*
* @param string $url
* @param array $form
* @param array $form
*
* @return ResponseInterface
*/
Expand All @@ -95,22 +95,22 @@ public function post($url, array $form = [])
* JSON request.
*
* @param string $url
* @param $query
* @param $query
*
* @return ResponseInterface
*/
public function json($url, $query = [])
{
return $this->request('POST', $url, ['json' => $query]);
}
public function json($url, $query = [])
{
return $this->request('POST', $url, ['json' => $query]);
}

/**
* Upload file.
*
* @param string $url
* @param array $files
* @param array $form
* @param array $queries
* @param array $files
* @param array $form
* @param array $queries
*
* @return ResponseInterface
*/
Expand All @@ -119,16 +119,16 @@ public function upload($url, array $queries = [], array $files = [], array $form
$multipart = [];

foreach ($files as $name => $path) {
if (is_array($path)){
if (is_array($path)) {
foreach ($path as $item) {
$multipart[] = [
'name' => $name . '[]',
] + $item;
] + $this->fileToMultipart($item);
}
}else{
} else {
$multipart[] = [
'name' => $name,
] + $path;
] + $this->fileToMultipart($path);
}
}

Expand All @@ -139,6 +139,19 @@ public function upload($url, array $queries = [], array $files = [], array $form
return $this->request('POST', $url, ['query' => $queries, 'multipart' => $multipart]);
}

private function fileToMultipart($file)
{
if (is_array($file)) {
return $file;
} elseif (@file_exists($file)) {
return ['contents' => fopen($file, 'r')];
} elseif (filter_var($file, FILTER_VALIDATE_URL)) {
return ['contents' => file_get_contents($file)];
} else {
return ['contents' => $file];
}
}

/**
* Set GuzzleHttp\Client.
*
Expand Down Expand Up @@ -196,7 +209,7 @@ public function getMiddlewares()
*
* @param string $url
* @param string $method
* @param array $options
* @param array $options
*
* @return ResponseInterface
*/
Expand All @@ -213,10 +226,10 @@ public function request($method, $url, $options = [])
$response = $this->getClient()->request($method, $url, $options);

Log::debug('API response:', [
'Status' => $response->getStatusCode(),
'Reason' => $response->getReasonPhrase(),
'Status' => $response->getStatusCode(),
'Reason' => $response->getReasonPhrase(),
'Headers' => $response->getHeaders(),
'Body' => strval($response->getBody()),
'Body' => strval($response->getBody()),
]);

return $response;
Expand Down

0 comments on commit 665decc

Please sign in to comment.