Rumah > Artikel > pembangunan bahagian belakang > php 信息采集 采不到内容?该如何解决
php 信息采集 采不到内容?
今天突然想采集点东西,刚开始还可以,一切正常,可是过了一段时间就什么也采集不到了,不知道问题出在哪里了,代码如下,请教各位?
function getContent($url) {
$url = trim($url);
$content = '';
if (extension_loaded('curl')) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch,CURLOPT_HTTPHEADER,array(
'Accept-Language: zh-cn',
'Connection: Keep-Alive',
'Cache-Control: no-cache'
));
$user_agent ="Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)";
curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
$content = curl_exec($ch);
curl_close($ch);
} else {
$content = file_get_contents($url);
}
return trim($content);
}//end func getContent();