socketÀÌ¿ëÇÑ Å¸»çÀÌÆ® À̹ÌÁö Àбâ |
|
|
 |
13³â Àü |
function fetch_url($url) {
$url_parsed = parse_url($url);
$host = $url_parsed["host"];
$port = $url_parsed["port"];
if($port==0) $port = 80;
$path = $url_parsed["path"];
if(empty($path)) $path = "/";
if(empty($host)) return false;
if($url_parsed["query"] != "") $path .= "?".$url_parsed["query"];
$out = "GET ".$path." HTTP/1.0\r\nHost: ".$host."\r\n\r\n";
$fp = fsockopen($host, $port, $errno, $errstr, 30);
usleep(50);
if($fp) {
socket_set_timeout($fp, 30);
fwrite($fp, $out);
$body = false;
while(!feof($fp)) {
$buffer = fgets($fp, 128);
if($body) $content .= $buffer;
if($buffer=="\r\n") $body = true;
}
fclose($fp);
}else {
return false;
}
return $content;
}
$image = fetch_url("url");
$f = fopen("./data/test.jpg","w");
$lock=flock($f,2);
if($lock) {
fwrite($f,$image);
}
flock($f,3);
fclose($f);
|
|
̵̧ : 684 |
̵̧
¸ñ·Ï
|
|