【问题标题】:How to upload images to Imgur using Laravel?如何使用 Laravel 将图像上传到 Imgur?
【发布时间】:2020-11-13 12:56:38
【问题描述】:

有什么方法可以使用 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())));

我的刀片文件是

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

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

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

【问题讨论】:

  • 您是否尝试在代码中使用一些dumps?如果你转储$request-&gt;file('thumbnail')怎么办?
  • 谢谢。我刚刚检查了我的刀片文件。我在请求中使用了“文件”而不是“缩略图”。现在如何从响应中获取 URL?
  • Guzzle 请求的响应是什么?你能试试dump(json_decode($response-&gt;getBody()-&gt;getContents(), true));
  • 另一个错误。 GuzzleHttp\Exception\ClientException Client error: POST api.imgur.com/3/image` 导致 403 Permission Denied 响应:{"data":{"error":"Malformed auth header","re​​quest":"\/3\/image","method":" POST"},"success":false,"status":403}` 我正在使用标头进行身份验证。
  • 我已经对标题错位的问题进行了排序

标签: php laravel image upload imgur


【解决方案1】:

试试这个,

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

【讨论】:

    猜你喜欢
    • 2016-04-25
    • 1970-01-01
    • 2011-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-23
    • 2021-06-11
    相关资源
    最近更新 更多