【问题标题】:How to pass Laravel $request->file(...) directly to Guzzle POST request如何将 Laravel $request->file(...) 直接传递给 Guzzle POST 请求
【发布时间】:2020-08-12 06:03:49
【问题描述】:

如何通过 POST 请求将 $request->file(...) 传递给 Guzzle或者有可能吗?我应该先上传吗?

这是我的尝试:

$client = new Client([ 'base_uri' => 'http://api.domain.com']);
$response = $client->post(
        '/api/dosomething',
        [
            'multipart' => [
                [
                  'Content-type' => 'multipart/form-data',
                  'name' => 'photo_1',
                  'contents' => $request->file('photo_1')
                ]
            ]
         ]
); 

这不起作用。响应正文是空白的。

【问题讨论】:

    标签: php laravel guzzle


    【解决方案1】:

    我找到了下面的解决方法(我们需要在temp文件夹中指定临时文件的路径,代码$file->getPathName()):

    $client = new Client([ 'base_uri' => 'http://api.domain.com']);
    $file = $request->file('photo_1')
    $response = $client->post(
            '/api/dosomething',
            [
                'multipart' => [
                    [
                      'Content-type' => 'multipart/form-data',
                      'name' => 'photo_1',
                      'contents' => fopen($file->getPathName(), 'r')
                    ]
                ]
             ]
    ); 
    

    【讨论】:

      猜你喜欢
      • 2020-06-16
      • 2020-03-19
      • 2015-12-24
      • 1970-01-01
      • 2017-12-11
      • 2020-07-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多