douti8321 2015-08-25 20:33 采纳率: 0%
浏览 83

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

Using Behat to test some behavior that involves downloading a file. Using Goutte and Guzzle to intercept the file download so I can interact with it in another step.

//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);

It works fine, but if I run two different scenarios in a row that run this same step, I get the error:

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

Edit: I tried not closing the $handle, and then every scenario after it just skips instead of running. Also tried using $guzzleClient->getConfig()->remove('curl.options'); and that caused a later step to not work.

Edit2: Example of problem:

I took out all other steps except the one that I've included the code for here, the download of the zip file.

My feature now essentially looks like this:

 Background:
   Given I am logged in as an admin

 Scenario: A
    When I click "Export All"

 Scenario: B
    When I click "Export All"

When I run it, the output looks like this:

  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)

Followed by a stack trace that I can't find any reference to any of my code in. Full stack trace is here: http://pastebin.com/Fv48gdYm

  • 写回答

1条回答 默认 最新

  • doujichan1399 2015-08-26 14:55
    关注

    I removed the part that was setting the curl opt file, and instead just read the content of the response into the file handle.

    //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); 
    

    And now everything works perfectly.

    评论

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line