Location: PHPKode > projects > TVEz - Media Library > tvez-0.2.1/html/shared/http_request.php
<?php

# ------------------------------------------------------------------------------
#
# function http_request ($host, $port, $timeout,$page)
# ------------------------------------------------------------------------------
#
# Copyright (C) 2003 Christian Eheim and Alex Pachikov
#
# This file is part of TVEz (tvez.sourceforge.net).
#
# TVEz is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# TVEz is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with TVEz; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
# ------------------------------------------------------------------------------
#
# Created on 12/07/2003 by Christian Eheim (hide@address.com)
#
# LAST MODIFIED:
# $Date: 2004/02/09 04:52:01 $
# $Revision: 1.1 $
# $Author: eheim $
#
# ------------------------------------------------------------------------------

################################################################################
# Makes an HTTP request to a given URL and return the page source
################################################################################
function http_request ($host, $port, $timeout,$page) {

	$host = preg_replace("/http:\/\//i","",$host);

	$request  = "GET $page HTTP/1.0\r\n";
	$request .= "Host: $host\r\n";
	$request .= "User-Agent: Mozilla/3.0\r\n";
	$request .= "\r\n";

	$socket  = fsockopen( $host, $port, $errno, $errstr, $timeout );
	if ($socket) {
		fputs( $socket, $request );
		$body = '';
		$headersDone = false;
		while ( !feof( $socket ) && $socket) {
			$line = fgets( $socket, 4096 );
			# If the headers are done, this line is part of the body
			if ($headersDone)
				$body .= $line;
			# If we hit the first blank line, the headers are done
			else if ( preg_match("/^\r\n$/", $line) )
				$headersDone = true;
			# otherwise, we are in the headers
			else {
				if ( preg_match("/^HTTP\/... ([0-9]*) .*$/", $line, $sts) )
					$status = $sts[1];
				else if ( preg_match("/^(.*): (.*)$/", $line, $matches) ) {
					$headers[$matches[1]] = $matches[2];
					# We are being redirected
					if ( $matches[1] == "Location" ) {
						$redirect = $matches[2];
						# We have a full url in the redirect
						if ( preg_match("/http/i", $redirect) ) {
							$ahost = preg_replace("/http:\/\//i","",$redirect);
							$host = preg_replace("/\/.*$/i","",$ahost);
							$page = preg_replace("/$host/i","",$ahost);
						}
						else {
							# The redirect is off the root
							if ( preg_match("/^\//", $redirect) )	
								$page = $redirect;
							# The redirect is relative to the current page
							else
								$page .= $redirect;
						}
						fclose( $socket );
						return http_request($host, $port, $timeout, $page);
					}
				}
			}
		}
		fclose( $socket );
		# Follow redirects
		
		return $body;
	}
	else { echo $errstr; return false; }
} 

################################################################################
# Makes an HTTP request to a given URL and return the page source
################################################################################
function http_headers ($host, $port, $timeout,$page) {

	$host = preg_replace("/http:\/\//i","",$host);

	$request  = "GET $page HTTP/1.0\r\n";
	$request .= "Host: $host\r\n";
	$request .= "User-Agent: Mozilla/3.0\r\n";
	$request .= "\r\n";

	$socket  = fsockopen( $host, $port, $errno, $errstr, $timeout );
	if ($socket) {
		fputs( $socket, $request );
		$body = '';
		$headersDone = false;
		while ( !feof( $socket ) && $socket) {
			$line = fgets( $socket, 4096 );
			# If the headers are done, this line is part of the body
			if ($headersDone)
				return $headers;
			# If we hit the first blank line, the headers are done
			else if ( preg_match("/^\r\n$/", $line) )
				$headersDone = true;
			# otherwise, we are in the headers
			else {
				if ( preg_match("/^HTTP\/... ([0-9]*) .*$/", $line, $sts) )
					$status = $sts[1];
				else if ( preg_match("/^(.*): (.*)$/", $line, $matches) ) {
					$headers[$matches[1]] = $matches[2];
				}
			}
		}
		fclose( $socket );
		# Follow redirects
		
		return $headers;
	}
	else { echo $errstr; return false; }
} 

?>
Return current item: TVEz - Media Library