2 回答

TA貢獻(xiàn)1786條經(jīng)驗(yàn) 獲得超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貢獻(xiàn)2036條經(jīng)驗(yàn) 獲得超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()方法將表單發(fā)送到 imgur,它會序列化表單數(shù)據(jù),服務(wù)器將收到 400 bad method 響應(yīng)。
- 2 回答
- 0 關(guān)注
- 155 瀏覽
添加回答
舉報