【发布时间】:2013-07-08 07:17:15
【问题描述】:
我正在编写简单的 php 代理,但无法显示 png 文件,输出为
它应该是:
图像在 Notepad++ 中打开。我的 php curl 代码如下所示:
$ua = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, $ua);
curl_setopt($ch, CURLOPT_HEADER_OUT, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
$content = curl_exec($ch);
$info = curl_getinfo($ch);
header('Content-Type:' . $info['content_type']);
echo $content
我尝试使用和不使用 CURLOPT_BINARYTRANSFER,输出是相同的,并且图像不显示。如何显示图像?
编辑:当我将数据保存到文件并使用 Location 标头重定向时,图像正确显示:
$file = fopen('proxy_tmp~', 'w');
fwrite($file, $content);
fclose($file);
header('Location: ' . DIR . 'proxy_tmp~');
编辑 2:我有 gzip 压缩,但是当我禁用它时,我遇到了同样的问题,当我在 Notepad++ 中打开两个文件时,一个是 DOS/Windows ANSI(原始),另一个是 DOS /Windows UTF-8(由脚本打开的文件)。当我在记事本中打开文件并将编码更改为 ANSI 并保存文件时,一切正常。
编辑 3:我想我在 GNU/Linux 上做了同样的事情,但没有 CURLOPT_BINARYTRANSFER 选项,它工作正常,这是我的项目 https://github.com/jcubic/yapp。我还在使用 Wamp 的 Windows 10 上对其进行了测试,并且运行良好。
【问题讨论】:
标签: php image curl binary-data