2 回答

TA贡献1786条经验 获得超13个赞
尝试这个,
$file = $request->file('file');
$file_path = $file->getPathName();
$client = new \GuzzleHttp\Client();
$response = $client->request('POST', 'https://api.imgur.com/3/image', [
'headers' => [
'authorization' => 'Client-ID ' . 'your-client-id-here',
'content-type' => 'application/x-www-form-urlencoded',
],
'form_params' => [
'image' => base64_encode(file_get_contents($request->file('file')->path($file_path)))
],
]);
$data['file'] = data_get(response()->json(json_decode(($response->getBody()->getContents())))->getData(), 'data.link');

TA贡献2036条经验 获得超8个赞
Laravel 9 及以上答案的更新,
use Illuminate\Support\Facades\Http;
$file = $request->file('file');
$file_path = $file->getPathName();
$response = Http::withHeaders([
'authorization' => 'Client-ID ' . 'your-client-id-here',
'content-type' => 'application/x-www-form-urlencoded',
])->send('POST', 'https://api.imgur.com/3/image', [
'form_params' => [
'image' => base64_encode(file_get_contents($request->file('file')->path($file_path)))
],
]);
$data['file'] = data_get(response()->json(json_decode(($response->getBody()->getContents())))->getData(), 'data.link');
不要使用 http Facadepost()方法将表单发送到 imgur,它会序列化表单数据,服务器将收到 400 bad method 响应。
- 2 回答
- 0 关注
- 127 浏览
添加回答
举报