Advertisement
Guest User

PHP read Meta-TAGs

a guest
Mar 6th, 2013
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.22 KB | None | 0 0
  1. <?php
  2. // ---------------------------------------------------------------
  3.  
  4. header("Content-Type: text/html; charset=utf-8");
  5.  
  6. error_reporting(E_ALL);
  7. ini_set("display_errors", 1);
  8.  
  9. ## $myscript = 'GetMeta1.php';
  10. $myscript = $_SERVER['PHP_SELF'];
  11.  
  12.  
  13. // ------------------------------------------------------------
  14.  
  15. print "<br />\n";
  16. print "<br /> --- <a href=\"".$myscript."\">HOME</a> (RESET) --- \n";
  17. print "<br />\n";
  18.  
  19. // ----------------------------------------------------
  20.  
  21. if (!empty($_REQUEST['url'])) {
  22.     $go_url = trim(chop($_REQUEST['url']));
  23.     $go_url = str_replace('http://http://','http://',$go_url);
  24.     if (strpos($go_url,'http://') === FALSE && strpos($go_url,'https://') === FALSE) { $go_url = 'http://'.$go_url; }
  25.     // ----- to-do: filters and/or escape url string -----
  26. }
  27. else {
  28.     $go_url = 'http://';
  29. }
  30.  
  31. // ----------------------------------------------------
  32.  
  33. function pingSite($url) {  
  34.    
  35.     $fp = curl_init($url);
  36.    
  37.     curl_setopt($fp,CURLOPT_TIMEOUT,10);
  38.     curl_setopt($fp,CURLOPT_FAILONERROR,1);
  39.     curl_setopt($fp,CURLOPT_RETURNTRANSFER,0);
  40.     curl_setopt($fp,CURLOPT_NOBODY,1);
  41.  
  42.     curl_exec($fp);
  43.    
  44.     if (curl_errno($fp) != 0) { $status = false; }
  45.     else { $status = true; }
  46.    
  47.     curl_close($fp);
  48.    
  49.     return $status;
  50.  
  51. }  
  52.  
  53. // ----------------------------------------------------
  54.  
  55.  
  56. $online = pingSite($go_url);
  57.  
  58. if ( !$online && !empty($go_url) && $go_url != 'http://' ) {
  59.  
  60. print "<br />\n";
  61. print "<br /> ........ Die URL <span style=\"margin:8px; padding:4px; background:#DDEEDD;\">".$go_url."</span> ist <strong style=\"color:red;\">nicht</strong> online !!! ... <a href=\"".$myscript."\">zur&uuml;ck</a>\n";
  62. print "<br />\n";
  63. }
  64. else {
  65.  
  66. // ------------------------------------------------------------
  67.  
  68. if ( !empty($_REQUEST['save']) && !empty($go_url) && $go_url != 'http://' ) {
  69.  
  70. print "<br />\n";
  71. print "<br /> To-Do ........ Speichern \n";
  72. print "<br />\n";
  73. print "<br /> ###### DEBUG ###### _POST ###### \n";
  74. print "<br />\n";
  75. print "<br />\n";
  76.  
  77. print "<pre style=\"margin:4px; padding:4px; background:#DEDEDE; text-align:left;\">\n";
  78. print_r($_POST);
  79. print "</pre>\n";
  80.  
  81. print "<br />\n";
  82. print "<br />\n";
  83. print "<br />\n";
  84.  
  85. }
  86. else {
  87. // ------------------------------------------------------------
  88.  
  89. $formular1 = '
  90. <form name="form1" method="post" action="">
  91.  <br /> URL: <input type="text" name="url" size="48" maxlength="180" value="'.$go_url.'" />
  92.  <br />
  93.  <br /> RUN: <input type="submit" name="meta" value="Get MetaTags" /> &nbsp;&nbsp; INFO: <span id="minfo" style="margin:12px; padding:4px; background:#DDEEDD;">Bitte eine URL eingeben.</span>
  94.  <br />
  95.  <br /> Titel: <input type="text" name="title" size="48" maxlength="90" />
  96.  <br />
  97.  <br /> Beschreibung:
  98.  <br /> <textarea name="description" cols="48" rows="4" wrap="VIRTUAL"></textarea>
  99.  <br />
  100.  <br /> Keywords:
  101.  <br /> <textarea name="keywords" cols="48" rows="4" wrap="VIRTUAL"></textarea>
  102.  <br />
  103.  <br /> OK:  <input type="submit" name="save" value="Speichern" />
  104.  <br />
  105. </form>
  106. ';
  107.  
  108.  
  109. // ----------------------------------------------------
  110.  
  111. print "<br />\n";
  112. print "<br />\n";
  113.  
  114. print $formular1;
  115.  
  116. print "<br />\n";
  117. print "<br />\n";
  118.  
  119. }
  120.  
  121.  
  122. // ----------------------------------------------------
  123.  
  124.  
  125. function str_utf8_encode($str) {
  126.     if (mb_detect_encoding($str, 'UTF-8', true) === FALSE) {
  127.     $str = utf8_encode($str);
  128.     }
  129.     return $str;
  130. }
  131.  
  132. // ----------------------------------------------------------------
  133. // ----------------------------------------------------------------
  134.  
  135. function get_meta_description($url) {
  136.  
  137. $html = '';
  138.  
  139. if($fh = @fopen($url,"r")){
  140.     while (!feof($fh)){
  141.         $buffer = fgets($fh, 4096);
  142.         if (strpos($buffer,'/head') !== FALSE) { break; }
  143.         $html .= $buffer;
  144.     }
  145.     fclose($fh);
  146. }
  147. else {
  148. print "<br />ERROR ... fopen\n";
  149. exit;
  150. }
  151.  
  152. // --------------------------------------------
  153.  
  154. $pattern1 = '/<title>(.*)<\/title>/iU';
  155. preg_match_all ($pattern1, $html, $matches_title);
  156.  
  157. ## print "<pre>\n";
  158. ## print_r($matches_title);
  159. ## print "</pre>\n";
  160.  
  161. // --------------------------------------------
  162.  
  163. $pattern2 = '/<meta name=(["\']?)(description|keywords)\\1\\s+content=(["\']?)(.+?)\\3[ \/>]/i';
  164. preg_match_all ($pattern2, $html, $matches_meta, PREG_SET_ORDER);
  165.  
  166. ## print "<pre>\n";
  167. ## print_r($matches_meta);
  168. ## print "</pre>\n";
  169.  
  170. // --------------------------------------------
  171.  
  172. $matchdata = array();
  173.  
  174. $matchdata['title'] = (isset($matches_title[1][0])) ? str_utf8_encode($matches_title[1][0]) : '';
  175.  
  176. foreach($matches_meta as $matchitem) {  
  177.     $mkey = strtolower($matchitem[2]);
  178.     $matchdata[$mkey] = (isset($matchitem[4])) ? str_utf8_encode($matchitem[4]) : '';
  179. }
  180.  
  181. // --------------------------------------------
  182.  
  183. return $matchdata;
  184.  
  185.  
  186. }
  187.  
  188. // ----------------------------------------------------------------
  189. // ----------------------------------------------------------------
  190.  
  191.  
  192. // ----------------------------------------------------
  193. if ( !empty($_REQUEST['meta']) ) {
  194.  
  195.  
  196. // ---------------------------------
  197. if ( !empty($go_url) && $go_url != 'http://' ) {
  198.  
  199. $data = get_meta_description($go_url);
  200.  
  201.  
  202.  
  203. print "<p>###### DEBUG ######</p>\n";
  204. print "<br />URL: ".$go_url."\n";
  205. print "<br />\n";
  206.  
  207. print "<pre style=\"margin:4px; padding:4px; background:#DEDEDE; text-align:left;\">\n";
  208. print_r($data);
  209. print "</pre>\n";
  210.  
  211. print "<br />\n";
  212. print "<br />\n";
  213.  
  214.  
  215. $jscode = '';
  216.  
  217. $jscode .= '
  218. <script type="text/javascript">
  219. ';
  220.  
  221. $jscode .= '
  222. document.form1.title.value = "'.$data['title'].'";
  223. document.form1.description.value = "'.$data['description'].'";
  224. document.form1.keywords.value = "'.$data['keywords'].'";
  225. ';
  226.  
  227. $jscode .= '
  228. document.getElementById("minfo").innerHTML = "OK ... MetaTags wurden gelesen.";
  229. ';
  230.  
  231. $jscode .= '
  232. </script>
  233. ';
  234.  
  235.  
  236. print $jscode;
  237.  
  238.  
  239. }
  240. else {
  241. // ---------------------------------
  242.  
  243. print '
  244. <script type="text/javascript">
  245. document.getElementById("minfo").innerHTML = "Kannst Du nicht Lesen? ... zuerst eine URL eintragen !!!";
  246. </script>
  247. ';
  248.  
  249. }
  250. // ---------------------------------
  251.  
  252. }
  253. // ----------------------------------------------------
  254.  
  255. }
  256. // ------------------------------------------------------------
  257. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement