这是我正在使用的代码
$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();
它没有错误,但它也没有输出任何内容。我试过更换网址,但仍然没有输出。
答案 0 :(得分:1)
代码示例的主要问题是Symfony\Component\DomCrawler\Crawler
对象的实例化。正如目前所写,&#34; GET&#34;是$crawler
的唯一内容;因此,对$crawler->filter()
的调用返回包含空DOMNodeList的Symfony\Component\DomCrawler\Crawler
实例。这就是你输出为空的原因。
替换:
$crawler = new Crawler('GET', $event->getRequest()->getUrl());
使用:
$crawler = new Crawler(null, $event->getRequest()->getUrl());
$crawler->addContent(
$event->getResponse()->getBody(),
$event->getResponse()->getHeader('Content-Type')
);