Home > Backend Development > PHP Tutorial > What are the ways to obtain web content in php? Code example for php to obtain web page content

What are the ways to obtain web content in php? Code example for php to obtain web page content

不言
Release: 2023-04-04 10:44:01
forward
3278 people have browsed it

What this article brings to you is what are the methods of obtaining web content in PHP? The code example for php to obtain web page content has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

3 methods of using php to obtain web page source code and crawl web content, we can choose according to actual needs.

1. Use file_get_contents to obtain the web page source code

This method is the most commonly used and only requires two lines of code. It is very simple and convenient.

Reference code:

<?php
$fh= file_get_contents(&#39;http://www.webkaka.com/&#39;);
echo $fh;
?>
Copy after login

2. Use fopen to obtain the source code of the web page

There are many people using this method, but there is a lot of code.

Reference code:

<?php
$fh = fopen(&#39;http://www.webkaka.com/&#39;, &#39;r&#39;);
if($fh){
    while(!feof($fh)) {
        echo fgets($fh);
    }
}
?>
Copy after login

3. Use curl to obtain the source code of the web page

The practice of using curl to obtain the source code of the web page is often required by people with higher requirements. Use, for example, when you need to obtain web page header information while crawling web content, as well as the use of ENCODING encoding, the use of USERAGENT, etc.

Reference code one:

<?php
// 创建一个新cURL资源
$ch = curl_init();
// 设置URL和相应的选项
curl_setopt($ch, CURLOPT_URL, "http://www.webkaka.com/");
curl_setopt($ch, CURLOPT_HEADER, false);
// 抓取URL并把它传递给浏览器
$data = curl_exec($ch);
echo $data;
//关闭cURL资源,并且释放系统资源
curl_close($ch);
?>
Copy after login

Reference code two:

<?php
$szUrl = "http://www.webkaka.com/";
$UserAgent = &#39;Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; SLCC1; .NET CLR 2.0.50727; .NET CLR 3.0.04506; .NET CLR 3.5.21022; .NET CLR 1.0.3705; .NET CLR 1.1.4322)&#39;;
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $szUrl);
curl_setopt($curl, CURLOPT_HEADER, 0);  //0表示不输出Header,1表示输出
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_ENCODING, &#39;&#39;);
curl_setopt($curl, CURLOPT_USERAGENT, $UserAgent);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
$data = curl_exec($curl); 
echo $data;
//echo curl_errno($curl); //返回0时表示程序执行成功 
exit();
?>
Copy after login

The above is the detailed content of What are the ways to obtain web content in php? Code example for php to obtain web page content. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:csdn.net
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