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:
0
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
0 -
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
0
Howdy, Stranger!
Categories
- 94 All Categories
- 20 VoIP
- 7 SIP
- 16 asterisk
- 44 Programming
- 1 Nodejs
- 4 javascript
- 19 PHP
- 8 Codeigniter
- 14 database
- 1 UI/UX
- 2 Flutter
- 28 OS
- 26 Linux
- 1 Virtualization
- 1 Android
- 1 Windows
- 2 legal