[함수] fopen 해결및 메타검색소스참고
컨텐츠 정보
- 31,689 조회
- 0 추천
- 목록
본문
자료에등록이안되네엽 왜그런다지.
fopen대용으로 사용해보세엽
메타소스데모: http://dnsolution.co.kr/dnmeta/naver.php
<?
class BrowserEmulator {
var $headerLines = Array();
var $postData = Array();
var $authUser = "";
var $authPass = "";
var $port;
var $lastResponse = Array();
var $debug = false;
function BrowserEmulator() {
$this->resetHeaderLines();
$this->resetPort();
}
function addHeaderLine($name, $value) {
$this->headerLines[$name] = $value;
}
function resetHeaderLines() {
$this->headerLines = Array();
/*******************************************************************************/
/************** YOU MAX SET THE USER AGENT STRING HERE *******************/
/* */
/* default is "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)", */
/* which means Internet Explorer 6.0 on WinXP */
$this->headerLines["User-Agent"] = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)";
/*******************************************************************************/
}
/**
* $url불러fopen을 만듦
* 값을부름. 모든헤드에 보냄, 데이터와사용자보안데이타보냄.
* 파일조정되었을시 되돌림, or 오류시없엠.
**/
function fopen($url) {
$this->lastResponse = Array();
preg_match("~([a-z]*://)?([^:^/]*)(:([0-9]{1,5}))?(/.*)?~i", $url, $matches);
$protocol = $matches[1];
$server = $matches[2];
$port = $matches[4];
$path = $matches[5];
if ($port!="") {
$this->setPort($port);
}
if ($path=="") $path = "/";
$socket = false;
$socket = fsockopen($server, $this->port);
if ($socket) {
$this->headerLines["Host"] = $server;
if ($this->authUser!="" && $this->authPass!="") {
$this->headerLines["Authorization"] = "Basic ".base64_encode($this->authUser.":".$this->authPass);
}
if (count($this->postData)==0) {
$request = "GET $path HTTP/1.0\r\n";
} else {
$request = "POST $path HTTP/1.0\r\n";
}
if ($this->debug) echo $request;
fputs ($socket, $request);
if (count($this->postData)>0) {
$PostStringArray = Array();
foreach ($this->postData AS $key=>$value) {
$PostStringArray[] = "$key=$value";
}
$PostString = join("&", $PostStringArray);
$this->headerLines["Content-Length"] = strlen($PostString);
}
foreach ($this->headerLines AS $key=>$value) {
if ($this->debug) echo "$key: $value\n";
fputs($socket, "$key: $value\r\n");
}
if ($this->debug) echo "\n";
fputs($socket, "\r\n");
if (count($this->postData)>0) {
if ($this->debug) echo "$PostString";
fputs($socket, $PostString."\r\n");
}
}
if ($this->debug) echo "\n";
if ($socket) {
$line = fgets($socket, 1000);
if ($this->debug) echo $line;
$this->lastResponse[] = $line;
$status = substr($line,9,3);
while (trim($line = fgets($socket, 1000)) != ""){
if ($this->debug) echo "$line";
$this->lastResponse[] = $line;
if ($status=="401" AND strpos($line,"WWW-Authenticate: Basic realm=\"")===0) {
fclose($socket);
return FALSE;
}
}
}
return $socket;
}
/**
* 포트추가. 포트파라메터값 HTTP POST 요구을 바디에 보냄.
**/
function addPostData($name, $value) {
$this->postData[$name] = $value;
}
/**
* 모든일반포트파라메터지움.
**/
function resetPostData() {
$this->postData = Array();
}
/**
* 사용자페스워드 사용요구.
* .
**/
function setAuth($user, $pass) {
$this->authUser = $user;
$this->authPass = $pass;
}
/**
* 사용자일반포트선택요구.
**/
function setPort($portNumber) {
$this->port = $portNumber;
}
/**
* 보낸사용자기본 HTTP default(80)리셋요구.
**/
function resetPort() {
$this->port = 80;
}
/**
* $url 불러파일을만듦.
* 값을부르고. 헤드보냄, 데이터와사용자보안.
* 요구파일성공적으로 정렬시 되돌림, 또는오류슘김.
**/
function file($url) {
$file = Array();
$socket = $this->fopen($url);
if ($socket) {
$file = Array();
while (!feof($socket)) {
$file[] = fgets($socket, 10000);
}
} else {
return FALSE;
}
return $file;
}
function getLastResponseHeaders() {
return $this->lastResponse;
}
}
/*
// 예제코드
$be = new BrowserEmulator();
$be->addHeaderLine("Referer", "http://dnsolution.co.kr/");
$be->addHeaderLine("Accept-Encoding", "x-compress; x-zip");
$be->addPostData("Submit", "OK");
$be->addPostData("item", "42");
$be->setAuth("admin", "secretpass");
// also possible:
// $be->setPort(10080);
$file = $be->fopen("http://dnsolution.co.kr/somepage.html");
$response = $be->getLastResponseHeaders();
while ($line = fgets($file, 1024)) {
// 파일width아무거나!
}
fclose($file);
*/
?>
--------------------------------------------------------------------------------------
fopen() 이나 fsockopen() 으로 열리지 않는 페이지는 file() 로 잘 열리더군요,
그리고 post 형식 form 페이지는 아래 함수로 잘 열립니다.
function HTTP_Post($URL,$data, $referrer=\"\") {
// parsing the given URL
$URL_Info=parse_url($URL);
// Building referrer
if($referrer==\"\") // if not given use this script as referrer
$referrer=$_SERVER[\"SCRIPT_URI\"];
// making string from $data
foreach($data as $key=>$value)
$values[]=\"$key=\".urlencode($value);
$data_string=implode(\"&\",$values);
// Find out which port is needed - if not given use standard (=80)
if(!isset($URL_Info[\"port\"]))
$URL_Info[\"port\"]=80;
// building POST-request:
$request.=\"POST \".$URL_Info[\"path\"].\" HTTP/1.1\\n\";
$request.=\"Host: \".$URL_Info[\"host\"].\"\\n\";
$request.=\"Referer: $referer\\n\";
$request.=\"Content-type: application/x-www-form-urlencoded\\n\";
$request.=\"Content-length: \".strlen($data_string).\"\\n\";
$request.=\"Connection: close\\n\";
$request.=\"\\n\";
$request.=$data_string.\"\\n\";
$fp = fsockopen($URL_Info[\"host\"],$URL_Info[\"port\"]);
fputs($fp, $request);
while(!feof($fp)) {
$result .= fgets($fp, 128);
}
fclose($fp);
return $result;
}
사용은
$output=HTTP_Post(\"http://www.server.com/script.php\",$_POST);
$_POST 는 post 폼에서 필요한 변수들의 배열입니다. 예를들면
$_POST[\"name\"] = \"php\";
$_POST[\"pwd\"] = \"school\";
$_POST[\"email\"] = \"webmaster@phpschool.com\";
출처 : http://man.phpschool.com/manual/en/ref.curl.php
fopen대용으로 사용해보세엽
메타소스데모: http://dnsolution.co.kr/dnmeta/naver.php
<?
class BrowserEmulator {
var $headerLines = Array();
var $postData = Array();
var $authUser = "";
var $authPass = "";
var $port;
var $lastResponse = Array();
var $debug = false;
function BrowserEmulator() {
$this->resetHeaderLines();
$this->resetPort();
}
function addHeaderLine($name, $value) {
$this->headerLines[$name] = $value;
}
function resetHeaderLines() {
$this->headerLines = Array();
/*******************************************************************************/
/************** YOU MAX SET THE USER AGENT STRING HERE *******************/
/* */
/* default is "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)", */
/* which means Internet Explorer 6.0 on WinXP */
$this->headerLines["User-Agent"] = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)";
/*******************************************************************************/
}
/**
* $url불러fopen을 만듦
* 값을부름. 모든헤드에 보냄, 데이터와사용자보안데이타보냄.
* 파일조정되었을시 되돌림, or 오류시없엠.
**/
function fopen($url) {
$this->lastResponse = Array();
preg_match("~([a-z]*://)?([^:^/]*)(:([0-9]{1,5}))?(/.*)?~i", $url, $matches);
$protocol = $matches[1];
$server = $matches[2];
$port = $matches[4];
$path = $matches[5];
if ($port!="") {
$this->setPort($port);
}
if ($path=="") $path = "/";
$socket = false;
$socket = fsockopen($server, $this->port);
if ($socket) {
$this->headerLines["Host"] = $server;
if ($this->authUser!="" && $this->authPass!="") {
$this->headerLines["Authorization"] = "Basic ".base64_encode($this->authUser.":".$this->authPass);
}
if (count($this->postData)==0) {
$request = "GET $path HTTP/1.0\r\n";
} else {
$request = "POST $path HTTP/1.0\r\n";
}
if ($this->debug) echo $request;
fputs ($socket, $request);
if (count($this->postData)>0) {
$PostStringArray = Array();
foreach ($this->postData AS $key=>$value) {
$PostStringArray[] = "$key=$value";
}
$PostString = join("&", $PostStringArray);
$this->headerLines["Content-Length"] = strlen($PostString);
}
foreach ($this->headerLines AS $key=>$value) {
if ($this->debug) echo "$key: $value\n";
fputs($socket, "$key: $value\r\n");
}
if ($this->debug) echo "\n";
fputs($socket, "\r\n");
if (count($this->postData)>0) {
if ($this->debug) echo "$PostString";
fputs($socket, $PostString."\r\n");
}
}
if ($this->debug) echo "\n";
if ($socket) {
$line = fgets($socket, 1000);
if ($this->debug) echo $line;
$this->lastResponse[] = $line;
$status = substr($line,9,3);
while (trim($line = fgets($socket, 1000)) != ""){
if ($this->debug) echo "$line";
$this->lastResponse[] = $line;
if ($status=="401" AND strpos($line,"WWW-Authenticate: Basic realm=\"")===0) {
fclose($socket);
return FALSE;
}
}
}
return $socket;
}
/**
* 포트추가. 포트파라메터값 HTTP POST 요구을 바디에 보냄.
**/
function addPostData($name, $value) {
$this->postData[$name] = $value;
}
/**
* 모든일반포트파라메터지움.
**/
function resetPostData() {
$this->postData = Array();
}
/**
* 사용자페스워드 사용요구.
* .
**/
function setAuth($user, $pass) {
$this->authUser = $user;
$this->authPass = $pass;
}
/**
* 사용자일반포트선택요구.
**/
function setPort($portNumber) {
$this->port = $portNumber;
}
/**
* 보낸사용자기본 HTTP default(80)리셋요구.
**/
function resetPort() {
$this->port = 80;
}
/**
* $url 불러파일을만듦.
* 값을부르고. 헤드보냄, 데이터와사용자보안.
* 요구파일성공적으로 정렬시 되돌림, 또는오류슘김.
**/
function file($url) {
$file = Array();
$socket = $this->fopen($url);
if ($socket) {
$file = Array();
while (!feof($socket)) {
$file[] = fgets($socket, 10000);
}
} else {
return FALSE;
}
return $file;
}
function getLastResponseHeaders() {
return $this->lastResponse;
}
}
/*
// 예제코드
$be = new BrowserEmulator();
$be->addHeaderLine("Referer", "http://dnsolution.co.kr/");
$be->addHeaderLine("Accept-Encoding", "x-compress; x-zip");
$be->addPostData("Submit", "OK");
$be->addPostData("item", "42");
$be->setAuth("admin", "secretpass");
// also possible:
// $be->setPort(10080);
$file = $be->fopen("http://dnsolution.co.kr/somepage.html");
$response = $be->getLastResponseHeaders();
while ($line = fgets($file, 1024)) {
// 파일width아무거나!
}
fclose($file);
*/
?>
--------------------------------------------------------------------------------------
fopen() 이나 fsockopen() 으로 열리지 않는 페이지는 file() 로 잘 열리더군요,
그리고 post 형식 form 페이지는 아래 함수로 잘 열립니다.
function HTTP_Post($URL,$data, $referrer=\"\") {
// parsing the given URL
$URL_Info=parse_url($URL);
// Building referrer
if($referrer==\"\") // if not given use this script as referrer
$referrer=$_SERVER[\"SCRIPT_URI\"];
// making string from $data
foreach($data as $key=>$value)
$values[]=\"$key=\".urlencode($value);
$data_string=implode(\"&\",$values);
// Find out which port is needed - if not given use standard (=80)
if(!isset($URL_Info[\"port\"]))
$URL_Info[\"port\"]=80;
// building POST-request:
$request.=\"POST \".$URL_Info[\"path\"].\" HTTP/1.1\\n\";
$request.=\"Host: \".$URL_Info[\"host\"].\"\\n\";
$request.=\"Referer: $referer\\n\";
$request.=\"Content-type: application/x-www-form-urlencoded\\n\";
$request.=\"Content-length: \".strlen($data_string).\"\\n\";
$request.=\"Connection: close\\n\";
$request.=\"\\n\";
$request.=$data_string.\"\\n\";
$fp = fsockopen($URL_Info[\"host\"],$URL_Info[\"port\"]);
fputs($fp, $request);
while(!feof($fp)) {
$result .= fgets($fp, 128);
}
fclose($fp);
return $result;
}
사용은
$output=HTTP_Post(\"http://www.server.com/script.php\",$_POST);
$_POST 는 post 폼에서 필요한 변수들의 배열입니다. 예를들면
$_POST[\"name\"] = \"php\";
$_POST[\"pwd\"] = \"school\";
$_POST[\"email\"] = \"webmaster@phpschool.com\";
출처 : http://man.phpschool.com/manual/en/ref.curl.php
관련자료
-
링크
댓글 0
등록된 댓글이 없습니다.