Priya Rawat
Priya Rawat

Reputation: 259

Symfony DomCrawler is not outputing anything

This is the code I am using

$client = new Client();

$requests = [
$client->createRequest('GET', 'http://httpbin.org'),
$client->createRequest('GET', 'http://httpbin.org')
];

$options = [
'complete' => [
    [
        'fn' => function (CompleteEvent $event) {
            $crawler = new Crawler('GET',  $event->getRequest()->getUrl());
            echo '<p>'.$crawler->filter('title')->text().'</p>';
        },
        'priority' => 0,
        'once'     => false
    ]
  ]
];

$pool = new Pool($client, $requests, $options);
$pool->wait();

It gives no error but it outputs nothing either. I have tried replacing the URLs but still I get no output.

Upvotes: 0

Views: 728

Answers (1)

Shaun Bramley
Shaun Bramley

Reputation: 2047

Your primary issue with the code sample is the instantiation of your Symfony\Component\DomCrawler\Crawler object. As currently written, "GET" is the sole content of $crawler; as a result the call to $crawler->filter() returns an instance of Symfony\Component\DomCrawler\Crawler that contains an empty DOMNodeList. This is why your output is empty.

Replace:

$crawler = new Crawler('GET',  $event->getRequest()->getUrl());

with:

$crawler = new Crawler(null, $event->getRequest()->getUrl());
$crawler->addContent(
    $event->getResponse()->getBody(),
    $event->getResponse()->getHeader('Content-Type')
);

Upvotes: 1

Related Questions