Symfony DomCrawler没有输出任何东西

时间:2015-04-08 05:21:52

标签: php guzzle

这是我正在使用的代码

$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();

它没有错误,但它也没有输出任何内容。我试过更换网址,但仍然没有输出。

1 个答案:

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