如何在php中显示curl中的二进制数据 [英] How to display binary data from curl in php
问题描述
我写简单的php代理,我无法显示png文件,输出是
I'm writing simple php proxy and I have trouble displaying png file, the output is
,应为:
图片已打开在记事本++。我的php curl代码看起来像这样:
The images are opened in Notepad++. My php curl code look like this:
$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输出是相同的,不显示。如何显示图片?
I try with and without CURLOPT_BINARYTRANSFER the output is the same and the image is not displaying. How can I display the image?
编辑:当我将数据保存到文件并使用位置标题重定向时,显示图片正确:
EDIT: when I'm saving the data to the file and redirect using Location header the image is displayed correctly:
$file = fopen('proxy_tmp~', 'w');
fwrite($file, $content);
fclose($file);
header('Location: ' . DIR . 'proxy_tmp~');
EDIT 2 :我有gzip压缩,有同样的问题,当我在记事本++中打开这两个文件是一个是DOS / Windows ANSI(原始),另一个是DOS / Windows UTF-8(由脚本打开的文件)。当我在记事本中打开文件并将编码更改为ANSI并保存文件时,一切正常。
EDIT 2: I had gzip compression, bu when I disabled it I have the same issue, when I open both files in Notepad++ one is DOS/Windows ANSI (original) and the other is DOS/Windows UTF-8 (the file opened by a script). When I open a file in Notepad and change encoding to ANSI and save the file, everything is ok.
推荐答案
Content-Length
头解决了二进制文件代理的问题。
In my case adding Content-Length
header fixed problems with binary files proxy.
这篇关于如何在php中显示curl中的二进制数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!