Search code examples
phpsymfonyhttprequestgoutte

Send post request with same parameter names in Goutte


One site I'm scraping uses the same name for two parameters so I would like to do something like this:

$params = array('dates' => '20140414', 'o' => '192382', 'o' => '213003' etc...);
$crawler = $client->request('POST', $url, $params);

However since it's not possible to have two identical keys in an array I ran into problems. Would it be possible to make such a request in Goutte (Symfony's BrowserKit)? Here is a print screen of the exact request I want to make from Chrome's network tab.

enter image description here


Solution

  • In order to do this with Goutte (or Guzzle, it has the same issue), you must construct your own form POST request instead of using $formParameters. This requires manually setting the Content-Type and sending the parameters as the request body.

    Let's say you want to send the following parameters:

    ['foo' => 1, 'bar' => 2, 'bar'=> 3, 'baz' => 4]
    

    This is what your code could look like

    $queryParams = [
        'foo=1',
        'bar=2',
        'bar=3',
        'baz=4',
    ];
    
    $content = implode('&', $queryParams);
    
    //This produces foo=1&bar=2&bar=3&baz=4
    
    /** @var Goutte\Client $client */
    $crawler = $client->request('POST', 'http://example.com/post.php', [], [], ['HTTP_CONTENT_TYPE' => 'application/x-www-form-urlencoded'], $content);
    

    Note that parameters and values must be urlencoded.