当前位置:编程学习 > php >>

php模仿用户访问网页程序代码

php教程模仿用户访问网页程序代码

function httpget( $url, $followredirects=true ) {
global $final_url;
$url_parsed = parse_url($url);
if ( empty($url_parsed['scheme']) ) {
    $url_parsed = parse_url('http://'.$url);
}
$final_url = $url_parsed;
 
$port = $url_parsed["port"];
if ( !$port ) {
    $port = 80;
}
$rtn['url']['port'] = $port;
 
$path = $url_parsed["path"];
if ( empty($path) ) {
    $path="/";
}
if ( !empty($url_parsed["query"]) ) {
    $path .= "?".$url_parsed["query"];
}
$rtn['url']['path'] = $path;
 
$host = $url_parsed["host"];
$foundbody = false;
 
$out = "get $path http/1.0rn";
$out .= "host: $hostrn";
$out .= "user-agent:      mozilla/5.0 (windows; u; windows nt 5.1; en-us; rv:1.8.1) gecko/20061010 firefox/2.0rn";
$out .= "connection: closernrn";
 
if ( !$fp = @fsockopen($host, $port, $errno, $errstr, 30) ) {
    $rtn['errornumber'] = $errno;
    $rtn['errorstring'] = $errstr;
 
}
fwrite($fp, $out);
while (!@feof($fp)) {
 
    $s = @fgets($fp, 128);
    if ( $s == "rn" ) {
        $foundbody = true;
        continue;
    }
    if ( $foundbody ) {
        $body .= $s;
    } else {
        if ( ($followredirects) && (stristr($s, "location:") != false) ) {
            $redirect = preg_replace("/location:/i", "", $s);
            return httpget( trim($redirect) );
        }
        $header .= $s;
    }
}
 
fclose($fp);
 
return(trim($body));
补充:Php教程,Php常用代码 
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,