01 function urlfopen($url, $limit = 0, $post = '', $cookie = '', $bysocket = false , $ip = '', $timeout = 15, $block = true, $encodetype = 'urlencode') {
02 $return = '';
03 $matches = parse_url($url);
04 $host = $matches['host'];
05 $path = $matches['path'] ? $matches['path'].(isset($matches['query']) ? '?'.$matches['query'] : '') : '/';
06 $port = !empty($matches['port']) ? $matches['port'] : 80;
07
08 if($post) {
09 $out = post $path http/1.0\r\n;
10 $out .= accept: */*\r\n;
11 $out .= accept-language: zh-cn\r\n;
12 $boundary = $encodetype == 'urlencode' ? '' : ';'.substr($post, 0, trim(strpos($post, \n)));
13 $out .= $encodetype == 'urlencode' ? content-type: application/x-www-form-urlencoded\r\n : content-type: multipart/form-data$boundary\r\n;
14 $out .= user-agent: $_server[http_user_agent]\r\n;
15 $out .= host: $host\r\n;
16 $out .= 'content-length: '.strlen($post).\r\n;
17 $out .= connection: close\r\n;
18 $out .= cache-control: no-cache\r\n;
19 $out .= cookie: $cookie\r\n\r\n;
20 $out .= $post;
21 } else {
22 $out = get $path http/1.0\r\n;
23 $out .= accept: */*\r\n;
24 $out .= accept-language: zh-cn\r\n;
25 $out .= user-agent: $_server[http_user_agent]\r\n;
26 $out .= host: $host\r\n;
27 $out .= referer: \r\n;
28 $out .= connection: close\r\n;
29 $out .= cookie: $cookie\r\n\r\n;
30 }
31 $fp = @fsockopen(($ip ? $ip : $host), $port, $errno, $errstr,$timeout);
32 if(!$fp) {
33 return '';
34 } else {
35 stream_set_blocking($fp, $block);
36 stream_set_timeout($fp, $timeout);
37 @fwrite($fp, $out);
38 $status = stream_get_meta_data($fp);
39 if(!$status['timed_out']) {
40 while (!feof($fp)) {
41 if(($header = @fgets($fp)) && ($header == \r\n $header ==\n)) {
42 break;
43 }
44 }
45
46 $stop = false;
47 while(!feof($fp) && !$stop) {
48 $data = fread($fp, ($limit == 0 $limit > 8192 ? 8192 :$limit));
49 $return .= $data;
50 if($limit) {
51 $limit -= strlen($data);
52 $stop = $limit
53 }
54 }
55 }
56 @fclose($fp);
57 return $return;
58 }
59 }
