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 –
- Laravel 9 Work With Livewire Form Submit Tutorial
- How To Get Selected Checkbox Value From Checkbox List in jQuery
- Laravel 9 How To Work With Livewire Pagination Example
Let’s get started.
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()
<?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.
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.
Open into browser.
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.