<?php
	/***********************************************************************
	* Script (c) Kubwa (https://kubwa.de)
	*
	* This script was release under BSD license.
	* You are free to use, share or change this code as you wish. This
	* header must be kept intact.
	***********************************************************************/

	function HttpRequest($Host, $PostData = "", $Auth = "", $Timeout = 60, $ContentType = "application/x-www-form-urlencoded")
	{
		$Res = "";
		try
		{
			if (function_exists("curl_init"))
			{
				$Handler = curl_init();
				curl_setopt($Handler, CURLOPT_URL, $Host);
				curl_setopt($Handler, CURLOPT_TIMEOUT, $Timeout);
				curl_setopt($Handler, CURLOPT_SSL_VERIFYHOST, false);
				curl_setopt($Handler, CURLOPT_SSL_VERIFYPEER, false);

				$Headers = array();
				$Headers[] = "Connection: Close";
				$Headers[] = "User-Agent: Kubwa Framework HTTP-Request";
				if (!empty($Auth))
				 {$Headers[] = "Authorization: Basic ".base64_encode($Auth);}
				if (!empty($PostData))
				{
					$Headers[] = "Content-type: ".$ContentType;
					$Headers[] = "Content-Length: ".strlen($PostData);
					curl_setopt($Handler, CURLOPT_POST, true);
					curl_setopt($Handler, CURLOPT_POSTFIELDS, $PostData);
				}
				curl_setopt($Handler, CURLOPT_HTTPHEADER, $Headers);

				curl_setopt($Handler, CURLOPT_FOLLOWLOCATION, true);
				curl_setopt($Handler, CURLOPT_HEADER, false);
				curl_setopt($Handler, CURLOPT_RETURNTRANSFER, true);
				$Res = curl_exec($Handler);
				curl_close($Handler);
			}
			else
			{
				$Headers = array();
				$Headers[] = "Connection: Close";
				$Headers[] = "User-Agent: Kubwa Framework HTTP-Request";
				if (!empty($Auth))
				 {$Headers[] = "Authorization: Basic ".base64_encode($Auth);}
				if (!empty($PostData))
				{
					$Headers[] = "Content-type: ".$ContentType;
					$Headers[] = "Content-Length: ".strlen($PostData);
					$Options = array('http' => array(	'header'  => $Headers,
														'method'  => 'POST',
														'timeout' => $Timeout,
														'content' => $PostData
													),
									'ssl' 	=> array(	'verify_peer' => false,
														'allow_self_signed' => true
													)
									);
				}
				else
				{
					$Options = array('http' => array(	'header'  => $Headers,
														'method'  => 'GET',
														'timeout' => $Timeout
													),
									'ssl' 	=> array(	'verify_peer' => false,
														'allow_self_signed' => true
													)
									);
				}
				$Res = file_get_contents($Host, false, stream_context_create($Options));
			}
			return $Res;
		}
		catch (HttpException $ex)
		{
			return $ex;
		}
	}
?>