Home >Backend Development >PHP Tutorial >curl - 如何用PHP下载微信用户上传的图片

curl - 如何用PHP下载微信用户上传的图片

WBOY
WBOYOriginal
2016-06-06 20:33:571194browse

http://mmbiz.qpic.cn/mmbiz/K6icFiaUPUZPxwORf6Eg7p9bgqVLWFyvEupNTTxhCgN...

如上面的图片,用CURL获取就变成了
curl - 如何用PHP下载微信用户上传的图片
试着加USER-AGENT等信息还是不行。

回复内容:

http://mmbiz.qpic.cn/mmbiz/K6icFiaUPUZPxwORf6Eg7p9bgqVLWFyvEupNTTxhCgN...

如上面的图片,用CURL获取就变成了
curl - 如何用PHP下载微信用户上传的图片
试着加USER-AGENT等信息还是不行。

可能微信资源服务器用了盗链技术吧、类似java里filter过滤器的东西
根据请求的浏览器类型之类的信息判定是否盗链。
是微信就正常返回,其他请求就直接返回盗链图。

评论中得出答案

'User-Agent' => 'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.89 Safari/537.36',

$url = 'http://mmbiz.qpic.cn/mmbiz/K6icFiaUPUZPxwORf6Eg7p9bgqVLWFyvEupNTTxhCgN...';

$ch = curl_init();
$httpheader = array(
'Host' => 'mmbiz.qpic.cn',
'Connection' => 'keep-alive',
'Pragma' => 'no-cache',
'Cache-Control' => 'no-cache',
'Accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,/;q=0.8',
'User-Agent' => 'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.89 Safari/537.36',
'Accept-Encoding' => 'gzip, deflate, sdch',
'Accept-Language' => 'zh-CN,zh;q=0.8,en;q=0.6,zh-TW;q=0.4'
);
$options = array(
CURLOPT_HTTPHEADER => $httpheader,
CURLOPT_URL => $url,
CURLOPT_TIMEOUT => 5,
CURLOPT_FOLLOWLOCATION => 1,
CURLOPT_RETURNTRANSFER => true
);
curl_setopt_array( $ch , $options );
$result = curl_exec( $ch );
curl_close($ch);
file_put_contents( './i.jpg', $result );

这个应该跟 refer 有关吧,网页中引用的refer相对于微信是外域网站,但是refer为空的情况下是可以打开该链接的,楼主可以把图片地址复制到一个新tab里打开试试

adsfasdfasdcurl - 如何用PHP下载微信用户上传的图片

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn