php - 如何制作表格并保存url和html代码?

标签 php html mysql sql database

考虑下面给出的 PHP 代码。此代码用于在输入网站的 url 时获取 html 源代码。现在我的问题是我想创建一个数据库并将 url 和 html 代码保存在该表中。在下面的代码中,变量$website$query包含urlhtml代码。那么我该怎么做呢?

 <!DOCTYPE HTML> 
 <html>
 <head>
 <style>
 .error {color: #FF0000;}
 </style>
 </head>
 <body> 

 <?php
 $websiteErr = "";
 $website = "";

 if ($_SERVER["REQUEST_METHOD"] == "POST") {     
     if (empty($_POST["website"])) {
       $website = "";
 } else {
    $website = test_input($_POST["website"]);
    // check if URL address syntax is valid (this regular expression also allows dashes   in the URL)
   if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$website)) {
     $websiteErr = "Invalid URL"; 
    }
  }

  }

  function test_input($data) {
      $data = trim($data);
      $data = stripslashes($data);
      $data = htmlspecialchars($data);
      return $data;
   }
   ?>

    <form method="post"  action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">

    Website: <input type="text" name="website">
    <?php echo $websiteErr;?>
    <br><br>

    <input type="submit" name="submit" value="Submit"> 
    </form>

    <?php
    $curl_handle=curl_init();
    curl_setopt($curl_handle, CURLOPT_URL,$website);
    curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2);
    curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl_handle, CURLOPT_USERAGENT, 'Mozilla/5.0');
    $query = curl_exec($curl_handle);
    curl_close($curl_handle);
    $query =  htmlentities($query);
    echo $query;
    ?>

    </body>
    </html>

最佳答案

您可以使用以下函数获取当前页面的 url

function getUrl() {
  $url  = @( $_SERVER["HTTPS"] != 'on' ) ? 'http://'.$_SERVER["SERVER_NAME"] :   'https://'.$_SERVER["SERVER_NAME"];
  $url .= ( $_SERVER["SERVER_PORT"] !== 80 ) ? ":".$_SERVER["SERVER_PORT"] : "";
  $url .= $_SERVER["REQUEST_URI"];
  return $url;
}

在您的页面

$current_url =  getUrl();
$page_content = file_get_contents($current_url); // to get content of the page
//Or You can
ob_start();
$page_content = ob_get_content(); 

希望你有想法

关于php - 如何制作表格并保存url和html代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27346509/

相关文章:

javascript - 音频播放完毕触发事件

html - 下拉导航栏并发症

php - 如何在变量中获取行并用逗号分隔值?

mysql - JOIN 与 "just"选择

php - 在 MySQL (PHP) 中插入 CURDATE() 和 CURTIME()

PhpStorm 没有在 array_map 中显示代码提示

javascript - jQuery - 如果表 tr 中有 4 个 td 做某事

mysql - 查询适用于 Windows 上的 MySQL,但不适用于 Ubuntu 上的 Mysql

PHP:通过单击的链接在 URL 中传递变量

php - 尝试在 MYSQL 表中显示 'name' 而不是 'id'