2014-12-07 42 views
0

考慮下面給出的PHP代碼。此代碼用於在輸入網站的URL時獲取html源代碼。現在我的問題是,我想做一個數據庫,並保存在該表中的網址和HTML代碼。在代碼下面的變量$網站$查詢包含的網址html代碼。那我該怎麼做呢?如何製作表格並保存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> 

回答

0

,你可以在頁面中使用下面的函數

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; 
} 

獲取當前頁面的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(); 

希望你有一個想法