Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Fixes problem with multi-dimensional files array #74

Merged
merged 1 commit into from

2 participants

@hason

fixes #37

@fabpot fabpot referenced this pull request from a commit
@fabpot fabpot merged branch hason/nested_files (PR #74)
Commits
-------

b207ffc Fixes problem with multi-dimensional files array

Discussion
----------

Fixes problem with multi-dimensional files array

fixes #37
6d26279
@fabpot fabpot merged commit b207ffc into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Aug 16, 2012
  1. @hason
This page is out of date. Refresh to see the latest.
Showing with 45 additions and 9 deletions.
  1. +21 −9 Goutte/Client.php
  2. +24 −0 Goutte/Tests/ClientTest.php
View
30 Goutte/Client.php
@@ -19,6 +19,7 @@
use Guzzle\Http\Message\Response as GuzzleResponse;
use Guzzle\Http\ClientInterface as GuzzleClientInterface;
use Guzzle\Http\Client as GuzzleClient;
+use Guzzle\Http\Message\EntityEnclosingRequestInterface;
/**
* Client.
@@ -112,15 +113,7 @@ protected function doRequest($request)
}
if ('POST' == $request->getMethod()) {
- $postFiles = array();
- foreach ($request->getFiles() as $name => $info) {
- if (isset($info['tmp_name']) && '' !== $info['tmp_name']) {
- $postFiles[$name] = $info['tmp_name'];
- }
- }
- if (!empty($postFiles)) {
- $guzzleRequest->addPostFiles($postFiles);
- }
+ $this->addPostFiles($guzzleRequest, $request->getFiles());
}
$guzzleRequest->getCurlOptions()
@@ -144,6 +137,25 @@ protected function doRequest($request)
return $this->createResponse($response);
}
+ protected function addPostFiles($request, array $files, $arrayName = '')
+ {
+ if (!$request instanceof EntityEnclosingRequestInterface) {
+ return;
+ }
+
+ foreach ($files as $name => $info) {
+ if (!empty($arrayName)) {
+ $name = $arrayName . '[' . $name . ']';
+ }
+
+ if (isset($info['tmp_name']) && '' !== $info['tmp_name']) {
+ $request->addPostFile($name, $info['tmp_name']);
+ } elseif (is_array($info)) {
+ $this->addPostFiles($request, $info, $name);
+ }
+ }
+ }
+
protected function createResponse(GuzzleResponse $response)
{
return new Response($response->getBody(true), $response->getStatusCode(), $response->getHeaders()->getAll());
View
24 Goutte/Tests/ClientTest.php
@@ -122,6 +122,30 @@ public function testUsesPostFiles()
), $request->getPostFiles());
}
+ public function testUsesPostFilesNestedFields()
+ {
+ $guzzle = $this->getGuzzle();
+ $client = new Client();
+ $client->setClient($guzzle);
+ $files = array(
+ 'form' => array(
+ 'test' => array(
+ 'name' => 'test.txt',
+ 'tmp_name' => __FILE__
+ ),
+ ),
+ );
+
+ $crawler = $client->request('POST', 'http://www.example.com/', array(), $files);
+ $request = $this->historyPlugin->getLastRequest();
+
+ $this->assertEquals(array(
+ 'form[test]' => array(
+ new PostFile('form[test]', __FILE__, 'text/x-php')
+ )
+ ), $request->getPostFiles());
+ }
+
public function testUsesCurlOptions()
{
$guzzle = $this->getGuzzle();
Something went wrong with that request. Please try again.