2012-02-26 159 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