share123

php中能够获取到某一网站内容的方法

方法一:file_get_contents 函数

example:

<?php
$url = "http://www.cnblogs.com";
$contents = file_get_contents($url);
echo $contents;
?> 

出现乱码需要在输出前加一句:

$getcontent = iconv("gb2312", "utf-8",$contents); 

方法二:fopen

example:

<?php
$handle = fopen ("http://www.cnblogs.com", "rb");
$contents = "";
do {
$data = fread($handle, 1024);
if (strlen($data) == 0) {
break;
}
$contents .= $data;
} while(true);
fclose ($handle);
echo $contents;
?> 

但是方法一和方法二需要服务器中php的配置开启了“allow_url_fopen = On”,才允许远端访问

 方法三:curl(这种方法比较好用。)

<?php
$url = "http://www.cnblogs.com";
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
//在需要用户检测的网页里需要增加下面两行
//curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
//curl_setopt($ch, CURLOPT_USERPWD, US_NAME.":".US_PWD);
$contents = curl_exec($ch);
curl_close($ch);
echo $contents;
?> 

 

分类:

技术点:

相关文章:

  • 2022-02-09
  • 2021-12-15
  • 2021-11-17
  • 2022-12-23
  • 2021-10-13
  • 2022-02-01
  • 2021-12-10
  • 2022-01-07
猜你喜欢
  • 2021-12-18
  • 2022-02-07
  • 2021-12-18
  • 2021-12-07
  • 2021-11-23
  • 2022-01-12
  • 2021-12-20
相关资源
相似解决方案