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 關注
- 171 瀏覽
添加回答
舉報