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

WBOY
Release: 2016-06-13 10:35:04
Original
1086 people have browsed it

fsockopen成功,fwrite失败的原因有哪些?

经过本地和一台WINDOWS服务器测试都很稳定。

但在LINUX服务器上,fsockopen每次也是成功的,但fwrite却多数都失败,很久才成功一次。。

谁遇到过这样的情况吗。。
另外//注释标记那一部分,谁能解释下。。

代码如下:

PHP code
<!--Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->$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 
Copy after login





------解决方案--------------------
检查文件的读写性。
------解决方案--------------------
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 <font color="#e78608">------解决方案--------------------<div class="clear">
                 
              
              
        
            </div></font>
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template