kath
kath

Reputation: 31

How do I get the page source of the html page using php codes?

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

Upvotes: 0

Views: 379

Answers (2)

Corey Ballou
Corey Ballou

Reputation: 43507

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

Upvotes: 2

John M.
John M.

Reputation: 2264

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()

Upvotes: 0

Related Questions