• Модуль: main
  • Путь к файлу: ~/bitrix/modules/main/classes/general/http.php
  • Класс: CHTTP
  • Вызов: CHTTP::Query
public function Query($method, $host, $port, $path, $postdata = false, $proto = '', $post_content_type = 'N', $dont_wait_answer = false)
{
	$this->status = 0;
	$this->result = '';
	$this->headers = array();
	$this->cookies = array();
	$fp = fsockopen($proto.$host, $port, $this->errno, $this->errstr, $this->http_timeout);
	if ($fp)
	{
		$strRequest = "$method $path HTTP/1.0rn";
		$strRequest .= "Connection: closern";
		$strRequest .= "User-Agent: {$this->user_agent}rn";
		$strRequest .= "Accept: */*rn";
		$strRequest .= "Host: $hostrn";
		$strRequest .= "Accept-Language: enrn";

		foreach ($this->additional_headers as $key => $value)
			$strRequest .= $key.": ".$value."rn";

		if ($method == 'POST' || $method == 'PUT')
		{
			if ('N' !== $post_content_type)
				$strRequest .= $post_content_type == '' ? '' : "Content-type: ".$post_content_type."rn";
			else
				$strRequest.= "Content-type: application/x-www-form-urlencodedrn";

			if(!array_key_exists("Content-Length", $this->additional_headers))
				$strRequest.= "Content-Length: ".strlen($postdata) . "rn";
		}
		$strRequest .= "rn";
		fwrite($fp, $strRequest);

		if ($method == 'POST' || $method == 'PUT')
		{
			if(is_resource($postdata))
			{
				while(!feof($postdata))
					fwrite($fp, fread($postdata, 1024*1024));
			}
			else
			{
				fwrite($fp, $postdata);
			}
		}

		if ($dont_wait_answer)
		{
			fclose($fp);
			return true;
		}

		$headers = "";
		while(!feof($fp))
		{
			$line = fgets($fp, 4096);
			if($line == "rn" || $line === false)
			{
				//$line = fgets($fp, 4096);
				break;
			}
			$headers .= $line;
		}
		$this->ParseHeaders($headers);

		if(is_resource($this->fp))
		{
			while(!feof($fp))
			{
				$buf = fread($fp, 40960);
				if ($buf === false)
					break;
				fwrite($this->fp, $buf);
				fflush($this->fp);
			}
		}
		else
		{
			$this->result = "";
			while(!feof($fp))
			{
				$buf = fread($fp, 4096);
				if ($buf === false)
					break;
				$this->result .= $buf;
			}
		}

		fclose($fp);

		return true;
	}

	/** @global CMain $APPLICATION */
	global $APPLICATION;
	$APPLICATION->ThrowException(
		sprintf("Error connecting to %s:%s. Error code: "%s", error description: "%s"",
			$this->errstr,
			$this->errno,
			$host,
			$port
		)
	);
	return false;
}