【问题标题】:Navigating forms and grabbing DOM-Elements from a website using Guzzle/Goutte (PHP, Debugging)使用 Guzzle/Goutte(PHP,调试)导航表单并从网站抓取 DOM 元素
【发布时间】:2020-02-04 01:43:46
【问题描述】:

我对 PHP 还很陌生,并且在填写一些表格后使用 Goutte/Guzzle 从网站上获取一些基本信息。

但是我在查找问题时遇到了问题(可能有很多问题),因为我找不到显示或控制台记录任何结果或问题的方法。该脚本以代码 0 结束,但不返回任何内容。关于如何打印出当前存储在 $client 中的内容的提示已经有很长的路要走。

这是我试图与一堆 cmets 一起运行以进行澄清的整个代码。很抱歉使用了这么大的块,但其中任何一个都可能有问题。

<?php

use Goutte\Client;
use GuzzleHttp\Client as GuzzleClient;

class grabPlate
{
    // WKZ
    public function checkPlate
    {
        $goutteClient = new Client();
        $guzzleClient = new GuzzleClient(array(
            'cookies'  => true,
            'timeout' => 60,
        ));
        $goutteClient->setClient($guzzleClient);

        $crawler = $goutteClient->request('GET', 'https://kfz-portal.berlin.de/kfzonline.public/start.html?oe=00.00.11.000000');

        //Click the first "Start" in the top left
        $link = $crawler
            ->filter('a:contains("Start")')
            ->eq(0)
            ->link()
        ;
        $crawler = $client->click($link);


        //Check checkbox, fill in name and press the button
        $buttonCrawlerNode = $crawler->selectButton('Weiter');
        $form = $buttonCrawlerNode->form();
        $form['gwt-uid-1']->tick();
        $form['select2-hidden-accessible']->select('Herr');
        $form['gwt-uid-4'] = 'John';
        $form['gwt-uid-5'] = 'Doe';
        $client->submit($form);

        //Fill some Data into the forms and search
        $buttonCrawlerNode = $crawler->selectButton('Button-3616');
        $form = $buttonCrawlerNode->form();
        $form['.kfzonline-KennzeichenGrossEb'] = 'AB';
        $form['.kfzonline-KennzeichenGrossEn'] = '123';
        $client->submit($form);

        //Extract collection
        $info = $crawler->extract('.collection');

        //return 1 if something is inside collection, 0 if it's empty
        if($info == NULL) {
            return 1;
        } else {
            return 0;
        }




    }
}
 ?>

正如我所说,只是在 PHPStorm 中运行脚本会返回状态 0。但是当将其插入 API 并访问它时,我会收到服务器超时响应。

【问题讨论】:

    标签: php guzzle goutte


    【解决方案1】:

    您应该使用调试。在 PHP 中安装 xDebug 以执行此操作。这也很容易集成到 Phpstorm 中。 或者使用var_dump() 将有关任何类型变量的调试信息打印到控制台。

    【讨论】:

    • 我会尝试这两个选项。感谢您的快速响应!
    【解决方案2】:

    你可以在你的php页面中显示请求的页面,你必须添加这个sn-p:

    $client = new GuzzleHttp\Client();
    $res = $client->request('GET', 'https://www.facebook.com/');
    echo $res->getBody();
    

    【讨论】:

      猜你喜欢
      • 2020-10-26
      • 2017-07-29
      • 2018-10-23
      • 2018-10-17
      • 2015-05-20
      • 2016-02-22
      • 2015-11-14
      • 2021-04-28
      • 2014-08-04
      相关资源
      最近更新 更多