Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fetch HTML page and store it in MYSQL- How to

Tags:

html

php

mysql

  • What's the best way to store a formatted html page with CSS on to MYSQL database? Is it possible?
  • What the column type should be? How to retrieve the stored formatted HTML and display it correctly using PHP?

  • What if the page I would like to fetch has pics and videos, show I store the page as blob

  • What's the best way to fetch a page using PHP-CURL,fopen,..-?

Many questions guys but I really need your help to put me on the right way to do it.

Thanks a lot.

like image 359
codemaker Avatar asked May 03 '10 21:05

codemaker


People also ask

Can I store HTML code in database?

Certainly it is possible to store html (or whatever markup or language) inside a database. Also handling that with PHP is possible. All you have to make sure is that you escape the content so that a) your code is not open to sql injection and b) the statements are valid regardless of the contents value.

How do you store HTML codes?

Right-click within the HTML document, click File > Save As. In the Save As dialog box, specify the file name and location, then click Save.


1 Answers

Quite simple, try this code I made for you.

It's the basics to grab and save the source in a DB.

I didn't put error handling or whatever else, just keep it simple for the moment...

I didn't made the function to show the result, but you can print the $source to view the result.

Hope this will help you.

<?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);

?>
like image 174
geek1983 Avatar answered Sep 16 '22 23:09

geek1983