-
Notifications
You must be signed in to change notification settings - Fork 0
/
seo.php
243 lines (204 loc) · 6.65 KB
/
seo.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
<?php
header('Content-Type: application/json');
error_reporting(E_ALL);
ini_set('display_errors', 0);
$url = $_GET['url'];
// Validate and sanitize the URL input
if (!filter_var($url, FILTER_VALIDATE_URL)) {
echo json_encode(['error' => 'Invalid URL']);
exit;
}
// Function to fetch the HTML content of a URL
function fetchHTML($url)
{
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.82 Safari/537.36');
$html = curl_exec($ch);
curl_close($ch);
return $html;
}
// Fetch the HTML content of the provided URL
$html = fetchHTML($url);
$dom = new DOMDocument();
libxml_use_internal_errors(true); // Ignore any HTML parsing errors
$dom->loadHTML($html);
libxml_use_internal_errors(false);
// Create a DOMXPath object to query the DOM
$xpath = new DOMXPath($dom);
// Function to check if the nofollow meta tag exists
function hasMetaTag($html, $attribute, $values)
{
$dom = new DOMDocument();
libxml_use_internal_errors(true);
$dom->loadHTML($html);
libxml_use_internal_errors(false);
$metaTags = $dom->getElementsByTagName('meta');
foreach ($metaTags as $metaTag) {
$attrValue = $metaTag->getAttribute($attribute);
if (in_array($attrValue, $values)) {
return true; // Meta tag exists
}
}
return false; // Meta tag does not exist
}
// Extract the domain from the provided URL
$urlParts = parse_url($url);
$domain = $urlParts['host'];
// Fetch necessary information
$title = $dom->getElementsByTagName('title')->item(0)->textContent;
$description = $xpath->evaluate('string(//meta[@name="description"]/@content)');
$description = $description ?: false;
// favicon
$favicon = $xpath->evaluate('string(//link[@rel="icon" or @rel="shortcut icon"]/@href)');
$favicon = $favicon ?: false;
$headings = [];
for ($i = 1; $i <= 6; $i++) {
$headingNodes = $xpath->query("//h{$i}");
foreach ($headingNodes as $headingNode) {
$text = trim(preg_replace('/\s+/', ' ', $headingNode->textContent));
$headings["h{$i}"][] = $text;
}
}
// imaages
$imageNodes = $xpath->query('//img');
$totalImageCount = $imageNodes->length;
$imagesWithoutAltText = [];
foreach ($imageNodes as $imageNode) {
$alt = $imageNode->getAttribute('alt');
if (empty($alt)) {
$src = $imageNode->getAttribute('src');
if (!empty($src)) {
$imagesWithoutAltText[] = $src;
}
}
}
// Server Signature
function getServerSignature($url)
{
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
$response = curl_exec($ch);
$headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$headers = substr($response, 0, $headerSize);
$serverSignature = null;
foreach (explode("\r\n", $headers) as $header) {
if (stripos($header, 'Server:') !== false) {
$serverSignature = trim(substr($header, strlen('Server:')));
break;
}
}
curl_close($ch);
return $serverSignature;
}
// Check if the website allows crawling by checking the robots.txt file
function checkRobotsTxt($url)
{
$robotsUrl = rtrim($url, '/') . '/robots.txt';
$robotsContent = fetchHTML($robotsUrl);
return !empty($robotsContent) && strpos($robotsContent, 'User-agent: *') !== false;
}
function getSPFRecord($domain)
{
$spfRecords = dns_get_record($domain, DNS_TXT);
foreach ($spfRecords as $record) {
if (stripos($record['txt'], 'v=spf1') !== false) {
return $record['txt'];
}
}
return false; // SPF record not found
}
// all function call
$spfRecord = getSPFRecord($domain);
$redirects = checkURLRedirects($url);
$serverSignature = getServerSignature($url);
$hasNoindex = hasMetaTag($html, 'name', ['noindex', 'googlebot']);
$hasNofollow = hasMetaTag($html, 'name', ['nofollow']);
// test code here
function checkURLRedirects($url)
{
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
if ($response === false) {
// Error occurred while making the request
return false;
}
$redirectUrl = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
curl_close($ch);
return $redirectUrl;
}
// Extract internal links with link text
$internalLinks = [];
$internalLinkUrls = [];
$internalLinkNodes = $xpath->query('//a[not(starts-with(@href, "#"))]');
foreach ($internalLinkNodes as $linkNode) {
$href = $linkNode->getAttribute('href');
$text = trim(preg_replace('/\s+/', ' ', $linkNode->textContent));
if (!empty($href) && !empty($text)) {
// Check if $href is an absolute URL and belongs to the same domain
if (filter_var($href, FILTER_VALIDATE_URL)) {
$parsedHref = parse_url($href);
// Check if the parsed URL matches any of the domain variations
$parsedUrlHost = isset($parsedHref['host']) ? $parsedHref['host'] : '';
$originalUrlHost = parse_url($url, PHP_URL_HOST);
$wwwOriginalUrlHost = 'www.' . $originalUrlHost;
$withoutWwwOriginalUrlHost = preg_replace('/^www\./', '', $originalUrlHost);
if (
$parsedUrlHost === $originalUrlHost ||
$parsedUrlHost === $wwwOriginalUrlHost ||
$parsedUrlHost === $withoutWwwOriginalUrlHost ||
$wwwOriginalUrlHost === $parsedUrlHost ||
$withoutWwwOriginalUrlHost === $parsedUrlHost
) {
$fullUrl = $href;
} else {
continue; // Skip external URLs
}
} else {
$base = rtrim($url, '/');
$separator = '/';
if (substr($href, 0, 1) === '/') {
$separator = '';
}
$fullUrl = $base . $separator . $href;
}
$lowercaseUrl = strtolower($fullUrl);
// Check if the lowercase URL has already been added to the array
$isInternalLink = isset($internalLinkUrls[$lowercaseUrl]);
if (!$isInternalLink) {
$internalLinks[] = [
'url' => $fullUrl,
'text' => $text
];
// Add the lowercase URL to the list of added URLs
$internalLinkUrls[$lowercaseUrl] = true;
}
}
}
// test code end here
// Prepare the response array
$response = [
'url' => $url,
'domain' => $domain,
'redirects' => $redirects,
'spfRecord' => $spfRecord,
'title' => $title,
'description' => $description,
'favicon' => $favicon,
'headings' => $headings,
'totalImages' => $totalImageCount,
'imagesWithoutAltText' => $imagesWithoutAltText,
'internalLinks' => $internalLinks,
'externalLinks' => $externalLinks,
'hasRobotsTxt' => checkRobotsTxt($url),
'hasNofollow' => $hasNofollow,
'hasNoindex' => $hasNoindex,
'serverSignature' => $serverSignature,
];
echo json_encode($response);
?>