【发布时间】:2019-05-07 01:25:13
【问题描述】:
我正在尝试发布一个带有文件和一些字段的请求,在我使用 Guzzle http v3 之前,下面的代码正在运行,
$client = Client();
$request = $client->request('POST', $url, ['headers' => ['Authorization' => 'auth_trusted_header')]]);
/** @var \GuzzleHttp\Post\PostBody $body */
$body = $request->getBody();
$body->setField('authorId', $user->getId());
$body->setField('context', 'avatar');
$body->addFile(
new PostFile(
'file',
fopen('data://text/plain;base64,'.$avatar, 'r'),
$user->getId().'.png'
)
);
try {
$response = $client->send($request);
} catch (\Exception $e) {
$response = false;
}
升级到 V6 后,我更新了下面的代码,但不知何故它不起作用....
use GuzzleHttp\Client;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Psr7\Request as GuzzleRequest;
$client = new Client();
$request = new GuzzleRequest('POST', $url, $this->genHeadersSettings(),
[
'multipart' => [
[
'Content-type' => 'multipart/form-data',
'name' => 'file',
'contents' => fopen('data://text/plain;base64,'.$avatar, 'r'),
'filename' => $user->getId().'.png',
]
],
'form_params' => [
'authorId' => $user->getId(),
'context' => 'avatar',
]
]
);
以上代码抛出Invalid resource type: array...
有什么办法吗?
【问题讨论】:
标签: symfony curl guzzle php-7.2 guzzle6