Access a web page with PHP and store its content in a file

PHPz
Release: 2023-06-13 18:42:01
Original
1987 people have browsed it

Today we will learn how to use PHP to access a web page and save its content to a local file. This skill is very useful for crawling and data analysis.

First, we need to use the curl function in PHP to obtain the web page content. curl is a library that can be used to access the network and can support multiple protocols such as HTTP, FTP, SMTP, etc.

We first create a PHP file, the example file is called get_html.php, and then add the following code to the file:

<?php
// 获取网址
$url = 'http://www.example.com';

// 初始化 curl
$ch = curl_init();

// 设置参数
curl_setopt($ch, CURLOPT_URL, $url);  // 设置访问的 URL
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // 将结果返回而不显示
curl_setopt($ch, CURLOPT_HEADER, false); // 不返回头信息

// 发送请求
$result = curl_exec($ch);

// 关闭 curl
curl_close($ch);

// 打印结果
echo $result;
?>
Copy after login

In the above code, we first define the URL to be accessed, Then initialize curl with the curl_init() function. Next, we set some of the parameters mentioned above, such as the accessed URL, returning the results without displaying them, not returning header information, etc. Finally, we execute the request with the curl_exec() function and store the result in the $result variable. Finally, we print out the value of the $result variable to see the content of this web page.

Next, we need to store the web page content into a local file. We can use the file_put_contents() function to write string contents directly to a file. It is important to note that we need to create the file first and then save the content to the file.

<?php
// 获取网址
$url = 'http://www.example.com';

// 初始化 curl
$ch = curl_init();

// 设置参数
curl_setopt($ch, CURLOPT_URL, $url);  // 设置访问的 URL
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // 将结果返回而不显示
curl_setopt($ch, CURLOPT_HEADER, false); // 不返回头信息

// 发送请求
$result = curl_exec($ch);

// 关闭 curl
curl_close($ch);

// 将结果保存到文件中
$file = 'page.html';
file_put_contents($file, $result);
?>
Copy after login

In the above code, we first define the URL to be accessed, then use the curl_init() function to initialize curl and set some parameters. We then execute the request and store the result in the $result variable. Finally, we use the file_put_contents() function to save the value of the $result variable to a file called page.html.

Okay, we have learned how to use PHP to access a web page and store its content into a file. This is a basic skill for data scraping and analysis. I hope this article will be helpful to you.

The above is the detailed content of Access a web page with PHP and store its content in a file. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!