-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathurlDetails.php
88 lines (78 loc) · 2.93 KB
/
urlDetails.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<?php
class urlPreviewDetails {
private $dom="";
public $websiteDetails=array();
private $imageArr=array();
public function __construct($url) {
$this->initializeDom($url);
}
private function initializeDom($url){
if($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);
$this->dom= new DOMDocument();
@$this->dom->loadHTML($data);
$this->websiteDetails["Url"]=$url;
return $this->dom;
}
else
{
echo "Url is empty";
}
}
function listWebsiteDetails(){
$this->websiteDetails["Title"]=$this->getWebsiteTitle();
$this->websiteDetails["Description"]=$this->getWebsiteDescription();
$this->websiteDetails["Keywords"]=$this->getWebsiteKeyword();
$this->websiteDetails["Image"]=$this->getWebsiteImages();
return json_encode($this->websiteDetails);
}
function getWebsiteTitle(){
$titleNode=$this->dom->getElementsByTagName("title");
$titleValue=$titleNode->item(0)->nodeValue;
return $titleValue;
}
function getWebsiteDescription(){
$descriptionNode=$this->dom->getElementsByTagName("meta");
for ($i=0; $i < $descriptionNode->length; $i++) {
$descriptionItem=$descriptionNode->item($i);
if($descriptionItem->getAttribute('name')=="description"){
return $descriptionItem->getAttribute('content');
}
}
}
function getWebsiteKeyword(){
$keywordNode=$this->dom->getElementsByTagName("meta");
for ($i=0; $i < $keywordNode->length; $i++) {
$keywordItem=$keywordNode->item($i);
if($keywordItem->getAttribute('name')=="keywords"){
return $keywordItem->getAttribute('content');
}
}
}
function getWebsiteImages(){
$imageNode=$this->dom->getElementsByTagName("img");
for ($i=0; $i < $imageNode->length; $i++) {
$imageItem=$imageNode->item($i);
$this->imageArr[].=$imageItem->getAttribute('src');
}
return $this->imageArr;
}
function getWebsiteOgImage(){
$descriptionNode=$this->dom->getElementsByTagName("meta");
for ($i=0; $i < $descriptionNode->length; $i++) {
$descriptionItem=$descriptionNode->item($i);
if($descriptionItem->getAttribute('property')=="og:image"){
return $descriptionItem->getAttribute('content');
}
}
}
}
$url=$_POST["url"];
$urlPreview=new urlPreviewDetails($url);
echo $urlPreview->listWebsiteDetails();