<?php
class Files
{
public static function getUrlContentList($url, $ip = '')
{
if (empty ( $url )) {
return false;
}
$parsed = parse_url ( $url );
$host = $parsed ['host'];
$uri = $parsed ['path'] ? $parsed ['path'] : '';
$uri .= $parsed ['query'] ? '?' . $parsed ['query'] : '';
$uri .= $parsed ['fragment'] ? '#' . $parsed ['fragment'] : '';
unset ( $parsed );
$ips = gethostbynamel ( $host );
$rets = array ();
if (! empty ( $ip )) {
if (! in_array ( $ip, $ips )) {
return false;
} else {
$rets [] = self::HttpVisit ( $ip, $host, $uri );
return $rets;
}
}
foreach ( $ips as $ip ) {
$rets [] = self::HttpVisit ( $ip, $host, $uri );
}
return $rets;
}
public static function HttpVisit($ip, $host, $uri)
{
$errstr = '';
$errno = '';
$fp = fsockopen ( $ip, 80, $errno, $errstr, 90 );
if (! $fp) {
return false;
} else {
$out = "GET {$uri} HTTP/1.1/r/n";
$out .= "Host:{$host}/r/n";
$out .= "Connection: close/r/n/r/n";
fputs ( $fp, $out );
while ( $line = fread ( $fp, 4096 ) ) {
$response .= $line;
}
fclose ( $fp );
//去掉Header头信息
$pos = strpos ( $response, "/r/n/r/n" );
$response = substr ( $response, $pos + 4 );
return $response;
}
}
}
/*
调用方法:
$data = Files::getUrlContentList('http://xxx/xxx.htm', '192.168.xxx.xxx');
print_r($data);
*/
?>
如果一个域名对应多个IP,取得指定IP下的网页内容
最新推荐文章于 2025-05-03 23:14:20 发布
本文介绍了一种使用PHP实现的网页内容抓取方法,通过解析URL并利用socket连接来获取网页内容,适用于需要从指定IP地址抓取数据的场景。
1万+

被折叠的 条评论
为什么被折叠?



