本帖最后由 lovewangya 于 2011-10-24 15:13:37 编辑

解决方案 »

  1.   

    $this->headers["Request-Line"] = "Request-Line: {$this->method} {$path} HTTP/{$this->httpVersion}";
    $this->debug("请求头信息(<span style=color:blue>stream</span>)",implode("\r\n",$this->headers) ."\r\n\r\n".$data);
    for($i=1;$i<=$this->Retry;$i++)
    {
    $result = @file_get_contents($path,FALSE,$stream_context);
    $this->header = @$http_response_header;
    if($this->header) break;
    }
    if(!$this->header) return false;
    $this->header = implode("\r\n",$http_response_header);
    $this->putCookie($this->header);
    $this->debug("响应头信息(<span style=color:blue>stream</span>)",$this->header);
    if(preg_match("/Location: (\S+)/i",$this->header,$match) && $jump)
    {
      $jumpUrl = $match[1];
      $jumpUrl = strstr($jumpUrl,"http") ? $jumpUrl : getUrlPath($path) . $jumpUrl;
      return $this->get($jumpUrl,$path);
    }
    $returnContent =  $this->safeEncoding($this->header,$result,$returnCode);
    $this->msg .= $returnContent . "\r\n" . str_repeat("-", 167) . "\r\n";
    return $returnContent;
    }
    function writeCookie()
    {
    $cookieArray = unserialize(@file_get_contents($this->cookieFile));
    if($cookieArray)
    {
    $newCookie = array();
    foreach($cookieArray as $key=>$val)
    {
    $newCookie[] = "{$key}={$val}";
    }
    $this->headers["Cookie"] = "Cookie: " . implode(";",$newCookie);
    }
    }
    function encodePostData($data)
    {
    //也可以用这个函数来实现:http_build_query
    $encoded = "";
    $data = explode("&",$data);
    foreach($data as $val)
    {
    $temp = explode("=",$val);
    $encoded .= $encoded ? "&" : "";
    $encoded .= rawurlencode($temp[0]) . "=" . rawurlencode(@$temp[1]);
    }
    return $encoded;
    }
    function addCookie($newCookie)
    {
    if(!$this->cookieFile) return;
    if(!is_array($newCookie))
    {
    $tempArray = explode(";",$newCookie);
    $tempArray = array_filter($tempArray,create_function( '$v ', 'return   !empty($v); ')); 
    $newCookie = array();
    foreach($tempArray as $val)
    {
    $tempArray_1 = explode("=",$val);
    $newCookie[$tempArray_1[0]] = $tempArray_1[1];
    }
    }
    $oldCookie = unserialize(@file_get_contents($this->cookieFile));
        if($oldCookie)
    {
    file_put_contents($this->cookieFile,serialize(array_merge_1($oldCookie,$newCookie)));
    }
    else
    {
    file_put_contents($this->cookieFile,serialize($newCookie));
    }
    }
    function putCookie($result)
    {
    preg_match_all("/Set-Cookie: (.*?)=(.*?);/",$result,$match);
    if($match[1])
    {
    $newCookie = array();
    foreach($match[1] as $key=>$val)
    {
    $newCookie[$val] = $match[2][$key];
    }
    $this->addCookie($newCookie);
    }
    }
    function parseUrl($path)
    {
    $path = $this->formatUrl($path);
    $path = parse_url($path);
    if(!@$path["host"]) die("解析url错误!<br/>" . print_r($path));
    if(@$path["scheme"]) $this->scheme= $path["scheme"];
    if(@$path["host"])   $this->host  = $path["host"];
    if(@$path["port"])   $this->port  = $path["port"];
    if(@$path["query"])  $this->query = $path["query"];
    if(@$path["path"])   $this->path  = $path["path"];
    if(strtolower($this->scheme)=="https") $this->port = 443;
    }
    function formatUrl($url)
    {
    $inUrl = array();
    $inUrl[] = "http://reg.163.com/services/getid";
    if(in_array($url,$inUrl))
    {
    return $url;
    }

    $tempUrl = str_replace("//","",$url);
    if(strstr($tempUrl,"/"))
    {
    $last = substr($url,strripos($url,"/")+1);
    if((!strstr($last, '.') && !strstr($last,'?') && $last))
    {
    $url = $url . "/";
    }
    }
    else
    {
    $url = $url . "/";
    }
    return $url;
    }
    function debug($msg, $object = false)
    {
    $this->msg .=  strip_tags("{$msg}\r\n{$object}\r\n" .  str_repeat("-", 167) . "\r\n");
    if ($this->debug)
    {
    print "<div style='border: 1px solid red; padding: 0.5em; margin: 0.5em;'><strong>{$this->host}:</strong> ".$msg;
    if ($object)
    {
    ob_start();
    print_r($object);
    $content = htmlentities(ob_get_contents());
    ob_end_clean();
    print '<pre>'.$content.'</pre>';
    }
    print '</div>';
    }
    }
    function readCurlCookie()
    {
    $returnArray = array();
    $cookies = @file_get_contents($this->cookieFile);
    if(preg_match("/([\s\S]*?\.)\r\n([\s\S]*+)/i",$cookies,$match))
    {
    $returnArray[0] = $match[1];
    $newCookies = array();
    $cookies = $match[2];
    if(preg_match_all("/(.*?) (.*?) (.*?) (.*?) (.*?) (.*?) (.*?)\r\n/",$cookies,$match))
    {
    foreach($match[1] as $key=>$val)
    {
    $newCookies[$match[6][$key]] = array($match[1][$key],$match[2][$key],$match[3][$key],$match[4][$key],$match[5][$key],$match[6][$key],$match[7][$key]);
    $returnArray[2][] = $newCookies[$match[6][$key]];
    }
    $returnArray[1] = $newCookies;
    }
    }
    return $returnArray;
    }
    function readCurlCookieDebug()
    {
    $returnArray = array();
    $readCurlCookie = $this->readCurlCookie();
    if(!$readCurlCookie) return false;
    foreach($readCurlCookie[2] as $key=>$val)
    {
    $returnArray[] = "{$val[5]}={$val[6]}";
    }
    return $returnArray;
    }
    function addCurlCookie($cookie)
    {
    $readCurlCookie = $this->readCurlCookie();
    if(!$readCurlCookie)
    {
    $readCurlCookie[0] = "# Netscape HTTP Cookie File\r\n# http://curlm.haxx.se/rfc/cookie_spec.html\r\n# This file was generated by libcurl! Edit at your own risk.";
    }
    $domain = $this->cookieDomain ? $this->cookieDomain : $this->host;
    $cookie = explode(";",$cookie);
    $cookie = array_filter($cookie,create_function( '$v ', 'return   !empty($v); ')); 
    foreach($cookie as $val)
    {
    $tempCookie = explode("=",$val);
    $readCurlCookie[1][$tempCookie[0]] = array($domain,"FALSE","/","FALSE","9999999999",$tempCookie[0],$tempCookie[1]);
    }
    return $this->updateCurlCookie($readCurlCookie);
    }
    function updateCurlCookie($array)
    {
    $newCookie = $array[0] . "\r\n\r\n";
    foreach($array[1] as $key=>$val)
    {
    $newCookie .= implode(" ",$array[1][$key]) . "\r\n";
    }
    file_put_contents($this->cookieFile,$newCookie);
    return true;
    }
    function extendsCookieTime()
    {
    $readCurlCookie = $this->readCurlCookie();
    if(!$readCurlCookie) return false;
    foreach($readCurlCookie[1] as $key=>$val)
    {
    if($readCurlCookie[1][$key][4] < 9999999999) 
    {
    $readCurlCookie[1][$key][4] = 9999999999;
    }
    }
    return $this->updateCurlCookie($readCurlCookie);
    }
    function safeEncoding($header,$body,$code="")
    {
    if($code)
    {
    $code = $code;
    }
    else if(preg_match("/charset=(\S+)/i",$header,$match))
    {
    $code = $match[1];  
    }
    else if(preg_match("/<meta(?:.[^>]*)charset\s*=([a-zA-Z0-9-_]+)/i",$body,$match))
    {
    $code = $match[1];  
    }
    else
    {
    $code = strtolower(checkEncoding($body));
    }
    $code = strtolower($code);
    return ($code == "utf-8" ? $body : @iconv($code,"utf-8//IGNORE",$body));
    }
    }
    ?>
      

  2.   

    php的属性默认就是public,  这关键字简直可以删掉, 可以提高可读性.
      

  3.   


    没听说过删除这个还可以提高可读性强烈建议加上public,包括function,这样可读性更高,对于以后的PHP版本也会更兼容
      

  4.   

    楼主 能否看下我的疑问
    http://topic.csdn.net/u/20111025/10/e53a0cb6-2cd1-4ba9-8ef9-d97249267300.html?83033
      

  5.   

    看的太累了,。帮你格式化一下
    <?php
    class Http
    {
    /* get($param_path,$param_referer="",$param_cookie="",$param_jump=1,$param_returnCode="",$param_data="") */
    public $toolType = 1;  //1:curl,2:socket,3:Stream
    public $httpVersion = "1.0";  //http协议版本(注意:curl,socket 能随意使用1.0/1.1 stream版本只能使用默认)
    public $cookieFile;  //存放Cookie信息的文件名称和路径
    public $headers = array();  //头部信息(发送的)
    public $header;  //头部信息(接收的)
    public $scheme;  //http/https
    public $host;  // 主机
    public $port = 80;  //主机端口
    public $query;  //查询字符串
    public $path;  //路径
    public $method;  //GET或者POST
    public $timeout = 100;  //超时时间
    public $encode = true;  //post数据是否重新编码
    public $Retry = 3;  //重试次数
    public $cookieDomain;  //curl添加cookie的时候用到的
    public $debug;  //是否调试
    public $msg;  //所有信息报告 
    function __construct($type=1)
    {
    $this->toolType = $type;
    $this->headers["Request-Line"] = "";
    $this->headers["Accept"] = "Accept: image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/QVOD, application/QVOD, */*";
    $this->headers["Accept-Encoding"] = "";//Accept-Encoding: gzip, deflate
    $this->headers["Accept-Language"] = "Accept-Language: zh-cn";//Accept-Language: zh-cn
    $this->headers["Cache-Control"] = "";//Cache-Control: no-cache
    $this->headers["Connection"] = "";//Connection: Keep-Alive
    $this->headers["Content-Length"] = "";//Content-Length: 100
    $this->headers["Content-Type"] = "";//Content-Type: application/x-www-form-urlencoded
    $this->headers["Cookie"] = "";//Cookie: key=val;key=val;
    $this->headers["Host"] = "";//Host: 127.0.0.1
    $this->headers["Referer"] = "";//Referer: http://www.baidu.com
    $this->headers["User-Agent"] = "User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) ; EmbeddedWB 14.52 from: http://www.bsalsa.com/ EmbeddedWB 14.52; .NET CLR 2.0.50727)";
    }
    function get($path,$referer="",$cookie="",$jump=1,$returnCode="",$data="")
    {
    $this->method = 'GET';
    switch ($this->toolType)
    {
    case 1: return $this->Request_curl($path,$referer,$cookie,$jump,$returnCode,$data);break;
    case 2: return $this->Request_socket($path,$referer,$cookie,$jump,$returnCode,$data);break;
    case 3: return $this->Request_stream($path,$referer,$cookie,$jump,$returnCode,$data);break;
    }
    }
    function post($path,$referer="",$cookie="",$jump=1,$returnCode="",$data="")
    {
    $this->method = 'POST';
    switch ($this->toolType)
    {
    case 1: return $this->Request_curl($path,$referer,$cookie,$jump,$returnCode,$data);break;
    case 2: return $this->Request_socket($path,$referer,$cookie,$jump,$returnCode,$data);break;
    case 3: return $this->Request_stream($path,$referer,$cookie,$jump,$returnCode,$data);break;
    }
    }
    function Request_curl($path,$referer="",$cookie="",$jump=1,$returnCode="",$data="")
    {
    $path = iconv("utf-8","gb2312",$path);
    $this->parseUrl($path);
    if($data && $this->encode) $data = $this->encodePostData($data);
    if($cookie) $this->addCurlCookie($cookie);
    if($referer) $this->headers["Referer"] = "Referer: {$referer}";
    $ch = curl_init();
    curl_setopt($ch,CURLOPT_HTTP_VERSION,($this->httpVersion == "1.0" ? CURL_HTTP_VERSION_1_0 : CURL_HTTP_VERSION_1_1) );
    curl_setopt($ch,CURLOPT_URL,$path);
    curl_setopt($ch,CURLOPT_TIMEOUT,$this->timeout); // 设置超时限制防止死循环
    curl_setopt($ch,CURLOPT_AUTOREFERER,true); // 自动设置Referer
    curl_setopt($ch,CURLOPT_FOLLOWLOCATION,$jump);
    curl_setopt($ch,CURLOPT_HEADER,1);
    curl_setopt($ch, CURLOPT_NOBODY,false);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);//设定返回的数据是否自动显示
    curl_setopt($ch,CURLOPT_COOKIEJAR,$this->cookieFile);
    curl_setopt($ch,CURLOPT_COOKIEFILE,$this->cookieFile);
    $this->headers = array_filter($this->headers,create_function( '$v ', 'return !empty($v); '));
    curl_setopt($ch,CURLOPT_HTTPHEADER,$this->headers);
    if($this->method == "POST")
    {
    curl_setopt($ch,CURLOPT_POST,1);
    curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
    }
    /*--------------设置https------------------------------*/
    if(strtolower($this->scheme) == "https")
    {
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,2);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,FALSE);
    }
    /*--------------调试------------------------------------*/
    $this->headers["Request-Line"] = "Request-Line: {$this->method} {$path} HTTP/{$this->httpVersion}";
    $debugHeaders = $this->headers;
    if($t = $this->readCurlCookieDebug()) $debugHeaders = array_merge($debugHeaders,array("Cookie: " . implode(";",$t)));
    $this->debug("请求头信息(<span style=color:blue>CURL</span>)",implode("\r\n",$debugHeaders) ."\r\n\r\n".$data);
    for($i=1;$i<=$this->Retry;$i++)
    {
    $info = curl_exec($ch);
    if($info) break;
    }
    curl_close($ch);
    if(!$info) return false;
    $this->extendsCookieTime();//延长cookie使用时间
    if(preg_match("/([\s\S]*?)\r\n\r\n([\s\S]*)/is",$info,$match))
    {
    $this->header = $match[1];
    $this->debug("响应头信息(<span style=color:blue>CURL</span>)",$this->header);
    $returnContent = $this->safeEncoding($this->header,$match[2],$returnCode);
    $this->msg .= $returnContent . "\r\n" . str_repeat("-", 167) . "\r\n";
    return $returnContent;
    }
    return false;
    }
    function Request_socket($path,$referer="",$cookie="",$jump=1,$returnCode="",$data="")
    {
    $path = iconv("utf-8","gb2312",$path);
    $this->parseUrl($path);
    if($data && $this->encode) $data = $this->encodePostData($data);
    if($cookie) $this->addCookie($cookie);
    $this->writeCookie();//给$this->headers["Cookie"]赋值
    $this->headers["Host"] = "Host: {$this->host}";
    if($referer) $this->headers["Referer"] = "Referer: {$referer}";
    if ($data && $this->method == "POST")
    {
    $this->headers["Content-Type"] = ($this->headers["Content-Type"] ? $this->headers["Content-Type"] : 'Content-Type: application/x-www-form-urlencoded');
    $this->headers["Content-Length"] = 'Content-Length: '.strlen($data);
    }
    else
    {
    $this->headers["Content-Type"] = "";
    $this->headers["Content-Length"] = "";
    $data = "";
    }
    $this->headers["Request-Line"] = "{$this->method} " . $this->path . (($this->query) ? "?" : "") . $this->query . " HTTP/{$this->httpVersion}";
    $this->headers = array_filter($this->headers,create_function( '$v ', 'return !empty($v); '));
    for($i=1;$i<=$this->Retry;$i++)
    {
    $fp = @fsockopen((strtolower($this->scheme) == "https" ? "ssl://" : "") . $this->host,$this->port,$errno,$errstr,$this->timeout);
    if($fp) break;
    }
    if(!$fp) return false;
    socket_set_timeout($fp, $this->timeout);
    $request = implode("\r\n", $this->headers)."\r\n\r\n".$data;
    $this->debug("请求头信息(<span style=color:blue>SOCKET</span>)",$request);
    fwrite($fp, $request);
    $result = "";
    $header = "";
    $inheader = 1;
    while(!feof($fp))
    {
    $line = fgets($fp,1024);
    if($inheader && ($line == "\n" || $line == "\r\n")){$inheader = 0;}
    elseif(!$inheader){$result .= $line;}
    else{$header .= $line;}
    }
    fclose($fp);
    $this->putCookie($header);
    $this->header = $header;
    $this->debug("响应头信息(<span style=color:blue>SOCKET</span>)",$header);
    if(preg_match("/Location: (\S+)/i",$header,$match) && $jump)
    {
    $jumpUrl = $match[1];
    $jumpUrl = strstr($jumpUrl,"http") ? $jumpUrl : getUrlPath($path) . $jumpUrl;
    return $this->get($jumpUrl,$path);
    }
    $returnContent = $this->safeEncoding($this->header,$result,$returnCode);
    $this->msg .= $returnContent . "\r\n" . str_repeat("-", 167) . "\r\n";
    return $returnContent;
    }
    function Request_stream($path,$referer="",$cookie="",$jump=1,$returnCode="",$data="")
    {
    $path = iconv("utf-8","gb2312",$path);
    $this->parseUrl($path);
    if($data && $this->encode) $data = $this->encodePostData($data);
    if($cookie) $this->addCookie($cookie);
    $this->writeCookie();//给$this->headers["Cookie"]赋值
    $this->headers["Host"] = "Host: {$this->host}";
    if($referer) $this->headers["Referer"] = "Referer: {$referer}";
    if ($data && $this->method == "POST")
    {
    $this->headers["Content-Type"] = ($this->headers["Content-Type"] ? $this->headers["Content-Type"] : 'Content-Type: application/x-www-form-urlencoded');
    $this->headers["Content-Length"] = 'Content-Length: '.strlen($data);
    }
    else
    {
    $this->headers["Content-Type"] = "";
    $this->headers["Content-Length"] = "";
    $data = "";
    }
    $this->headers = array_filter($this->headers,create_function( '$v ', 'return !empty($v); '));
    $context = array(
    "http"=>array(
    "method" =>$this->method,
    "header" =>implode("\r\n",$this->headers),
    "timeout"=>$this->timeout,
    "content"=>$data)
    );
    $stream_context = stream_context_create($context);

      

  6.   


    /*--------------调试------------------------------------*/
    $this->headers["Request-Line"] = "Request-Line: {$this->method} {$path} HTTP/{$this->httpVersion}";
    $this->debug("请求头信息(<span style=color:blue>stream</span>)",implode("\r\n",$this->headers) ."\r\n\r\n".$data);
    for($i=1;$i<=$this->Retry;$i++)
    {
    $result = @file_get_contents($path,FALSE,$stream_context);
    $this->header = @$http_response_header;
    if($this->header) break;
    }
    if(!$this->header) return false;
    $this->header = implode("\r\n",$http_response_header);
    $this->putCookie($this->header);
    $this->debug("响应头信息(<span style=color:blue>stream</span>)",$this->header);
    if(preg_match("/Location: (\S+)/i",$this->header,$match) && $jump)
    {
    $jumpUrl = $match[1];
    $jumpUrl = strstr($jumpUrl,"http") ? $jumpUrl : getUrlPath($path) . $jumpUrl;
    return $this->get($jumpUrl,$path);
    }
    $returnContent = $this->safeEncoding($this->header,$result,$returnCode);
    $this->msg .= $returnContent . "\r\n" . str_repeat("-", 167) . "\r\n";
    return $returnContent;
    }
    function writeCookie()
    {
    $cookieArray = unserialize(@file_get_contents($this->cookieFile));
    if($cookieArray)
    {
    $newCookie = array();
    foreach($cookieArray as $key=>$val)
    {
    $newCookie[] = "{$key}={$val}";
    }
    $this->headers["Cookie"] = "Cookie: " . implode(";",$newCookie);
    }
    }
    function encodePostData($data)
    {
    //也可以用这个函数来实现:http_build_query
    $encoded = "";
    $data = explode("&",$data);
    foreach($data as $val)
    {
    $temp = explode("=",$val);
    $encoded .= $encoded ? "&" : "";
    $encoded .= rawurlencode($temp[0]) . "=" . rawurlencode(@$temp[1]);
    }
    return $encoded;
    }
    function addCookie($newCookie)
    {
    if(!$this->cookieFile) return;
    if(!is_array($newCookie))
    {
    $tempArray = explode(";",$newCookie);
    $tempArray = array_filter($tempArray,create_function( '$v ', 'return !empty($v); '));
    $newCookie = array();
    foreach($tempArray as $val)
    {
    $tempArray_1 = explode("=",$val);
    $newCookie[$tempArray_1[0]] = $tempArray_1[1];
    }
    }
    $oldCookie = unserialize(@file_get_contents($this->cookieFile));
    if($oldCookie)
    {
    file_put_contents($this->cookieFile,serialize(array_merge_1($oldCookie,$newCookie)));
    }
    else
    {
    file_put_contents($this->cookieFile,serialize($newCookie));
    }
    }
    function putCookie($result)
    {
    preg_match_all("/Set-Cookie: (.*?)=(.*?);/",$result,$match);
    if($match[1])
    {
    $newCookie = array();
    foreach($match[1] as $key=>$val)
    {
    $newCookie[$val] = $match[2][$key];
    }
    $this->addCookie($newCookie);
    }
    }
    function parseUrl($path)
    {
    $path = $this->formatUrl($path);
    $path = parse_url($path);
    if(!@$path["host"]) die("解析url错误!<br/>" . print_r($path));
    if(@$path["scheme"]) $this->scheme= $path["scheme"];
    if(@$path["host"]) $this->host = $path["host"];
    if(@$path["port"]) $this->port = $path["port"];
    if(@$path["query"]) $this->query = $path["query"];
    if(@$path["path"]) $this->path = $path["path"];
    if(strtolower($this->scheme)=="https") $this->port = 443;
    }
    function formatUrl($url)
    {
    $inUrl = array();
    $inUrl[] = "http://reg.163.com/services/getid";
    if(in_array($url,$inUrl))
    {
    return $url;
    } $tempUrl = str_replace("//","",$url);
    if(strstr($tempUrl,"/"))
    {
    $last = substr($url,strripos($url,"/")+1);
    if((!strstr($last, '.') && !strstr($last,'?') && $last))
    {
    $url = $url . "/";
    }
    }
    else
    {
    $url = $url . "/";
    }
    return $url;
    }
    function debug($msg, $object = false)
    {
    $this->msg .= strip_tags("{$msg}\r\n{$object}\r\n" . str_repeat("-", 167) . "\r\n");
    if ($this->debug)
    {
    print "<div style='border: 1px solid red; padding: 0.5em; margin: 0.5em;'><strong>{$this->host}:</strong> ".$msg;
    if ($object)
    {
    ob_start();
    print_r($object);
    $content = htmlentities(ob_get_contents());
    ob_end_clean();
    print '<pre>'.$content.'</pre>';
    }
    print '</div>';
    }
    }
    function readCurlCookie()
    {
    $returnArray = array();
    $cookies = @file_get_contents($this->cookieFile);
    if(preg_match("/([\s\S]*?\.)\r\n([\s\S]*+)/i",$cookies,$match))
    {
    $returnArray[0] = $match[1];
    $newCookies = array();
    $cookies = $match[2];
    if(preg_match_all("/(.*?) (.*?) (.*?) (.*?) (.*?) (.*?) (.*?)\r\n/",$cookies,$match))
    {
    foreach($match[1] as $key=>$val)
    {
    $newCookies[$match[6][$key]] = array($match[1][$key],$match[2][$key],$match[3][$key],$match[4][$key],$match[5][$key],$match[6][$key],$match[7][$key]);
    $returnArray[2][] = $newCookies[$match[6][$key]];
    }
    $returnArray[1] = $newCookies;
    }
    }
    return $returnArray;
    }
    function readCurlCookieDebug()
    {
    $returnArray = array();
    $readCurlCookie = $this->readCurlCookie();
    if(!$readCurlCookie) return false;
    foreach($readCurlCookie[2] as $key=>$val)
    {
    $returnArray[] = "{$val[5]}={$val[6]}";
    }
    return $returnArray;
    }
    function addCurlCookie($cookie)
    {
    $readCurlCookie = $this->readCurlCookie();
    if(!$readCurlCookie)
    {
    $readCurlCookie[0] = "# Netscape HTTP Cookie File\r\n# http://curlm.haxx.se/rfc/cookie_spec.html\r\n# This file was generated by libcurl! Edit at your own risk.";
    }
    $domain = $this->cookieDomain ? $this->cookieDomain : $this->host;
    $cookie = explode(";",$cookie);
    $cookie = array_filter($cookie,create_function( '$v ', 'return !empty($v); '));
    foreach($cookie as $val)
    {
    $tempCookie = explode("=",$val);
    $readCurlCookie[1][$tempCookie[0]] = array($domain,"FALSE","/","FALSE","9999999999",$tempCookie[0],$tempCookie[1]);
    }
    return $this->updateCurlCookie($readCurlCookie);
    }
    function updateCurlCookie($array)
    {
    $newCookie = $array[0] . "\r\n\r\n";
    foreach($array[1] as $key=>$val)
    {
    $newCookie .= implode(" ",$array[1][$key]) . "\r\n";
    }
    file_put_contents($this->cookieFile,$newCookie);
    return true;
    }
    function extendsCookieTime()
    {
    $readCurlCookie = $this->readCurlCookie();
    if(!$readCurlCookie) return false;
    foreach($readCurlCookie[1] as $key=>$val)
    {
    if($readCurlCookie[1][$key][4] < 9999999999)
    {
    $readCurlCookie[1][$key][4] = 9999999999;
    }
    }
    return $this->updateCurlCookie($readCurlCookie);
    }
    function safeEncoding($header,$body,$code="")
    {
    if($code)
    {
    $code = $code;
    }
    else if(preg_match("/charset=(\S+)/i",$header,$match))
    {
    $code = $match[1];
    }
    else if(preg_match("/<meta(?:.[^>]*)charset\s*=([a-zA-Z0-9-_]+)/i",$body,$match))
    {
    $code = $match[1];
    }
    else
    {
    $code = strtolower(checkEncoding($body));
    }
    $code = strtolower($code);
    return ($code == "utf-8" ? $body : @iconv($code,"utf-8//IGNORE",$body));
    }
    }
    ?>
      

  7.   

    另外发现了一些问题,比如函数的参数的问题,,post($path,$referer="",$cookie="",$jump=1,$returnCode="",$data="")这样的不合理,有默认值的参数应该放在最后,代码中多次出现。基础知识有所欠缺
      

  8.   

    呵,我没看具体的代码,只看到public $toolType = 1;     //1:curl,2:socket,3:Stream这一句的时候,就知道,LZ应该是有优化的余地,至于怎么优化,就不讲了,还有就是觉得如果硬是要写成CLASS,那还是利于写成CLASS的样子,结构性上可以好好重新考虑一下,你的功能也许是很有用,但结构上我想不好好考虑,实用性上也是值得商榷的。