【问题标题】:PHP GuzzleHttp . how to upload the image file using guzzlehttp in laravel 5.2?PHP GuzzleHttp 。如何在 laravel 5.2 中使用 guzzlehttp 上传图像文件?
【发布时间】:2017-01-06 06:45:28
【问题描述】:

如何使用 GuzzleHttp(6.0 版)发出 post 请求。我正在尝试执行以下操作并收到错误

我得到了图像值

Illuminate\Http\UploadedFile Object
(
    [test:Symfony\Component\HttpFoundation\File\UploadedFile:private] => 
    [originalName:Symfony\Component\HttpFoundation\File\UploadedFile:private] => 1.53mb.jpg
    [mimeType:Symfony\Component\HttpFoundation\File\UploadedFile:private] => image/jpeg
    [size:Symfony\Component\HttpFoundation\File\UploadedFile:private] => 1607671
    [error:Symfony\Component\HttpFoundation\File\UploadedFile:private] => 0
    [pathName:SplFileInfo:private] => C:\wamp\tmp\php32BB.tmp
    [fileName:SplFileInfo:private] => php32BB.tmp
)

这里我使用 guzzlehttp 上传图片

$response = $this->client->request('POST', url('/update_profile'), [
    'multipart' => [
        [
            'name'     => 'foo',
            'contents' => fopen('C:\wamp\tmp\php32BB.tmp', 'r'),//this path is image save temperary path
        ]
    ]
]);

现在我得到了错误 fopen(C:\wamp\tmp\php17BC.tmp): 无法打开流: 没有这样的文件或目录。

如何在多部分中使用内容

【问题讨论】:

  • 您是否创建了 C:\wamp\tmp 文件夹?如果目录不存在,fopen 不会为您创建目录。
  • 我解决了这个问题 $image_path = $post_array['image']->getPathname(); $image_mime = $post_array['image']->getmimeType(); $image_org = $post_array['image']->getClientOriginalName(); $response = $this->client->post(url('/update_profile'), [ 'multipart' => [ [ 'name' => 'image', 'filename' => $image_org, 'Mime-Type' => $image_mime, '内容' => fopen( $image_path, 'r' ), ], ] ]);

标签: laravel-5.2 guzzle


【解决方案1】:

我解决了这个问题

$image_path = $post_array['image']->getPathname();
$image_mime = $post_array['image']->getmimeType();
$image_org  = $post_array['image']->getClientOriginalName();

$response = $this->client->post(url('/update_profile'), [
            'multipart' => [
                [
                    'name'     => 'image',
                    'filename' => $image_org,
                    'Mime-Type'=> $image_mime,
                    'contents' => fopen( $image_path, 'r' ),
                ],
            ]
        ]);

【讨论】:

  • 您如何检索该文件?
  • 谢谢你!我真的不知道如何从 UploadFile 对象开始并以格式正确的 Guzzle Request 结束!
猜你喜欢
  • 1970-01-01
  • 2017-05-13
  • 1970-01-01
  • 1970-01-01
  • 2016-05-09
  • 1970-01-01
  • 2017-04-20
  • 2017-09-08
  • 1970-01-01
相关资源
最近更新 更多