fsockopen手工GET/POST数据

//直接POST数据
function _post_file($host, $file, $vars){
        $fp = fsockopen($host, 80, $errno, $errstr, 10);
        if (!$fp) {
                echo "Socket Error: $errstr ($errno)\n";
                return false;
        }
        $post .= "POST $file HTTP/1.1\r\n";
        $post .= "Host: $host\r\n";
        $post .= "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.8.1.5) Gecko/20070713 Firefox/2.0.0.5\r\n";
        $post .= "Keep-Alive: 300\r\n";
        $post .= "Connection: keep-alive\r\n";
        $post .= "Referer: http://$host";
        $post .= "Content-Type: application/x-www-form-urlencoded\r\n";
        $post .= "Content-Length: ". strlen($vars) ."\r\n";
        $post .= $vars;
        $post .= "\r\n\r\n";

        fwrite($fp, $post);
        while (!feof($fp)) {
                $res = fread($fp, 1024);
        }
        fclose($fp);
        return $res;
}

//直接GET数据
function _get_file($host, $file){
        $fp = fsockopen($host, 80, $errno, $errstr, 10);
        if (!$fp) {
            echo "SocketError: $errstr ($errno)\n";
            return false;
        } 
        $get = "GET $file HTTP/1.1\r\n";
        $get .= "Host: $host\r\n";
        $get .= "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.8.1.5) Gecko/20070713 Firefox/2.0.0.5\r\n";
        $get .= "Referer: http://$host\r\n";
        $get .= "Connection: Close\r\n\r\n";

        fwrite($fp, $get);
        while (!feof($fp)) {
                echo fread($fp, 1024);
        }
        fclose($fp);
}

发表评论

返回顶部