소켓이용 post, get 방식으로 내용 받아오는 클래스

GET, POST를 이용해 내용을 받아오는 클래스 조회수:2806

검색해보니 나오긴 하는데 제가 필요한 기능을 갖추지 못했더군요.
그래서 제가 필요한 대로 급조해봤습니다.
혹여나 필요하신 분 있을까 해서 올려봅니다. 참조하시기 바랍니다.

사용 예:
server = “phpschool.com”;
$http->port = 80;
$http->method = “get”; // POST를 사용하시려면 post로 변경해 주세요.
$http->path = “/bbs2/inc_board.html”;

$http->header[“Accept”] = “image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*”;
$http->header[“Referer”] = “http://phpschool.com/”;
$http->header[“Accept-Language”] = “ko”;
$http->header[“Content-Type”] = “application/x-www-form-urlencoded”;
$http->header[“User-Agent”] = “Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.0.3705)”;
$http->header[“Host”] = $http->server;
$http->header[“Connection”] = “Keep-Alive”;
$http->header[“Cache-Control”] = “no-cache”;

$http->variable[“code”] = “tnt2”;

$http->openSock();
$data_get = $http->getPage();

echo nl2br(htmlspecialchars($data));

?>

——————————————————————–
class.network.get_Page.php
——————————————————————–

var $server;
var $port;
var $method;
var $sock;
var $path;
var $header = array();
var $variable = array();
var $cmd;
var $config;

function http_get_Page() {
$this->port = 80;
$this->method = “get”;
$this->path = “/”;

$this->config = array(
“version” => “1.0”,
“name” => “Get files through http protocol”,
“debugname” => “class.network.get_Page.php”,
);
}

function openSock() {
$this->sock = @fsockopen($this->server, $this->port);
if (!$this->sock) $this->Error(“Cannot open socket [$this->server:$this->port]”);
}

function getPage() {
if (eregi(“GET”, $this->method)) {
if ($this->variable) {
$aid_url = “?”;
foreach ($this->variable as $key => $value) {
$key = urlencode($key);
$value = urlencode($value);
if ($aid_url == “?”) $aid_url .= $key.”=”.$value;
else $aid_url .= “&”.$key.”=”.$value;
}
}

$cmd = “GET “.$this->path.$aid_url.” HTTP/1.1″;
if (!$this->header) $this->Error(“No Any HTTP Headers.”);

foreach ($this->header as $key => $value) {
$cmd .= “
“.$key.”: “.$value;
}

$cmd .= “

“;
}

if (eregi(“POST”, $this->method)) {
if ($this->variable) {
$aid_query = “”;

foreach ($this->variable as $key => $value) {
$key = urlencode($key);
$value = urlencode($value);
if (!$aid_query) $aid_query .= $key.”=”.$value;
else $aid_query .= “&”.$key.”=”.$value;
}
}

$cmd = “POST “.$this->path.” HTTP/1.1″;
if (!$this->header) $this->Error(“No Any HTTP Headers.”);

$strlen = strlen($aid_query);
$this->header[“Content-Length”] = $strlen;

foreach ($this->header as $key => $value) {
$cmd .= “
“.$key.”: “.$value;
}

$cmd .= “

“;
$cmd .= $aid_query;
}

fputs($this->sock, $cmd);
$str_get = “”;

while (!feof($this->sock)) {
$str_get .= fgets($this->sock, 1024);
}

return $str_get;
}

function Error($message) {
die ($this->config[debugname].” Error: “.$message);
}
}

?>

댓글 남기기

이메일은 공개되지 않습니다. 필수 입력창은 * 로 표시되어 있습니다