如何在 BuzzBrowser 后调用中将文件数据作为后参数发送 [英] How to send file data as post parameter in BuzzBrowser post call

查看:19
本文介绍了如何在 BuzzBrowser 后调用中将文件数据作为后参数发送的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Buzz 浏览器 来调用 POST 方法.我在 端点 之一收到来自客户端的文件.

I am using Buzz Browser for calling POST method. I am receiving a file from client at one of the end point.

$fileObj = $requestObject->files->get('image_data');

这是正确的fileObject.我已经使用 is_file() 方法验证了这一点.

This is correct fileObject. I have verified this using is_file() method.

现在,我必须使用此文件作为参数来触发 Buzz Browser 发布事件.

Now, I have to fire a Buzz Browser post event using this file as parameter.

$headers = array('Content-Type'=>'multipart/form-data'); 
$url = $genericHost . $api; //A valid URL
$params = array('image_data' => $fileObj);//fileObj obtained above

使用 buzzBrowser

$browser->post($url, $headers, $params);

并将其作为-

$fileObj = $this->get('request')->files->get('image_data');

但是,最终的 $fileObj 是 NULL.

But, the final $fileObj is NULL.

我试图转储 $this->get('request')->files-

object(Symfony\Component\HttpFoundation\FileBag)#11 (1) {
["parameters":protected]=>
     array(0) {
    }
}

表示没有文件.我的方法有什么问题?

Means no file is there. What is wrong in my approach?

1

调试多了,发现文件对象是作为普通参数发送的:

After debugging more, I found that file object is being sent as normal parameters:

["request"]=>
  object(Symfony\Component\HttpFoundation\ParameterBag)#7 (1) {
    ["parameters":protected]=>
    array(1) {
      ["image_data"]=>
      string(14) "/tmp/php6QLezs"
    }
  }

请求头为:

["headers"]=>
   object(Symfony\Component\HttpFoundation\HeaderBag)#13 (2) {
    ["headers":protected]=>
    array(7) {
      ["content-type"]=>
      array(1) {
        [0]=>
        string(70) "multipart/form-data; boundary=----------------------------1b3e33ff2ecb"
      }
      ["content-length"]=>
      array(1) {
        [0]=>
        string(3) "159"
       }
      ["host"]=>
      array(1) {
        [0]=>
        string(16) "host_name"
      }
      ["accept"]=>
        array(1) {
       [0]=>
       string(3) "*/*"
     }
     ["x-php-ob-level"]=>
      array(1) {
       [0]=>
       int(1)
     }
    }

推荐答案

最终通过Buzz Browser的 send方法解决.

use Buzz\Message\Form\FormRequest;
use Buzz\Message\Form\FormUpload; 

$request = new FormRequest();
$request->setField('image_data', new FormUpload($imageFilePath));
$request->setHeaders($headers);
$request->setMethod($method);
$request->setHost($genericHost);
$request->setResource($api);
$response= $this->browser->send($request, null);

使用Buzz Browser

这篇关于如何在 BuzzBrowser 后调用中将文件数据作为后参数发送的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆