【问题标题】:How to upload file with Guzzle client?如何使用 Guzzle 客户端上传文件?
【发布时间】:2015-01-16 17:28:19
【问题描述】:

我正在尝试对上传方法进行 BDD 测试。我在 symfony2 项目中使用 Behat 和 Mink。

现在我可以用这个客户端做简单的请求了:

$this->client = $this->mink->getSession('goutte')->getDriver()->getClient();

$this->client->request("POST", $url, array('content-type' => 'application/json'), array(), array(), $fields);

没有任何问题。

如何对文件进行请求?我试过这个:

$file = new \Symfony\Component\HttpFoundation\File\UploadedFile($path, "video");
$fields = json_encode($table->getColumnsHash()[0]);
$this->client->request("POST", $url, array('content-type' => 'multipart/form-data'), array($file), array(), $fields);

我收到的错误是:

PHP 致命错误:调用未定义的方法 GuzzleHttp\Stream\Stream::addFile()

什么是错误? 谢谢!

【问题讨论】:

    标签: symfony behat mink guzzle goutte


    【解决方案1】:

    好吧,我终于找到了答案。希望对某人有所帮助。

    上传文件,正确的方法是:

    $fields = $table->getColumnsHash()[0]; //array('name' => 'test', 'surname' => 'test');
    $fields["file"] = fopen($path, 'rb');
    $this->client->request("POST", $url, array('Content-Type => multipart/form-data'), array(), array(), $fields);
    

    诀窍是不能使用 Goutte 请求的第四个参数,但必须将所有字段作为 body 原始数据传递。

    【讨论】:

    • 你的 $table 变量是什么?
    • 这是一个 Behat\Gherkin\Node\TableNode 对象。当你使用这个 notation 时,你有一个这样的对象
    【解决方案2】:

    我不知道 Guzzle 上传,但简单的上传工作如下。您可以删除不必要的位。

    注意:我建议您将虚拟图像文件保留在项目文件夹中,因为如果有很多开发人员在同一个项目上工作,他们将具有完全相同的文件夹结构,因此可以访问图像。我见过一些人从他们的桌面上选择的图像因人而异,因此测试失败。

    files_path 下面必须指向您的项目目录,并且它应该存在,例如/var/www/html/myproject/test/build/dummy/

    behat.yml

    default:
        context:
            class: Site\FrontendBundle\Features\Context\FeatureContext
            parameters:
                output_path: %behat.paths.base%/build/behat/output/
                screen_shot_path: %behat.paths.base%/build/behat/screenshot/
        extensions:
            Behat\Symfony2Extension\Extension:
                mink_driver: true
                kernel:
                    env: test
                    debug: true
            Behat\MinkExtension\Extension:
                base_url: 'http://localhost/local/symfony/web/app_test.php/'
                files_path: %behat.paths.base%/build/dummy/
                javascript_session: selenium2
                browser_name: firefox
                goutte: ~
                selenium2: ~
        paths:
            features: %behat.paths.base%/src
            bootstrap: %behat.paths.features%/Context
    

    假设您在/var/www/html/myproject/test/build/dummy/ 文件夹下有 jpg.jpg,如下所示。

    上传功能示例:

    Feature: Create League
      In order to upload a file
      As a user
      I should be able to select and upload a file
    
      @javascript
      Scenario: I can create league
        Given I am on "/"
    
        When I attach the file "jpg.jpg" to "league_flag"
        And I press "Submit"
        Then I should see "Succeeded."
    

    【讨论】:

    • 您好,感谢您的回答。我错过了一些东西。我尝试运行,它说它无法连接到主机。我安装了 Selenium 驱动程序,我还应该安装 Selenium 服务器吗?感谢您的帮助!
    • See my answer here. 我解释了如何运行 Selenium。您只需将其下载到您的项目中并使用命令运行它。
    • 感谢您的帮助,但我终于找到了按照我的意愿完成请求的方法。
    猜你喜欢
    • 2021-01-06
    • 2020-06-07
    • 1970-01-01
    • 2010-10-26
    • 2018-03-24
    • 2016-09-21
    • 1970-01-01
    • 1970-01-01
    • 2012-02-03
    相关资源
    最近更新 更多