【问题标题】:Upload file through API通过 API 上传文件
【发布时间】:2021-07-22 17:19:00
【问题描述】:

我正在使用如下 API 请求更新数据。

$response = Http::withToken(Session::get('SesTok'))->post('https://sometext/profile-update', [
                "first_name" => $request->first_name,
                "last_name" => $request->last_name,
                "email" => $request->profile_email,       
            ]);

现在我需要使用 API 请求上传 文件。我可以使用$request->file('logo') 获取文件详细信息。

如何通过 API 上传文件?

【问题讨论】:

    标签: laravel api


    【解决方案1】:

    使用Http::attach方法

        $response = Http::attach(
        'attachment', file_get_contents('photo.jpg'), 'photo.jpg'
        )->post('http://example.com/attachments');
    

    【讨论】:

    • 谢谢@Shahrukh。我应该这样使用吗? $response = Http::attach( 'attachment', file_get_contents($request->file('logo')), 'photo.jpg' )->post('http://example.com/attachments');
    • 使用file_get_contents($request->file('logo')->getRealPath());
    猜你喜欢
    • 2011-10-18
    • 2012-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-26
    • 2013-09-01
    • 2023-03-10
    • 1970-01-01
    相关资源
    最近更新 更多