getting meta tags info using curl and get_meta_tags()

前端 未结 2 459
别跟我提以往
别跟我提以往 2021-01-27 11:11

is there a way to use curl such that you can do something that is equivalent to the get_meta_tags() function in php? specifically to get the meta tags of an external site using

相关标签:
2条回答
  • 2021-01-27 11:33
    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("http://www.example.com");
    
    //parsing begins here:
    $doc = new DOMDocument();
    @$doc->loadHTML($html);
    $nodes = $doc->getElementsByTagName('title');
    
    //get and display what you need:
    $title = $nodes->item(0)->nodeValue;
    
    $metas = $doc->getElementsByTagName('meta');
    
    for ($i = 0; $i < $metas->length; $i++)
    {
    $meta = $metas->item($i);
    if($meta->getAttribute('name') == 'description')
        $description = $meta->getAttribute('content');
    if($meta->getAttribute('name') == 'keywords')
        $keywords = $meta->getAttribute('content');
    if($meta->getAttribute('language') == 'language');
        $language = $meta->getAttribute('language');
    }
    
    echo "Title: $title". '<br/><br/>';
    echo "Description: $description". '<br/><br/>';
    echo "Keywords: $keywords";
    
    0 讨论(0)
  • 2021-01-27 11:43

    is there a way to use curl such that you can do something that is equivalent to the get_meta_tags() function in php

    Nope, I don't think so.

    The best way would be to fetch the data, and parse it using a HTML parser. Alternatively, there are several regex based approaches in the user contributed notes in the manual.

    0 讨论(0)
提交回复
热议问题