php - Symfony2 DOMCrawler selectLink returns null uri


I have a problem with writing functional tests and DOMCrawler. My issue is to crawl through mail content with link. From docs i saw that the crawler can be created with html content as parameter. So this is my chunk of code:

$mailCrawler = new Crawler($message->getBody());
$linkCrawler = $mailCrawler->selectLink('Link name');

On third line I have an exception because $linkCrawler has empty $uri field. Exception message:

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

Can anyone tell me why crawler cant get that link?

I can only tell that the $message var getBody method returns correct content.





You need to specify root crawler url. Example:

$crawler = new Crawler('', '');
    <!DOCTYPE html>
            <a href=\"/rel-link\">rel-link-text</a>
            <a href=\"\">abs-link-text</a>
", 'UTF-8');

$cLink1 = $crawler->selectLink('rel-link-text')->eq(0);
$l1 = $cLink1->link();
echo $l1->getUri(); //

$cLink2 = $crawler->selectLink('abs-link-text')->eq(0);
$l2 = $cLink2->link();
echo $l2->getUri(); //

People are also looking for solutions to the problem: internet explorer - PHP header redirection does not work on IE 8


Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.