Home Tools

How to built Open Graph & Twitter Card Checker Tool

Open graph checker is a tool to validate the website page contains sufficient informations and it can be easily access by social media and it display as facebook post or twitter tweet in proper structure format.
open-graph-checker.php
1<?php
2//--
3$outerHead = "<head>";
4$outerHeadLength = strlen($outerHead);
5$outerHeadStart = 0;
6
7$innerHead = "</head>";
8$innerHeadLength = strlen($innerHead);
9$innerHeadStart = 0;
10//--
11
12//--
13$outerMeta = "<meta";
14$innerMeta = ">";
15$metaPointer = 0;
16//--
17
18//--
19$metaPropertyBase = "property=''";
20$metaPropertyPointer = 0;
21//--
22
23//--
24$metaContentBase = "content=''";
25$metaContentPointer = 0;
26//--
27
28$crawlOptions = array(
29CURLOPT_RETURNTRANSFER => true,     		// return web page
30CURLOPT_HEADER         => false,    		// don't return headers
31CURLOPT_FOLLOWLOCATION => true,     		// follow redirects
32CURLOPT_ENCODING       => "",       		// handle all encodings
33CURLOPT_USERAGENT      => "algoberrybot", 	// who am i
34CURLOPT_AUTOREFERER    => true,     		// set referer on redirect
35CURLOPT_CONNECTTIMEOUT => 10,      			// timeout on connect
36CURLOPT_TIMEOUT        => 10,      			// timeout on response
37CURLOPT_MAXREDIRS      => 0       			// stop after 10 redirects
38);
39
40$metaName = "";
41$metaContent = "";
42$openGraphList = array();
43$twitterCardList = array();
44$noMatchList = array();
45
46
47$openGraphMetadata = array("og:title","og:type","og:image","og:url","og:audio","og:description","og:determiner","og:locale","og:locale:alternate","og:site_name","og:video","og:image","og:image:secure_url","og:image:type","og:image:width","og:image:height","og:image:alt","og:video","og:video:secure_url","og:video:type","og:video:width","og:video:height","og:audio","og:audio:secure_url","og:audio:type","music.song","music:duration","music:album","music:album:disc","music:album:track","music:musician","music:song:disc","music:song:track","music:musician","music:release_date","music.playlist","music.radio_station","music:creator","video.movie","video:actor","video:actor:role","video:director","video:writer","video:duration","video:release_date","video:tag","video.episode","video:series","video.tv_show","video.other","article:published_time","article:modified_time","article:expiration_time","article:author","article:section","article:tag","book:author","book:isbn","book:release_date","book:tag","profile:first_name","profile:last_name","profile:username","profile:gender");
48$twitterCardMetadata = array("twitter:card","twitter:site","twitter:creator","twitter:url","twitter:title");
49
50
51function checkCategory($type) {
52	global $openGraphMetadata,$twitterCardMetadata;
53	$category = "opengraph";
54	foreach($openGraphMetadata as $key => $value) {
55		if($type == $value) {
56			return $category;
57		}
58	}
59	$category = "twittercard";
60	foreach($twitterCardMetadata as $key => $value) {
61		if($type == $value) {
62			return $category;
63		}
64	}
65	$category = "nomatch";
66	return $category;
67}
68
69function showList($base,$category) {
70	?>
71	<div style="margin-bottom:40px;">
72	<h3><?php echo $category; ?></h3>
73	<table width="100%">
74	<tr>
75		<th valign='top'>SR.No</th>
76		<th valign='top'>Tag Name</th>
77		<th valign='top'>Value</th>
78	</tr>
79	<?php
80	$count = 1;
81	foreach($base as $key => $value) {
82		if(count$value>=2) {
83			foreach($value as $innerKey => $innerValue) {
84				echo "<tr><td>".$count."</td><td>".$key."</td><td>".$innerValue."</td></tr>";
85				$count++;
86			}
87		}
88		else
89		{
90			echo "<tr><td>".$count."</td><td>".$key."</td><td>".$value[0]."</td></tr>";
91			$count++;
92		}
93	}
94	?>
95	</table>
96	</div>
97	<?php
98}
99
100$url = "";
101$validURL = "";
102if(isset$_POST["auditbutton"] && isset$_POST["url"]) {
103	$url = trim($_POST["url"]);
104	if($url != "") {
105		if(filter_var$url,FILTER_VALIDATE_URL == true) {
106			$validURL = $url; 
107		}
108	}
109	else
110	{
111		$url = " ";
112	}
113}
114?>
115<html>
116	<head>
117		<title>Open Graph & Twitter Card Checker Tool</title>
118		<style>
119		body {
120		font-family: 'Merriweather', serif;
121		font-size:14px;
122		}
123
124		h1 {
125		font-size:16px;
126		}
127
128		table {
129		border-collapse: collapse;
130		width: 100%;
131		}
132
133		th {
134		font-size:15px;
135		}
136		
137		td, th {  
138		border: 1px solid #ddd;
139		text-align: left;
140		padding: 15px;
141		}
142		
143		.error {
144		color:red;
145		}	
146		</style>
147	</head>
148	<body>
149		<h2>Open Graph & Twitter Card Checker Tool</h2>
150		<div>
151			<form action="open-graph-checker.php" method="post">
152			<input type="input" name="url" value="<?php echo $url; ?>" placeholder="https://www.algoberry.com"/>
153			<input type="submit" name="auditbutton" value="Audit"/>
154			</form>
155		</div>
156		<div style="margin-top:10px;margin-bottom:15px;">
157		<?php
158		if($validURL != "") {
159			$curlObject = curl_init($validURL);
160			curl_setopt_array($curlObject,$crawlOptions);
161			$webPageContent = curl_exec($curlObject);
162			$errorNumber = curl_errno($curlObject);
163			curl_close($curlObject);
164			if($errorNumber == 0) {
165				$webPageCounter = 0;
166				$webPageLength = strlen($webPageContent);
167				//-------- Head Session Start  --------
168				while($webPageCounter < $webPageLength) {
169					$character = $webPageContent[$webPageCounter];
170					//-------- Escape Character Start  --------
171					if($character == "") {	
172						$webPageCounter++;	
173						continue;
174					}
175					//-------- Escape Character End  --------
176					//-------- Outer Head Start  --------
177					if($outerHead[$outerHeadStart] == $character) {
178						$outerHeadStart++;
179						if($outerHeadStart == $outerHeadLength) {
180							$outerHeadStart = 0;
181							$webPageCounter++;
182							while($webPageCounter < $webPageLength) {
183								$character = $webPageContent[$webPageCounter];
184								//-------- Escape Character Start  --------
185								if($character == "") {	
186									$webPageCounter++;	
187									continue;
188								}
189								//-------- Escape Character End  --------
190								//-------- Outer Meta Start  --------
191								if($outerMeta[$metaPointer] == $character) {
192									$metaPointer++;
193									if($metaPointer == 5) {
194										$metaPointer = 0;
195										$webPageCounter++;
196										while($webPageCounter < $webPageLength) {
197											$character = $webPageContent[$webPageCounter];
198											//-------- Escape Character Start  --------
199											if($character == "") {	
200												$webPageCounter++;	
201												continue;
202											}
203											//-------- Escape Character End  --------
204											//-------- Meta Property Start  --------
205											if($metaPropertyBase[$metaPropertyPointer] == $character || $metaPropertyPointer == 9 && $character == "\"") {
206												if($metaPropertyPointer == 9) {
207													$metaPropertyPointer = 0;
208													$webPageCounter++;
209													while($webPageCounter < $webPageLength) {
210														if($webPageContent[$webPageCounter] != "'" && $webPageContent[$webPageCounter] != "\"")
211														{	$metaName .= $webPageContent[$webPageCounter]; }
212														else
213														{	break;	}
214														$webPageCounter++;
215													}
216													$metaName = trim($metaName);
217												}
218												else
219												{	$metaPropertyPointer++;	}
220											}
221											else
222											{	$metaPropertyPointer = 0;	}
223											//-------- Meta Property End  --------
224											//-------- Meta Content Start  --------
225											if($metaContentBase[$metaContentPointer] == $character || $metaContentPointer == 8 && $character == "\"") {
226												if($metaContentPointer == 8) {
227													$metaContentPointer = 0;
228													$webPageCounter++;
229													$titleStart = $webPageCounter;
230													$titleEnd = $webPageCounter;
231													while($webPageCounter < $webPageLength) {
232														if($webPageContent[$webPageCounter] != "'" && $webPageContent[$webPageCounter] != "\"")
233														{	$metaContent .= $webPageContent[$webPageCounter]; }
234														else
235														{	
236															$titleEnd = $webPageCounter - 1;
237															break;	
238														}
239														$webPageCounter++;
240													}
241													$metaContent = trim($metaContent);
242												}
243												else
244												{	$metaContentPointer++;	}
245											}
246											else
247											{	$metaContentPointer = 0;	}
248											//-------- Meta Content End  --------
249											//-------- Inner Meta Start  --------
250											if($character == $innerMeta) {
251												if($metaName != "") {
252													$metaName = strtolower($metaName);
253													$type = checkCategory($metaName);
254													if($type == "opengraph") {
255														$openGraphList[$metaName][] = $metaContent;
256													}
257													else if($type == "twittercard") {
258														$twitterCardList[$metaName][] = $metaContent;
259													} 
260													else
261													{
262														$noMatchList[$metaName][] = $metaContent;
263													}
264												}
265												$metaName = "";
266												$metaContent = "";
267												break;
268											}
269											//-------- Inner Meta End  --------
270											$webPageCounter++;
271										}
272									}
273								}
274								else
275								{	$metaPointer = 0;	}
276								//-------- Outer Meta End  --------
277								//-------- Inner Head Start  --------
278								if($innerHead[$innerHeadStart] == $character) {
279									$innerHeadStart++;
280									if($innerHeadStart == $innerHeadLength) {
281										$innerHeadStart = 0;
282										$headFound = 1;
283										break;
284									}
285								}
286								else if($innerHeadStart != 6)
287								{	$innerHeadStart = 0;	}
288								//-------- Inner Head End  --------
289								$webPageCounter++;
290							}
291							if($headFound == 1) {	
292								$webPageCounter++;
293								break;	
294							}
295						}
296					}
297					else if($outerHeadStart != 5)
298					{	$outerHeadStart = 0;	}
299					//-------- Outer Head End  --------
300					$webPageCounter++;
301				}
302				//-------- Head Session End  --------
303				
304				if(count$openGraphList>=1) {
305					showList($openGraphList,"Open Graph Tags");
306				}
307				if(count$twitterCardList>=1) {
308					showList($twitterCardList,"Twitter Card Tags");
309				}
310				if(count$noMatchList>=1) {
311					showList($noMatchList,"Miscellaneous Tags");
312				}
313			}
314			else
315			{
316			?>
317			<div class="error">Unable to access now</div>
318			<?php
319			}
320		}
321		else
322		{
323			if($url != "") {
324			?>
325			<div class="error">Please enter valid URL</div>
326			<?php
327			}	
328		}
329		?>
330		</div>
331		<div>
332		Created by <a href = "https://www.algoberry.com" target="_blank">https://www.algoberry.com</a>
333		</div>
334	</body>
335</html>