2012-02-26 228 views
0

這個API讓我頭疼..真的無法弄清楚什麼是錯在這裏..API谷歌Closure編譯器

它只是通過回這個錯誤:

414. That’s an error. 

The requested URL /compile... is too large to pr 

文件$filename存在時回聲線是轉義

代碼

$Compile = new Net_minify_JS(); 
$Compile->script = file_get_contents($filename); 
//echo $Compile->script; 
$Compile->content_length = strlen($Compile->script); 
echo '<pre>'; 
echo $Compile->get(); 
echo '</pre>'; 

class Net_minify_JS extends Net_socket { 
    private $content = null; 

    public $script = ''; 

    function get(){ 
     $this->url = 'closure-compiler.appspot.com'; 
     $this->path = '/compile?output_info=compiled_code&output_format=text&compilation_level=SIMPLE_OPTIMIZATIONS&js_code='.urlencode($this->script); 
     $this->method = 'POST'; 

     $this->connect(); 
     $this->content = $this->get_content(); 

     $string = str_replace("\r\n", "\n", $this->content); 

     return $string; 
    } 
} 

class Net_socket { 
    public $url = null; 
    public $path = null; 

    public $method = 'GET'; 
    public $content_type = 'application/x-www-form-urlencoded'; 
    public $content_length = 0; 

    public $port = 80; 
    public $timeout = 20; 

    private $fp = null; 
    private $response = null; 

    function connect(){ 
     $this->response = null; 

     $this->method = $this->prepare_method(); 

     if($this->fp = fsockopen($this->url, $this->port, $errno, $errstr, $this->timeout)){ 
      $write = "$this->method $this->path HTTP/1.1\r\n"; 
      $write .= "Host: $this->url\r\n"; 
      $write .= "Content-Type: $this->content_type\r\n"; 
      $write .= $this->content_length ? "Content-Length: $this->content_length\r\n":''; 
      $write .= "Connection: Close\r\n\r\n"; 

      fwrite($this->fp, $write); 

      while($line = fgets($this->fp)){ 
       if($line !== false) $this->response .= $line; 
      } 

      fclose($this->fp); 
     } 
     else{ 
      //echo "$errstr ($errno)<br>\n"; 
     } 
    } 

    function prepare_method(){ 
     return strtoupper($this->method); 
    } 

    function get_content(){ 
     $this->response = str_replace("\r\n", "\n", $this->response); 
     $expl = explode("\n\n", $this->response); 

     return $expl[1]; 
    } 
} 

回答

0

聽起來你違反大小限制。請參閱https://developers.google.com/closure/compiler/docs/api-ref#errors瞭解允許多少數據。

而且,爲了好的措施,在POST中傳遞js_code而不是GET。

編輯:您正在使用POST方法,但仍然將查詢字符串中的參數傳遞給請求主體。這意味着它們都在服務器(和任何代理)的訪問日誌中可見。即使它們不是祕密,查詢字符串仍然受到一定限制:http://en.wikipedia.org/wiki/Query_string#Compatibility_issues

+0

通過在POST中傳遞js_code,你是什麼意思?所有的變量都作爲POST方法傳遞 – clarkk 2012-02-26 09:32:47