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

Php 如何制作表格并保存url和html代码?,php,html,mysql,sql,database,Php,Html,Mysql,Sql,Database,考虑下面给出的PHP代码。此代码用于在输入网站url时获取html源代码。现在我的问题是,我想创建一个数据库,并将url和html代码保存在该表中。在下面的代码中,变量$WEBITE和$query包含url和html代码。那我该怎么做呢 <!DOCTYPE HTML> <html> <head> <style> .error {color: #FF0000;} </style> </head> <bo

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

 <!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(); 
希望你有个主意