douchou8935 2018-02-08 21:29
浏览 125
已采纳

在Laravel 5.2中使用DomCrawler获取表单

I'm working with Laravel 5.2 Commands and trying to get a form with Symfony component DomCrawler. So, with the help of DomCrawler docs and api, I have this code:

use Illuminate\Console\Command;
use GuzzleHttp\Client as GuzzleClient;
use Symfony\Component\DomCrawler\Crawler;

And, in handle() method:

$fake_body = '<html>
    <head>
    </head>
    <body>
        <div class="row search-filtro" style="    margin-top: 10px;">
            <form id="search_form" action="http://somesite.com/">

                <select class="form-control" id="slc_region" name="slc_region" form="form_busqueda" >
                    <option value="default" disabled selected style="display: none;">Ciudad</option>
                    <option value="default">Todo</option>
                    <option value="1">Región Metropolitana</option>
                    <option value="2">XV Arica y Parinacota</option>
                </select>

                <select class="form-control" id="slc_tipo" name="slc_tipo" form="form_busqueda" >
                    <option value="default" disabled selected style="display: none;">Categoría</option>
                    <option value="default">Todo</option><option value="Tiempo Libre">Tiempo Libre</option>
                    <option value="Otros">Otros</option><option value="Tecnología">Tecnología</option>
                    <option value="Salud, Deporte y Belleza">Salud, Deporte y Belleza</option>
                    <option value="Mi Casa">Mi Casa</option><option value="Infantil">Infantil</option>
                    <option value="Vestuario y Calzado">Vestuario y Calzado</option>
                </select>

                <input type="text" id="buscar_inp" name="buscar_inp" class="form-control" placeholder="Buscar Comercio..." >

                <button type="button" id="buscar_btn" class="btn btn-search btn-lg col-sm-12">BUSCAR</button>

            </form>
        </div>
    </body>
</html>
';
$site = new Crawler( $fake_body );
$form = $site->filter('form')->form();

Im programing this in Laravel Command, so when I run this in console with php artisan scrap-site, my script stops with this message error:

[InvalidArgumentException]
Current URI must be an absolute URL ("").

I already try setting action attribute of form with a relative url, absolute url, http, https, and deleting attribute but always is the same error.

Catching and tracing error message, I found in vendor/symfony/dom-crawler/AbstractUriElement.php folder the abstract class AbstractUriElement and the error is in __construct method.

/**
 * @param \DOMElement $node       A \DOMElement instance
 * @param string      $currentUri The URI of the page where the link is embedded (or the base href)
 * @param string      $method     The method to use for the link (get by default)
 *
 * @throws \InvalidArgumentException if the node is not a link
 */
public function __construct(\DOMElement $node, $currentUri, $method = 'GET')
{
    if (!in_array(strtolower(substr($currentUri, 0, 4)), array('http', 'file'))) {
        throw new \InvalidArgumentException(sprintf('Current URI must be an absolute URL ("%s").', $currentUri));
    }

    $this->setNode($node);
    $this->method = $method ? strtoupper($method) : null;
    $this->currentUri = $currentUri;
}

Make an echo for $currentUri param, it's empty!! :(

Any ideas?

  • 写回答

1条回答 默认 最新

  • douduo2407 2018-02-08 21:37
    关注

    Simply by adding root url to the crawler, you should be fine

    $fake_body = '<html>
        <head>
        </head>
        <body>
            <div class="row search-filtro" style="    margin-top: 10px;">
                <form id="search_form" action="http://somesite.com/">
    
                    <select class="form-control" id="slc_region" name="slc_region" form="form_busqueda" >
                        <option value="default" disabled selected style="display: none;">Ciudad</option>
                        <option value="default">Todo</option>
                        <option value="1">Región Metropolitana</option>
                        <option value="2">XV Arica y Parinacota</option>
                    </select>
    
                    <select class="form-control" id="slc_tipo" name="slc_tipo" form="form_busqueda" >
                        <option value="default" disabled selected style="display: none;">Categoría</option>
                        <option value="default">Todo</option><option value="Tiempo Libre">Tiempo Libre</option>
                        <option value="Otros">Otros</option><option value="Tecnología">Tecnología</option>
                        <option value="Salud, Deporte y Belleza">Salud, Deporte y Belleza</option>
                        <option value="Mi Casa">Mi Casa</option><option value="Infantil">Infantil</option>
                        <option value="Vestuario y Calzado">Vestuario y Calzado</option>
                    </select>
    
                    <input type="text" id="buscar_inp" name="buscar_inp" class="form-control" placeholder="Buscar Comercio..." >
    
                    <button type="button" id="buscar_btn" class="btn btn-search btn-lg col-sm-12">BUSCAR</button>
    
                </form>
            </div>
        </body>
    </html>
    ';
    
    $site = new Crawler( $fake_body, 'http://my-project.dev/' );
    $form = $site->filter('form')->form();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?