简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM