Skip to content

Instantly share code, notes, and snippets.

@vmanyushin
Created February 20, 2015 17:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vmanyushin/b385ce215e572d11e7db to your computer and use it in GitHub Desktop.
Save vmanyushin/b385ce215e572d11e7db to your computer and use it in GitHub Desktop.
PHP Dom parser
#!/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