Last active
January 22, 2017 03:20
-
-
Save gracefullight/1da6fe2bd043a68beb4229e870f46d81 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<? | |
/** | |
* [postData 외부파일을 POST 방식으로 읽기] | |
* @param [string] $str [url] | |
* @param [array] $data [parameters] | |
* @param [int] $sleepMs [연결지연ms] | |
* @return [string] [내용] | |
*/ | |
function postData($str, $data, $sleepMs=0){ | |
$url = parse_url($str); | |
switch(strtoupper($url['scheme'])){ | |
case 'HTTP': | |
if (!isset($url['port'])){ | |
$url['port'] = 80; | |
} | |
break; | |
case 'HTTPS': | |
$url['ssl'] = 'ssl://'; | |
if (!isset($url['port'])){ | |
$url['port'] = 443; | |
} | |
break; | |
} | |
$data_string = http_build_query($data); | |
$referrer = $_SERVER['SCRIPT_URL']; | |
$request = "POST {$url[path]} HTTP/1.1\r\nHost: {$url[host]}\r\nReferer: {$referrer}\r\n"; | |
$request .= "Content-type: application/x-www-form-urlencoded\r\n"; | |
$request .= "Content-length: " . strlen($data_string) . "\r\n"; | |
$request .= "Connection: close\r\n\r\n"; | |
$request .= "{$data_string}\r\n"; | |
$fp = @fsockopen($url['ssl'].$url[host], $url[port], $errno, $errstr, 10); | |
if ($fp){ | |
fwrite($fp, $request); | |
usleep(($sleepMs * 1000)); | |
do { | |
$header .= fread($fp, 1); | |
} while (!preg_match('/\\r\\n\\r\\n$/', $header)); | |
if (preg_match('/Transfer\\-Encoding:\\s+chunked\\r\\n/', $header, $matches)) { | |
// check encoding | |
do { | |
$byte = $chunk_size = ""; | |
do { | |
$chunk_size .= $byte; $byte = fread($fp, 1); | |
} while ($byte != "\r"); | |
fread($fp, 1); | |
$chunk_size = hexdec($chunk_size); | |
if ($chunk_size){ | |
$out .= @fread($fp, $chunk_size); | |
} | |
fread($fp, 2); | |
} while ($chunk_size); | |
} else if (preg_match('/Content\\-Length:\\s+([0-9]*)\\r\\n/', $header, $matches)) { | |
$out = fread($fp,$matches[1]); | |
} else { | |
while (!feof($fp)) { | |
$out .= fread($fp, 4096); | |
} | |
} | |
fclose($fp); | |
} | |
return $out; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment