2

I know goutte is built on top of guzzle. Here's a sample of simultaneous HTTP requests with guzzle.

<?php
$client->send(array(
    $client->get('http://www.example.com/foo'),
    $client->get('http://www.example.com/baz'),
    $client->get('http://www.example.com/bar')
));

Can simultaneous requests be run through goutte too?

Community
  • 1
  • 1
quickshiftin
  • 66,362
  • 10
  • 68
  • 89

1 Answers1

4

Just looking over Goutte's code real quick shows that it does not support multiple requests.

However if you'd like, you can imitate Goutte by collecting the Guzzle requests and creating a new Symfony\Component\BrowserKit\Response object, which is what Goutte returns for the user to interact with.

Check out their createResponse() function (which is unfortunately protected) for more information.

<?php

// Guzzle returns an array of Responses.
$guzzleResponses = $client->send(array(
    $client->get('http://www.example.com/foo'),
    $client->get('http://www.example.com/baz'),
    $client->get('http://www.example.com/bar')
));

// Iterate through all of the guzzle responses.
foreach($guzzleResponses as $guzzleResponse) {
    $goutteObject = new Symfony\Component\BrowserKit\Response(
           $guzzleResponse->getBody(true), 
           $guzzleResponse->getStatusCode(), 
           $guzzleResponse->getHeaders()
    );

    // Do things with $goutteObject as you normally would.
}

Note, when you wait for the responses to collect in $guzzleResponses, it will wait for all of the async to complete. If you want to do immediate responses, check out the Guzzle documentation for handling async requests.

Evan Darwin
  • 850
  • 1
  • 8
  • 29
  • Thanks for this Evan! I'm totally with you on this and started hacking on Goutte myself. The changes I've come up w/ are slightly different though. I've added a `doRequestMulti` to the goutte client and `requestMulti` in the BrowserKit client (yet to get any time to test!). Clearly some of the functionality will be lost (support for cookies, 'insulated', redirects and history), but it will probably work for my use case, because I don't want to crawl past any of these multi-request pages. Let me do a little testing and circle back w/ you. – quickshiftin Jun 30 '14 at 21:44
  • @quickshiftin Yeah there's the issue with losing some of the features that Goutte provides when doing request, but it provides some of the traversing abilities for the content that it returns. (In which case renders goutte useless) – Evan Darwin Jun 30 '14 at 21:55