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?