Fetch meta tags information from web URLs

I want to fetch meta information from web URLs

My current approach is

$url = "https://devsach.in/";
$tags = get_meta_tags($url);
echo "<pre>";
print_r($tags);

But with this I'm unable to get og: meta elements it's not mandatory to put name attribute in them but still I want them. How can I get it.

Tagged:

Answers

  • Simple approch, get it by curl

    $url = "https://devsach.in/";
    
    function file_get_contents_curl($url){
        $ch = curl_init();
    
    
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    
    
        $data = curl_exec($ch);
        curl_close($ch);
    
    
        return $data;
    }
    
    
    $html = file_get_contents_curl($url);
    
    
    //parsing begins here:
    $doc = new DOMDocument();
    @$doc->loadHTML($html);
    
    
    $nodes = $doc->getElementsByTagName('title');
    $title = $nodes->item(0)->nodeValue;
    
    
    $tags = [];
    $metas = $doc->getElementsByTagName('meta');
    foreach ($metas as $element) {
        $tag = [];
        foreach ($element->attributes as $node) {
            $tag[$node->name] = $node->value;
        }
        $tags[] = $tag;
    }
    
    print_r($tags);
    
    echo "Title: $title". '<br/><br/>';
    

    This will output

  • This will output

    Array
    (
        [0] => Array
            (
                [charset] => utf-8
            )
    
        [1] => Array
            (
                [name] => viewport
                [content] => width=device-width, initial-scale=1
            )
    
        [2] => Array
            (
                [property] => language
                [content] => en
            )
    
        [3] => Array
            (
                [property] => last-modified-date
                [http-equiv] => last-modified
                [content] => 2021-08-02
            )
    
        [4] => Array
            (
                [property] => og:type
                [content] => article
            )
    
        [5] => Array
            (
                [property] => og:site_name
                [content] => Sachin :: Welcome to devsach.in
            )
    
        [6] => Array
            (
                [property] => og:url
                [content] => https://devsach.in/
            )
    
        [7] => Array
            (
                [property] => og:description
                [name] => description
                [content] => Hello! I am Web Developer from India, New Delhi. I have rich experience in application development, web site design and development, also I am good at VoIP application development. I love to talk with you about our unique.
            )
    
        [8] => Array
            (
                [property] => og:image
                [content] => https://devsach.in/android-icon-192x192.png
            )
    
        [9] => Array
            (
                [name] => twitter:title
                [property] => og:title
                [content] => Hey there!! welcome to devsach.in
            )
    
        [10] => Array
            (
                [name] => twitter:description
                [content] => Hello! I am Web Developer from India, New Delhi. I have rich experience in application development, web site design and development, also I am good at VoIP application development. I love to talk with you about our unique.
            )
    
        [11] => Array
            (
                [name] => twitter:card
                [content] => summary
            )
    
    )
    Title: :: DevSachin
    
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!