我正在尝试使用guzzlehttp/guzzyv7.2上传图片。curl命令正常工作,如下所示:
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'http://31.207.77.245/services/update?id=' . $s2jsonResponse->hits[0]->id,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => array('Filedata'=> new CURLFILE($hit->metadata->path)),
CURLOPT_HTTPHEADER => array(
'Authorization: Bearer ' . $authToken
),
));
$response = curl_exec($curl);
curl_close($curl);而在使用guzzle时:
$client = new GuzzleHttp\Client();
$headers = [];
$headers['Authorization'] = 'Bearer ' . $authToken;
$request = $client->request('POST', 'http://31.207.77.245/services/update?id=' . $s2jsonResponse->hits[0]->id,
['multipart' =>[
[
'name' => 'Filedata',
'contents' => fopen($hit->metadata->filename, 'rb'),
'headers' => $headers
]
]]);
$urequest->getBody();它会失败,并显示以下错误消息:
Fatal error: Uncaught GuzzleHttp\Exception\RequestException: cURL error 55: Send failure: Broken pipe (see https://curl.haxx.se/libcurl/c/libcurl-errors.html) for http://31.207.77.245/services/update?id=B9l8TjX1a0A93YMSF6IDoH in /Users/augusto/Documents/assets/vendor/guzzlehttp/guzzle/src/Handler/CurlFactory.php:211发布于 2021-02-07 05:58:18
它的工作原理是:
$client = new GuzzleHttp\Client();
$headers = [];
$headers['Authorization'] = 'Bearer ' . $authToken;
$request = $client->request('POST', 'http://31.207.77.245/services/update?id=' . $s2jsonResponse->hits[0]->id, [
'headers' => $headers,
'multipart' => [
[
'name' => 'Filedata',
'contents' => fopen($hit->metadata->filename, 'rb')
]
]]);
$urequest->getBody();https://stackoverflow.com/questions/66057740
复制相似问题