经过本地和一台WINDOWS服务器测试都很稳定。但在LINUX服务器上,fsockopen每次也是成功的,但fwrite却多数都失败,很久才成功一次。。谁遇到过这样的情况吗。。
另外//注释标记那一部分,谁能解释下。。代码如下:$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 <= 0;
}
}
}*/
//注释标记
@fclose($fp);
return true;
}

解决方案 »

  1.   

    !isset($matches['host']) && $matches['host'] = '';
        !isset($matches['path']) && $matches['path'] = '';
        !isset($matches['query']) && $matches['query'] = '';
        !isset($matches['port']) && $matches['port'] = '';请教这几句代码的意思?
      

  2.   


    /* 对于楼主的应用, 这里得到的是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 <= 0;
                    }
                }
            }
      

  3.   

    看下这个函数parse_url就明白啦
      

  4.   

    fwrite却多数都失败,很久才成功一次。
    你是怎么看出来的,是指的是代码中 @fwrite($fp, $out);这段代码吗添加 error_reporting(E_ALL);
    试试,看看有什么错误提示
      

  5.   

    fwrite应该就是模拟POST了,我在接收POST的文件里写日志看出来的。。
      

  6.   

    大概找到问题在哪里了,看代码注释。。求解释。。
    @fwrite($fp, $out);//在这里exit,问题就角度解决了。。
    /*$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 <= 0;
    }
    }
    }*/
    @fclose($fp);//但在这里exit却不行。。