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

rn0zuynd  于 2023-03-16  发布在  PHP
关注(0)|答案(1)|浏览(103)

我现在正在使用一个简单的php crawler叫做Goutte。它使用Guzzle来执行http GET的请求。我可以执行抓取操作。但是,我试图在filter内部传递/回显一个变量,但是得到了错误Undefined variable: x。变量已经定义了。传递变量到filter的正确方法是什么?

$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";
});
zysjyyx4

zysjyyx41#

试试看:

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

如果你不需要在那个函数中修改&,你可以删除它。关于在匿名函数中继承父变量的参考。

相关问题