Advertisement
Guest User

Untitled

a guest
Feb 20th, 2019
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.84 KB | None | 0 0
  1.  
  2. function getLinkPreview($url){
  3. if($url == null || $url == ''){
  4. return [];
  5. }
  6. try {
  7.  
  8. $doc = new DOMDocument();
  9. $data = file_get_contents($url);
  10. @$doc->loadHTML($data);
  11. $rv = array(
  12. 'title' => '',
  13. 'url' => $url,
  14. 'description' => '',
  15. 'image' => '',
  16. );
  17.  
  18. // meta name="title"
  19. // meta name="og:title"
  20. // title
  21. $_title = $doc->getElementsByTagName('title');
  22. foreach($_title as $_ttl){
  23. $rv['title'] = $_ttl->textContent;
  24. }
  25.  
  26. // meta name="description"
  27. // meta name="og:description"
  28. // meta name="image"
  29. // meta name="og:image"
  30. $_meta = $doc->getElementsByTagName('meta');
  31. if($rv['description'] == ''){
  32. foreach($_meta as $_des){
  33. $_mName = $_des->getAttribute('name');
  34. if($_mName == 'description'){
  35. $rv['description'] = $_des->getAttribute('content');
  36. }
  37. }
  38. if($rv['description'] == ''){
  39. foreach($_meta as $_des){
  40. $_mName = $_des->getAttribute('property');
  41. if($_mName == 'description'){
  42. $rv['description'] = $_des->getAttribute('content');
  43. }
  44. }
  45. }
  46. if($rv['description'] == ''){
  47. foreach($_meta as $_des){
  48. $_mName = $_des->getAttribute('name');
  49. if($_mName == 'og:description'){
  50. $rv['description'] = $_des->getAttribute('content');
  51. }
  52. }
  53. }
  54. if($rv['description'] == ''){
  55. foreach($_meta as $_des){
  56. $_mName = $_des->getAttribute('property');
  57. if($_mName == 'og:description'){
  58. $rv['description'] = $_des->getAttribute('content');
  59. }
  60. }
  61. }
  62. }
  63. if($rv['image'] == ''){
  64. foreach($_meta as $_des){
  65. $_mName = $_des->getAttribute('name');
  66. if($_mName == 'image'){
  67. $rv['image'] = $_des->getAttribute('content');
  68. }
  69. }
  70. if($rv['image'] == ''){
  71. foreach($_meta as $_des){
  72. $_mName = $_des->getAttribute('property');
  73. if($_mName == 'image'){
  74. $rv['image'] = $_des->getAttribute('content');
  75. }
  76. }
  77. }
  78. if($rv['image'] == ''){
  79. foreach($_meta as $_des){
  80. $_mName = $_des->getAttribute('name');
  81. if($_mName == 'og:image'){
  82. $rv['image'] = $_des->getAttribute('content');
  83. }
  84. }
  85. }
  86. if($rv['image'] == ''){
  87. foreach($_meta as $_des){
  88. $_mName = $_des->getAttribute('property');
  89. if($_mName == 'og:image'){
  90. $rv['image'] = $_des->getAttribute('content');
  91. }
  92. }
  93. }
  94. }
  95. if($rv['image'] == ''){
  96. $_imgs = $doc->getElementsByTagName('img');
  97. if(count($_imgs) > 0){
  98. $rv['image'] = $_imgs[0]->getAttribute('src');
  99. }
  100. }
  101. $rv['title'] = str_replace("'","’",$rv['title']);
  102. $rv['description'] = str_replace("'","’",$rv['description']);
  103. return $rv;
  104.  
  105. } catch (Exception $e){
  106. $rv = array(
  107. 'title' => '',
  108. 'url' => $url,
  109. 'description' => '',
  110. 'image' => '',
  111. );
  112. return $rv;
  113. }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement