加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – Behat:Goutte / Guzzle通过cURL下载文件“警告:curl_se

发布时间:2020-12-13 22:23:34 所属栏目:PHP教程 来源:网络整理
导读:使用Behat测试涉及下载文件的某些行为.使用Goutte和Guzzle来拦截文件下载,这样我就可以在另一个步骤中与它进行交互. //Where to put the file$tmpFile = 'download.zip';$handle = fopen($tmpFile,'w');$goutteDriver = $this-getSession()-getDriver();$gou
使用Behat测试涉及下载文件的某些行为.使用Goutte和Guzzle来拦截文件下载,这样我就可以在另一个步骤中与它进行交互.

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

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

/** @var GuzzleHttpClient $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:wampwwwcems2vendorguzzleguzzlesrcGuzzleHttpCurlCurlHandle.php on line 219

Call Stack:
    0.0000     131776   1. {main}() C:wampwwwcems2vendorbehatbehatbinbehat:0
    0.0360    1699576   2. SymfonyComponentConsoleApplication->run() C:wampwwwcems2vendorbehatbehatbinbehat: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 GuzzleHttpClient $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);

现在一切都很完美.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读