获取HTML页面并将其存储在MYSQL中 - 如何

cod*_*ker 13 html php mysql

  • 将带有CSS的格式化html页面存储到MYSQL数据库的最佳方法是什么?可能吗?
  • 列类型应该是什么?如何检索存储的格式化HTML并使用PHP正确显示?

  • 如果我想要获取的页面有图片和视频,请显示我将页面存储为blob

  • 使用PHP-CURL,fopen,..-获取页面的最佳方法是什么?

很多问题的人,但我真的需要你的帮助,让我以正确的方式做到这一点.

非常感谢.

小智 8

很简单,试试我为你制作的代码.

这是在数据库中获取和保存源代码的基础知识.

我没有把错误处理或其他任何东西,只是暂时保持简单......

我没有让函数显示结果,但你可以打印$ source来查看结果.

希望这会帮助你.

<?php

function GetPage($URL)
{
    #Get the source content of the URL
    $source = file_get_contents($URL);

    #Extract the raw URl from the current one
    $scheme = parse_url($URL, PHP_URL_SCHEME); //Ex: http
    $host = parse_url($URL, PHP_URL_HOST); //Ex: www.google.com
    $raw_url = $scheme . '://' . $host; //Ex: http://www.google.com

    #Replace the relative link by an absolute one
    $relative = array();
    $absolute = array();

    #String to search
    $relative[0] = '/src="\//';
    $relative[1] = '/href="\//';

    #String to remplace by
    $absolute[0] = 'src="' . $raw_url . '/';
    $absolute[1] = 'href="' . $raw_url . '/';

    $source = preg_replace($relative, $absolute, $source); //Ex: src="/image/google.png" to src="http://www.google.com/image/google.png"

    return $source;
}

function SaveToDB($source)
{
    #Connect to the DB
    $db = mysql_connect('localhost', 'root', '');

    #Select the DB name
    mysql_select_db('test');

    #Ask for UTF-8 encoding
    mysql_query("SET NAMES 'utf8'");

    #Escape special chars
    $source = mysql_real_escape_string($source);

    #Set the Query
    $query = "INSERT INTO website (source) VALUES ('$source')"; //Save it in a text row, that's it...

    #Run the query
    mysql_query($query);

    #Close the connection
    mysql_close($db);
}

$source = GetPage('http://www.google.com');

SaveToDB($source);

?>
Run Code Online (Sandbox Code Playgroud)