12-07-2005, 22:27
|
|
|
חבר מתאריך: 23.09.03
הודעות: 12
|
|
אפשר גם כך :
קוד PHP:
$fl = @fopen($url, "r"); if ($fl) { while ($buffer = @fgets($fl, 4096)) { $file .= $buffer; } fclose ($fl); } else { $contents = getFileCont($url); $file = $contents['file']; }
ופונקציה getFileCont
קוד PHP:
function getFileCont($url) { $urlparts = parse_url($url); $path = $urlparts['path']; $host = $urlparts['host']; if ($urlparts['query'] != "") $path .= "?".$urlparts['query']; if (isset($urlparts['port'])) { $port = (int)$urlparts['port']; } else if ($urlparts['scheme'] == "http") { $port = 80; } else if ($urlparts['scheme'] == "https") { $port = 443; } if ($port == 80) { $portq = ""; } else { $portq = ":$port"; } $all = "*/*"; $request = "GET $path HTTP/1.0\r\nHost: $host$portq\r\nAccept: $all\r\nAccept-Encoding: identity\r\nUser-Agent:\r\n\r\n";//User-Agent -->write your agent $fsocket_timeout = 30; if (substr($url,0,5)=="https") { $target = "ssl://".$url; } else { $target = $url; } $fp = @fsockopen($target, $port, $errno, $errstr, $fsocket_timeout); if (!$fp) { $contents['state'] = "NOHOST"; return $contents; } else { if( !fputs( $fp, $request)) { $contents['state'] = "Cannot send request"; return $contents; } $data = null; socket_set_timeout($fp, $fsocket_timeout); $status = socket_get_status($fp); while( !feof($fp) && !$status['timed_out']) { $data .= fgets ($fp,8192); } fclose ($fp); if ($status['timed_out'] == 1) { $contents['state'] = "timeout"; } else $contents['state'] = "ok"; $contents['file'] = substr($data, strpos($data,"\r\n\r\n") + 4);//CRLF(13 10)(\r\n) } return $contents; }
|