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.
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;
This is simpler and only requires one line of code.
$result = file_get_contents($url); //一句函数直接出结果
Related recommendations:
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!