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

fsockopen顺利,fwrite失败的原因有哪些

2024/6/30 2:53:51发布45次查看
fsockopen成功,fwrite失败的原因有哪些?
经过本地和一台windows服务器测试都很稳定。
但在linux服务器上,fsockopen每次也是成功的,但fwrite却多数都失败,很久才成功一次。。
谁遇到过这样的情况吗。。
另外//注释标记那一部分,谁能解释下。。
代码如下:
php code$matches = parse_url($url); !isset($matches['host']) && $matches['host'] = ''; !isset($matches['path']) && $matches['path'] = ''; !isset($matches['query']) && $matches['query'] = ''; !isset($matches['port']) && $matches['port'] = ''; $host = $matches['host']; $path = $matches['path'] ? $matches['path'].($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; $out .= content-type: application/x-www-form-urlencoded\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 .= connection: close\r\n; $out .= cookie: $cookie\r\n\r\n; } $fp = @fsockopen(($ip ? $ip : $host), $port, $errno, $errstr, $timeout); if(!$fp) { return '';//note $errstr : $errno \r\n } else { stream_set_blocking($fp, $block); stream_set_timeout($fp, $timeout);var_dump($fp); @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
------解决方案--------------------
检查文件的读写性。
------解决方案--------------------
php code/* 对于楼主的应用, 这里得到的是tcp套接字的一些信息 */ $status = stream_get_meta_data($fp);/* 这里是检查tcp连接是否超时 */ if(!$status['timed_out']) {/* 如果没有超时, 那么就读取数据, 直到碰到文件结束符 */ while (!feof($fp)) {/* 由于这里是读取http头信息, 空行标识头信息结束, 因此需要break */ if(($header = @fgets($fp)) && ($header == \r\n || $header == \n)) { break; } }/* 这里是读取http的body, 个人认为这里的处理有一些粗糙, 至少先看看http响应头中有没有content-length根据它进行处理, 如果没有再如下读取 */ $stop = false; while(!feof($fp) && !$stop) {/* 读取最多8192字节($limit递减) */ $data = fread($fp, ($limit == 0 || $limit > 8192 ? 8192 : $limit));/* 将读取到的内容连接到返回字符串中 */ $return .= $data; if($limit) { $limit -= strlen($data); $stop = $limit ------解决方案--------------------

该用户其它信息

VIP推荐

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