我制作了一个基本脚本来获取 html 页面和图像。但我想在检索图片时继续保持联系。
- 我对测试脚本所做的一直是在 Web 服务器上询问地址。但它没有用。
那么我该怎么做或者我做错了什么?
基本脚本:
function fetch( $server, $url, $port = 80, &$resp_head )
{
if ( !$con = @fsockopen($server, $port, $error_number, $error_string, 30) ) return False;
stream_set_timeout( $con, 8600 );
// Reguest
$qry_header = 'GET '.$url." HTTP/1.1\r\n";
$qry_header .= 'Host: '.$server."\r\n";
$qry_header .= 'User-Agent: Mihtyom; (+http://'.$_SERVER['HTTP_HOST']."/)\r\n";
$qry_header .= "Accept: */*\r\n"; //google check the 3 "Accept"
$qry_header .= "Accept-Language: *\r\n";
$qry_header .= "Accept-Encoding: *\r\n";
$qry_header .= "Connection: Close\r\n\r\n";
fwrite( $con, $qry_header );
$inheader = True;
$data = '';
while (!feof( $con ))
{
$line = fgets( $con );
if (!$inheader)
{
$data .= $line;
continue;
}
if ($line == "\n" || $line == "\r\n")
{
$inheader = False;
continue;
}
$resp_head[] = trim($line);
}
fclose( $con );
return $data;
}
测试脚本:
function fetch( $server, $urls, $port = 80, &$resp_head )
{
if ( !$con = @fsockopen($server, $port, $error_number, $error_string, 30) ) return False;
stream_set_timeout( $con, 8600 );
$resp_head = array();
$data = array();
foreach ($urls as $key => $url)
{
$inheader = True;
$data[$key] = '';
// Reguest
$qry_header = 'GET '.$url." HTTP/1.1\r\n";
$qry_header .= 'Host: '.$server."\r\n";
$qry_header .= 'User-Agent: Mihtyom; (+http://'.$_SERVER['HTTP_HOST']."/)\r\n";
$qry_header .= "Accept: */*\r\n"; //google check the 3 "Accept"
$qry_header .= "Accept-Language: *\r\n";
$qry_header .= "Accept-Encoding: *\r\n";
$qry_header .= "Connection: Close\r\n\r\n";
fwrite( $con, $qry_header );
while (!feof( $con ))
{
$line = fgets( $con );
if (!$inheader)
{
$data[$key] .= $line;
continue;
}
if ($line == "\n" || $line == "\r\n")
{
$inheader = False;
continue;
}
$resp_head[$key][] = trim($line);
}
}
fclose( $con );
return $data;
}
新脚本(21-07-12 12:34):
function fetch( $server, $urls, $port = 80, &$resp_head )
{
if ( !$con = @pfsockopen($server, $port, $error_number, $error_string, 30) ) return False;
stream_set_timeout( $con, 8600 );
$resp_head = array();
$data = '';
$i = count($urls);
foreach ($urls as $key => $url )
{
$i--;
// Reguest
$qry_header = 'GET '.$url." HTTP/1.1\r\n";
$qry_header .= 'Host: '.$server."\r\n";
$qry_header .= 'User-Agent: Mihtyom; (+http://'.$_SERVER['HTTP_HOST']."/)\r\n";
$qry_header .= "Accept: */*\r\n";
$qry_header .= "Accept-Language: *\r\n";
$qry_header .= "Connection: ".($i == 0 ? 'Close' : 'keep-alive')."\r\n\r\n";
fwrite( $con, $qry_header );
}
while (!feof( $con ))
{
$line = fgets( $con );
if ($line == "\r\n\r\n" || $line == "\n\n") break;
$data .= $line;
}
fclose( $con );
return $data;
}