PHP获取网页源码最简单的两种方法

版权声明: https://blog.csdn.net/qq_36100763/article/details/79563350

第一种:curl

废话不多说,直接上代码

//1,获取curl句柄
$ch = curl_init();

// 2. 设置选项,包括URL           
curl_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;


第二种:file_get_contents()函数

这种更加简单,只需要一行代码。


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






猜你喜欢

转载自blog.csdn.net/qq_36100763/article/details/79563350