0

How do I get the page source of the html page using php codes and then save all all of those codes into a database?

Is it possible?

Please help. Thanks.

Database is MySQL

2
  • Which HTML page? The one you're generating? One from an external HTTP server? Commented Jan 12, 2010 at 2:56
  • Really? This isn't a duplicate? Commented Jan 12, 2010 at 3:26

2 Answers 2

2

The answer lies within file_get_contents() or cURL.

$string = file_get_contents('http://www.example.com/');
echo $string;

// create a new cURL resource
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "http://www.example.com/");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
// grab URL and pass it to the browser
$string = curl_exec($ch);
echo $string;
// close cURL resource, and free up system resources
curl_close($ch);

As far as storing in a database, presumably MySQL, you could use mysql_query(). I'd suggest using prepared statements but it seems as though that might be overwhelming at first glance for a PHP beginner.

Here's a very simple example of database connection and insertion:

$db = mysql_connect('DB_IP_OR_HOST','DB_USER','DB_PASS') or die("DB error");
mysql_select_db('YOUR_DB_NAME', $db);
$result = mysql_query('INSERT INTO MY_TABLE_NAME SET html = "' . mysql_real_escape_string($string) . '"');
mysql_close($db);
Sign up to request clarification or add additional context in comments.

Comments

0

If it lies on an external server, you can not view the PHP source, as it is a server-side language. You can only view the client-side source (generally HTML). If you would like to learn PHP to understand how the page was created, and make one similar, I suggest using Tizag's PHP tutorial as they are very easy to understand.

However, otherwise you can use fopen() or file_get_contents()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.