如何使用 Laravel 上传图片到 Imgur?

How to upload images to Imgur using Laravel?

有什么方法可以使用 Laravel.

将图像上传到 Imgur

我目前正在使用

$client = new \GuzzleHttp\Client();
$response = $client->request('POST', 'https://api.imgur.com/3/image', [
    'headers' => [
        'authorization' => 'Client-ID ' . 'app_id',
        'content-type' => 'application/x-www-form-urlencoded',
    ],
    'form_params' => [
        'image' => base64_encode(file_get_contents($request->file('thumbnail')))
    ],
]);
return response()->json(json_decode(($response->getBody()->getContents())));

我的 blade 文件是

<tr>
    <td>Thumbnail</td>
    <td>
        <input type="file" name="file" id="file">
    </td>
</tr>

我不断得到 Call to a member function path() on null

我想做的是上传文件,将其上传到 Imgur 并取回 URL 以将其插入我的数据库。

试试这个,

$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');