Рефакторинг
This commit is contained in:
parent
1b5852cc43
commit
981a1d0f0f
11 changed files with 626 additions and 270 deletions
94
src/Connection/HttpRequest.php
Normal file
94
src/Connection/HttpRequest.php
Normal file
|
|
@ -0,0 +1,94 @@
|
|||
<?php
|
||||
|
||||
class Connection_HttpConnection
|
||||
{
|
||||
const POST = "POST";
|
||||
const GET = "GET";
|
||||
|
||||
private $param = array(); // Параметры запроса
|
||||
public $data = null; // Содержание
|
||||
public $url; // Адресс
|
||||
public $method = self::GET; // Метод
|
||||
public $port = 80;
|
||||
public $host = "";
|
||||
public $proxy_host = false;
|
||||
public $proxy_port = false;
|
||||
|
||||
/**
|
||||
* Возвращает заголовок соединения
|
||||
*/
|
||||
public function getHeader()
|
||||
{
|
||||
$result = $this->method . " " . $this->url . " HTTP/1.1\r\n";
|
||||
$result .= "Host: ". $this->host ."\r\n";
|
||||
foreach ($this->param as $key => $value) {
|
||||
$result .= $key . ": " . $value . "\r\n";
|
||||
}
|
||||
$result .= "Connection: Close\r\n\r\n";
|
||||
$result .= $this->data;
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Установка параметров запроса
|
||||
* @parma string $name
|
||||
* @parma string $value
|
||||
*/
|
||||
public function setParameter($name, $value)
|
||||
{
|
||||
$this->param[$name] = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Метод запроса GET или POST
|
||||
*/
|
||||
public function setMethod($method)
|
||||
{
|
||||
$this->method = $method;
|
||||
}
|
||||
|
||||
public function setUrl($url)
|
||||
{
|
||||
$this->url = $url;
|
||||
$this->host = parse_url($this->url, PHP_URL_HOST);
|
||||
}
|
||||
|
||||
public function getUrl()
|
||||
{
|
||||
return $this->url;
|
||||
}
|
||||
|
||||
/**
|
||||
* Содержание запроса
|
||||
*/
|
||||
public function setContent($data)
|
||||
{
|
||||
$this->setParameter ("Content-length", strlen($data));
|
||||
$this->data = $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Посылает запрос и возвращает страницу
|
||||
*/
|
||||
public function getPage()
|
||||
{
|
||||
$host = ($this->proxy_host) ? $this->proxy_host : $this->host;
|
||||
$port = ($this->proxy_port) ? $this->proxy_port : $this->port;
|
||||
$socket = fsockopen($host, $port, $errno, $errstr, 30);
|
||||
if (! $socket) {
|
||||
return null; // Exception
|
||||
} else {
|
||||
$header = $this->getHeader();
|
||||
fwrite($socket, $header);
|
||||
|
||||
$result = null;
|
||||
while (! feof($socket)) {
|
||||
$result .= fgets($socket, 128);
|
||||
}
|
||||
fclose($socket);
|
||||
return $result;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue