方法一: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; ?>