Advertisement
ssaidz

Grab Title & Meta From Other Website [PHP]

Jun 22nd, 2016
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.16 KB | None | 0 0
  1. ac
  2. 8,38932104184 Edited
  3. Oct 28 '14 at 10:52
  4.  
  5. 15 Answers Order By  
  6. up vote
  7. 118
  8. down vote
  9. accepted
  10. This is the way it should be:
  11.  
  12. function file_get_contents_curl($url)
  13. {
  14.    $ch = curl_init();
  15.  
  16.    curl_setopt($ch, CURLOPT_HEADER, 0);
  17.    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  18.    curl_setopt($ch, CURLOPT_URL, $url);
  19.    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  20.  
  21.    $data = curl_exec($ch);
  22.    curl_close($ch);
  23.  
  24.    return $data;
  25. }
  26.  
  27. $html = file_get_contents_curl("http://example.com/");
  28.  
  29. //parsing begins here:
  30. $doc = new DOMDocument();
  31. @$doc->loadHTML($html);
  32. $nodes = $doc->getElementsByTagName('title');
  33.  
  34. //get and display what you need:
  35. $title = $nodes->item(0)->nodeValue;
  36.  
  37. $metas = $doc->getElementsByTagName('meta');
  38.  
  39. for ($i = 0; $i < $metas->length; $i++)
  40. {
  41.    $meta = $metas->item($i);
  42.    if($meta->getAttribute('name') == 'description')
  43.        $description = $meta->getAttribute('content');
  44.    if($meta->getAttribute('name') == 'keywords')
  45.        $keywords = $meta->getAttribute('content');
  46. }
  47.  
  48. echo "Title: $title". '<br/><br/>';
  49. echo "Description: $description". '<br/><br/>';
  50. echo "Keywords: $keywords";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement