phpsymfonydommeta-tagsdomcrawler

Get meta tags from url with DOM crawler


I have installed symfony/dom-crawler in my project. I am trying to get some of the meta tags from the URL of some random site to test.

$url = 'https://www.lala.rs/fun/this-news';

$crawler = new Crawler($url);

$data = $crawler->filterXpath("//meta[@name='description']")->extract(array('content'));

And it always returns [] as result.

I have tried with basic like meta description but maybe I do not understand it right. I checked the Symfony documentation but could not find the right approach for this.


Solution

  • You need to pass HTML content to the new Crawler($html) and not a URL.

    Works fine on this page, using viewport, because of missing description.

    <meta name="viewport" content="width=device-width, height=device-height, initial-scale=1.0, minimum-scale=1.0">
    
    $url = 'https://stackoverflow.com/questions/66494027/get-meta-tags-from-url-with-dom-crawler';
    $html = file_get_contents($url);
    $crawler = new Crawler($html);
    
    $data = $crawler->filterXpath("//meta[@name='viewport']")->extract(['content']);
    

    Which gives

    Array
    (
        [0] => width=device-width, height=device-height, initial-scale=1.0, minimum-scale=1.0
    )