简体   繁体   English

如何覆盖curl_exec设置标头

[英]how to override curl_exec setting headers


I'm building a combine/minify php script for js, using a number of online minify scripts, and I'm stuck at Google's Closure minify script. 我正在使用许多在线minify脚本为js构建一个Combine / minify php脚本,而我被困在Google的Closure minify脚本中。 This particular script requires that you do a POST with the content that needs to be minified, and you get the minified code in the response. 此特定脚本要求您对需要缩小的内容进行POST,并在响应中获取缩小的代码。 My script needs to work like this: 我的脚本需要像这样工作:
1. user adds the file or files to be minified and/or combined 1.用户添加要缩小和/或合并的文件
2. the user clicks 'Start' 2.用户单击“开始”
3. the script runs in the background, and a download is started; 3.该脚本在后台运行,并开始下载; the file downloaded is either: 下载的文件是:
3a. 3a。 one big js file containing the combined minified content of the files provided OR 一个大js文件,其中包含所提供文件的合并最小内容,或者
3b. 3b。 one archive containing the minified versions of the provided files. 一个包含所提供文件的缩小版本的存档。

The point of 3a is that no file is created on the server, but instead I change the header and simply output the minified content. 3a的要点是,服务器上没有创建任何文件,但是我更改了标头并仅输出缩小的内容。 And here's where I have problems. 这就是我遇到的问题。 The code in my minify method looks like this: 我的minify方法中的代码如下所示:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://closure-compiler.appspot.com/compile');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'output_info=compiled_code&output_format=text&compilation_level=SIMPLE_OPTIMIZATIONS&js_code=' . urlencode($str));
$result = curl_exec($ch);

return $result;

and later in the code, I have 然后在代码中,我有

$js = mini($js);
header('Content-Description: File Transfer');
header('Content-Type: application/javascript');
header('Content-Disposition: attachment; filename='.$ourFileName);
header('Content-Transfer-Encoding: text');
echo $js;

if it's not clear, the $str variable from the first code segment is the actual combined content of all the files, and mini is the minify method. 如果不清楚,则第一个代码段中的$ str变量是所有文件的实际组合内容, mini是minify方法。 The problem is that, if I run this, I see the content in the browser window (truncated), and if I inspect the page, I see an error: 问题是,如果运行此命令,则会在浏览器窗口中看到内容(被截断),并且如果检查页面,则会看到错误:

Cannot modify header information - headers already sent by (output started at E:\work\AdoTube\workspace\php\script.php:47)

(line 47 is the curl_exec call). (第47行是curl_exec调用)。 If I bypass the minify method, I get a nice 'download file', and the file looks ok. 如果我绕过minify方法,则会得到一个不错的“下载文件”,并且该文件看起来还可以。 So, my guess is the curl_exec call sets some headers on my script which prevent me from changing the headers later in the code. 因此,我的猜测是curl_exec调用在脚本中设置了一些标头,这阻止了我稍后在代码中更改标头。
How do I fix it? 我如何解决它? I know one idea would be to write the result to a temp file, and just download that, but i don't want to do that, unless it's the only option.. Thank you. 我知道一个想法是将结果写入临时文件,然后下载该文件,但是我不想这样做,除非它是唯一的选择。。谢谢。

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM