Home > Backend Development > PHP Tutorial > Two simple ways to obtain web page source code in PHP

Two simple ways to obtain web page source code in PHP

小云云
Release: 2023-03-21 14:18:01
Original
15522 people have browsed it

This article mainly shares with you two simple ways to obtain the source code of web pages in PHP. The first is curl, and the second is the file_get_contents() function. I hope it can help you.

The first type: curl

No more nonsense, just go to the code

//1,获取curl句柄
$ch = curl_init();
// 2. 设置选项,包括URLcurl_setopt($ch,CURLOPT_URL,"http://www.baidu.com/");
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);curl_setopt($ch,CURLOPT_HEADER,0);
// 3. 执行并获取HTML文档内容$output = curl_exec($ch);//打印网页源代码echo $output;
Copy after login

The second type: file_get_contents() function

This is simpler and only requires one line of code.

$result = file_get_contents($url);
//一句函数直接出结果
Copy after login

Related recommendations:

php obtains the content under the address through the web page address php gets the web page content php gets the web page source code php opens the network

The above is the detailed content of Two simple ways to obtain web page source code in PHP. 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