Guzzle / Goutte - 基本刮擦 - 传递变量来请求

时间:2016-09-24 20:17:11

标签: php guzzle goutte

我目前正在使用名为Goutte的简单php抓取工具。它使用Guzzle来执行http GET请求。我能够执行刮擦动作。但是,我试图在filter内传递/回显变量,但得到错误Undefined variable: x。变量已定义。将变量传递给过滤器的正确方法是什么?

$client = new Goutte\Client();
$crawler = $client->request('GET', 'http://github.com/');
$crawler = $client->click($crawler->selectLink('Sign in')->link());
$form    = $crawler->selectButton('Sign in')->form();
$x       = "hello";
$crawler = $client->submit($form, array('login' => 'xxxxx', 'password' => 'xxxxx'));
$crawler->filter('.flash-error')->each(function ($node) {
    echo $x;
    print $node->text() . "\n";
});

1 个答案:

答案 0 :(得分:2)

尝试:

$crawler->filter('.flash-error')->each(function ($node) use (&$x) {
    echo $x;
    print $node->text() . "\n";
});

如果您不需要在该功能中更改&,则可以将其删除。 For reference to inheriting parent variables inside anonymous functions.