Home > Backend Development > PHP Tutorial > PHP imitates users accessing web page program code

PHP imitates users accessing web page program code

高洛峰
Release: 2016-11-29 15:55:02
Original
1329 people have browsed it

function httpget( $url, $followredirects=true ) {  
global $final_url;  
$url_parsed = parse_url($url);  
if ( emptyempty($url_parsed['scheme']) ) {  
    $url_parsed = parse_url('http://'.$url);  
}  
$final_url = $url_parsed;  
  
$port = $url_parsed["port"];  
if ( !$port ) {  
    $port = 80; //www.php.cn
}  
$rtn['url']['port'] = $port;  
  
$path = $url_parsed["path"];  
if ( emptyempty($path) ) {  
    $path="/";  
}  
if ( !emptyempty($url_parsed["query"]) ) {  
    $path .= "?".$url_parsed["query"];  
}  
$rtn['url']['path'] = $path;  
  
$host = $url_parsed["host"];  
$foundbody = false;  
  
$out = "get $path http/1.0 ";  
$out .= "host: $host ";  
$out .= "user-agent:      mozilla/5.0 (windows; u; windows nt 5.1; en-us; rv:1.8.1) gecko/20061010 firefox/2.0 ";  
$out .= "connection: close ";  
  
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 == " " ) {  
        $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));
Copy after login


Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template