Home  >  Article  >  Backend Development  >  Access a web page with PHP and store its content in a file

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

PHPz
PHPzOriginal
2023-06-13 18:40:342058browse

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

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

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!

Statement:
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