微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

php – Behat:Goutte / Guzzle通过cURL下载文件“警告:curl_setopt_array():3607不是有效的文件句柄资源”

使用Behat测试涉及下载文件的某些行为.使用Goutte和Guzzle来拦截文件下载,这样我就可以在另一个步骤中与它进行交互.

//Where to put the file
$tmpFile = 'download.zip';
$handle  = fopen($tmpFile,'w');

$goutteDriver = $this->getSession()->getDriver();
$goutteClient = $goutteDriver->getClient();

/** @var \Guzzle\Http\Client $guzzleClient */
$guzzleClient = $goutteClient->getClient();
$guzzleClient->getConfig()->set('curl.options',[CURLOPT_FILE => $handle]);
$guzzleClient->setSslVerification(false);

$goutteDriver->visit($url);

fclose($handle);

它工作正常,但如果我连续运行两个不同的方案运行同一步骤,我得到错误

“Warning: curl_setopt_array(): 3607 is not a valid File-Handle resource”

编辑:我试图不关闭$handle,然后它之后的每个场景只是跳过而不是运行.还尝试使用$guzzleClient-> getConfig() – > remove(‘curl.options’);这导致后来的步骤不起作用.

Edit2:问题示例:

我拿出了所有其他步骤,除了我已经包含了这里的代码,下载zip文件.

我的功能现在基本上看起来像这样:

Background:
   Given I am logged in as an admin

 Scenario: A
    When I click "Export All"

 Scenario: B
    When I click "Export All"

当我运行它时,输出如下所示:

Background:                        
    Given I am logged in as an admin

  Scenario: A
    When I click "Export All" 

  Scenario: B

Warning: curl_setopt_array(): supplied argument is not a valid File-Handle resource in C:\wamp\www\cems2\vendor\guzzle\guzzle\src\Guzzle\Http\Curl\CurlHandle.PHP on line 219

Call Stack:
    0.0000     131776   1. {main}() C:\wamp\www\cems2\vendor\behat\behat\bin\behat:0
    0.0360    1699576   2. Symfony\Component\Console\Application->run() C:\wamp\www\cems2\vendor\behat\behat\bin\behat:32


When I click "Export All" (skipped)

接下来是堆栈跟踪,我找不到任何对我的任何代码的引用.完整堆栈跟踪在这里http://pastebin.com/Fv48gdYm

解决方法

删除了设置curl opt文件的部分,而只是将响应的内容读入文件句柄.

//Where to put the file
$tmpFile = 'download.zip';
$handle  = fopen($tmpFile,'w');

$goutteDriver = $this->getSession()->getDriver();
$goutteClient = $goutteDriver->getClient();

/** @var \Guzzle\Http\Client $guzzleClient */
$guzzleClient = $goutteClient->getClient();

//Remove this
//$guzzleClient->getConfig()->set('curl.options',[CURLOPT_FILE => $handle]);

$guzzleClient->setSslVerification(false);

$goutteDriver->visit($url);

//Add this
fwrite($handle,$goutteDriver->getContent());
fclose($handle);

现在一切都很完美.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐