function get_content_by_socket($url){
$url = eregi_replace('^http://', '', $url);
$temp = explode('/', $url);
$host = array_shift($temp);
$url = ''.implode('/', $temp);
$temp = explode(':', $host);
$host = $temp[0];
$port = isset($temp[1]) ? $temp[1] : 80;
//echo $url;
//echo $host;
$fp = fsockopen($host, 80) or die("Open ". $url ." failed");
$header = "GET /".$url ." HTTP/1.1\r\n";
$header .= "Accept: */*\r\n";
$header .= "Accept-Language: zh-cn\r\n";
$header .= "Accept-Encoding: gzip, deflate\r\n";
$header .= "If-Modified-Since: Tue, 06 Apr 2010 07:56:03 GMT; length=2235\r\n";
$header .= "User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; GTB6.4)\r\n";
$header .= "Host: ". $host ."\r\n";
$header .= "Referer: http://video.baidu.com/v?word=11&ct=301989888&rn=20&pn=0&db=0&s=0&fbl=800\r\n";
//fputs($content, "Referer: $domainrn");//伪造部分
$header .= "Connection: Keep-Alive\r\n";
$header .= "Cookie: BAIDUID=5F96971273579588527A980F307E8B7A:FG=1\r\n\r\n";
//$header .= "Connection: Close\r\n\r\n";
fwrite($fp, $header);
while (!feof($fp)) {
$contents .= fgets($fp, 8192);
}
fclose($fp);
return $contents;
}
以这个函数与为例
只要读取到第10行,下面的就不取了直接结束输出内容!这样取到了自己想要的,节省时间资源!
或者只读取到<div src=324423> 自定义哪个字段
可实现吗
------解决方案--------------------
$i = 0;
while(!eof($f) and $i<10) {
xxxx;
$i++;
}