//最近在学习DZ的源代码,因为以前没有php基础
//所以很费劲,下边的方法是UCenter中的一个方法,我看了半天没有明白是做什么
//麻烦大家帮帮忙,解释解释
function dfopen($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'].($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 .= "Referer: $boardurl\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 .= "Referer: $boardurl\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里边存储的是什么?
$fp = @fsockopen(($ip ? $ip : $host), $port, $errno, $errstr, $timeout);
if(!$fp) {
return '';
} else {
stream_set_blocking($fp, $block);
stream_set_timeout($fp, $timeout);
//这句是把字符串out的内容写入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 $return;
}
}

解决方案 »

  1.   

    fp里面存的是一个句柄,句柄是一种指向指针的指针。fwrite就是向地址写数据,这里是向http服务器发起一个请求,提交的是http的头
    告诉服务器,请求的方式和一些参数。然后http服务器会把你的请求返回结果给你。这时候,你就可以用fread来读取结果了
      

  2.   

    手册中写道 fsockopen() returns a file pointer which may be used together with the other file functions (such as fgets(), fgetss(), fwrite(), fclose(), and feof()).
    其实就是用soket 链接打开一个网络资源 ,
    fwrite($fp, $out); 这句就是向$fp = @fsockopen(($ip ? $ip : $host), $port, $errno, $errstr, $timeout); 中的ip或host发送数据
    $data = fread($fp, ($limit == 0 || $limit > 8192 ? 8192 : $limit)); 这句就是读取返回的数据
      

  3.   

    明白些了,这个应该就是所谓的两个web程序之间的通信,是吗?
      

  4.   

    LZ 可以查一下 fsockopen
      

  5.   

    LZ 可以查一下 fsockopen
      

  6.   

    是的,ucenter和其他程序如discuz和ucenterhome的test接口用的就是这个方法
      

  7.   

    fsockopen除了和其他web程序通讯,还可以和其他sock通讯。应用程序,服务段,只要有sock端口的都可以
      

  8.   

    fsockopen除了和其他web程序通讯,还可以和其他sock通讯。应用程序,服务段,只要有sock端口的都可以
      

  9.   

    其实就是 socket 连接, 组的包连过去,看看http协议