How To Save Website Page Content Using PHP Tutorial

Reading Time: 3 minutes
1,261 Views

Inside this article we will see the concept i.e How To Save Website Page Content Using PHP. Article contains classified information about website page content scrapping in PHP. How To get website page content and save into an external file we will see inside this article.

If you are looking for an article which gives you the information about website page scrapping in PHP then this article will give a better solution for this.

Learn More –

Let’s get started.

Application Programming

Create a file called it as index.php inside your project folder. Open index.php and write this code into it.

We will see file writing by these functions of PHP – file_put_contents() & fopen()

Using file_put_contents()

<?php


$ch = curl_init("https://www.tutorialspoint.com/index.htm");

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);

$content = curl_exec($ch);

curl_close($ch);

@file_put_contents("website.txt", $content);

Make sure, the location you write content into it will have the sufficient permission to store file. Here, we are saving given URL page content into website.txt file.

Using fopen()

Inside this, we will use fopen PHP function to write file content and then save.

<?php

$ch = curl_init("https://www.tutorialspoint.com/index.htm");

$fp = fopen("website.txt", 'wb');

// It set an option for a cURL transfer
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);

$content = curl_exec($ch);

curl_close($ch);

// Close file
fclose($fp);

You can use any method of PHP to save content into file.

Application Testing

Now,

Open into browser.

URL: http://localhost/index.php

Once you run your application, page content will be save into website.txt file. File will be saved into same directory of application.

We hope this article helped you to learn How To Save Website Page Content Using PHP Tutorial in a very detailed way.

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.

Sanjay KumarHello friends, I am Sanjay Kumar a Web Developer by profession. Additionally I'm also a Blogger, Youtuber by Passion. I founded Online Web Tutor and Skillshike platforms. By using these platforms I am sharing the valuable knowledge of Programming, Tips and Tricks, Programming Standards and more what I have with you all. Read more