Created
February 20, 2015 17:14
-
-
Save vmanyushin/b385ce215e572d11e7db to your computer and use it in GitHub Desktop.
PHP Dom parser
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/php | |
<?php | |
$url = "http://habrahabr.ru/post/242657/"; | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, $url); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5); | |
$dom = new DOMDocument(); | |
$dom->loadHTML(curl_exec($ch)); | |
$domxpath = new DOMXPath($dom); | |
$meta_description = $domxpath->query('//meta[@name="description"]')->item(0)->getAttribute('content'); | |
$meta_keywords = $domxpath->query('//meta[@name="keywords"]')->item(0)->getAttribute('content'); | |
$page_title = $domxpath->query('//title')->item(0)->nodeValue; | |
echo "meta description: $meta_description\n"; | |
echo "meta keywords : $meta_keywords \n"; | |
echo "title : $page_title\n"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment