PHP에서 socket을 이용해 HTTP GET 요청하기.
function requestHTTP($url, $port=80)
{
$result = '';
$ip = gethostbyname(getDomainName($url));
$sock = socket_create(AF_INET, SOCK_STREAM, getprotobyname('tcp'));
$connected = @socket_connect($sock, $ip, $port);
$header = "GET ".getResourcePath($url)." HTTP/1.1\r\n";
$header .= "Host: ".$ip."\r\n";
$header .= "Accept: */*\r\n";
$header .= "Connection: close\r\n\r\n";
if ($connected) {
socket_write($sock, $header, strlen($header));
while ($buf = socket_read($sock, 1024)) $result .= $buf;
}
socket_close($sock);
return $result;
}
function getDomainName($url)
{
$temp = str_replace('http://', '', $url);
$result = explode("/", $temp);
return $result[0];
}
function getResourcePath($url)
{
$temp = str_replace('http://', '', $url);
$arr = explode("/", $temp);
$cnt = count($arr);
for ($i=1; $i<$cnt; $i++) $result .= '/'.$arr[$i];
return $result;
}
{
$result = '';
$ip = gethostbyname(getDomainName($url));
$sock = socket_create(AF_INET, SOCK_STREAM, getprotobyname('tcp'));
$connected = @socket_connect($sock, $ip, $port);
$header = "GET ".getResourcePath($url)." HTTP/1.1\r\n";
$header .= "Host: ".$ip."\r\n";
$header .= "Accept: */*\r\n";
$header .= "Connection: close\r\n\r\n";
if ($connected) {
socket_write($sock, $header, strlen($header));
while ($buf = socket_read($sock, 1024)) $result .= $buf;
}
socket_close($sock);
return $result;
}
function getDomainName($url)
{
$temp = str_replace('http://', '', $url);
$result = explode("/", $temp);
return $result[0];
}
function getResourcePath($url)
{
$temp = str_replace('http://', '', $url);
$arr = explode("/", $temp);
$cnt = count($arr);
for ($i=1; $i<$cnt; $i++) $result .= '/'.$arr[$i];
return $result;
}
'PHP' 카테고리의 다른 글
[PHP] Comet with PHP+iframe (0) | 2009.07.05 |
---|---|
[PHP] 문자열 인코딩 체크방법 (2) | 2009.06.18 |
PHP 스크립트를 데몬으로 실행하기 (0) | 2009.06.04 |
[PHP] ImageMagick (0) | 2009.05.29 |
PHP에서 tif 파일을 jpg 로 변환하기 (0) | 2009.05.28 |