您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

一个读取远程文件的PHP函数

2024/2/21 2:14:51发布48次查看
一个读取远程文件的函数,非常好用!
function urlfopen($url, $limit = 0, $post = '', $cookie = '', $bysocket = false , $ip = '', $timeout = 15, $block = true, $encodetype = 'urlencode') { $return = ''; $matches = parse_url($url); $host = $matches['host']; $path = $matches['path'] ? $matches['path'].(isset($matches['query']) ? '?'.$matches['query'] : '') : '/'; $port = !empty($matches['port']) ? $matches['port'] : 80; if($post) { $out = post $path http/1.0\r\n; $out .= accept: */*\r\n; $out .= accept-language: zh-cn\r\n; $boundary = $encodetype == 'urlencode' ? '' : ';'.substr($post, 0, trim(strpos($post, \n))); $out .= $encodetype == 'urlencode' ? content-type: application/x-www-form-urlencoded\r\n : content-type: multipart/form-data$boundary\r\n; $out .= user-agent: $_server[http_user_agent]\r\n; $out .= host: $host\r\n; $out .= 'content-length: '.strlen($post).\r\n; $out .= connection: close\r\n; $out .= cache-control: no-cache\r\n; $out .= cookie: $cookie\r\n\r\n; $out .= $post; } else { $out = get $path http/1.0\r\n; $out .= accept: */*\r\n; $out .= accept-language: zh-cn\r\n; $out .= user-agent: $_server[http_user_agent]\r\n; $out .= host: $host\r\n; $out .= referer: \r\n; $out .= connection: close\r\n; $out .= cookie: $cookie\r\n\r\n; } $fp = @fsockopen(($ip ? $ip : $host), $port, $errno, $errstr, $timeout); if(!$fp) { return ''; } else { stream_set_blocking($fp, $block); stream_set_timeout($fp, $timeout); @fwrite($fp, $out); $status = stream_get_meta_data($fp); if(!$status['timed_out']) { while (!feof($fp)) { if(($header = @fgets($fp)) && ($header == \r\n || $header == \n)) { break; } } $stop = false; while(!feof($fp) && !$stop) { $data = fread($fp, ($limit == 0 || $limit > 8192 ? 8192 : $limit)); $return .= $data; if($limit) { $limit -= strlen($data); $stop = $limit } } } @fclose($fp); return $return; } }
复制代码
php
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product