使用“后台-附件管理-远程下载”功能,下载某些网站的图片时,会出现“文件头部信息获取失败,请更换其他网址”的错误信息
这是由于 get_headers 获取客户端HTTP请求头信息,不同环境返回的信息存在键值大小写差异,而当前程序均采用大写的方式予以读取,导致程序出错。
参考:
https://github.com/qiniu/php-sdk/issues/257
https://blog.csdn.net/I_am_hardy/article/details/123947516
比如:
get_headers('https://x0.ifengimg.com/ucms/2024_31/447238BB17CB45693F96B1BDDEDDB25895F245BF_size550_w1118_h620.png',1)
拿到的信息是:
array (size=17)
0 => string 'HTTP/1.1 200 OK' (length=15)
'Last-Modified' => string 'Mon, 29 Jul 2024 02:22:40 GMT' (length=29)
'Etag' => string '"d6745f868dc6a28d149e4a73278c41a4"' (length=34)
'access-control-allow-origin' => string '*' (length=1)
'content-type' => string 'image/png' (length=9)
'date' => string 'Mon, 29 Jul 2024 02:22:40 GMT' (length=29)
'server-info' => string 'tencent-c' (length=9)
'x-osc-hit' => string 'tencent' (length=7)
'x-osc-meta-visible' => string 'visible' (length=7)
'Server' => string 'Lego Server' (length=11)
'Cache-Control' => string 'max-age=7776000' (length=15)
'Age' => string '4207' (length=4)
'Content-Length' => string '558402' (length=6)
'Accept-Ranges' => string 'bytes' (length=5)
'X-NWS-LOG-UUID' => string '5720120506899452438' (length=19)
'Connection' => string 'close' (length=5)
'X-Cache-Lookup' => string 'Cache Hit' (length=9)
而:
get_headers('https://k.sinaimg.cn/n/collect/crawl/575/w550h825/20240722/ec54-e639c61d4203dd0758930274ab486587.jpg/w700d1q75cms.jpg',1)
拿到的信息是:
array (size=27)
0 => string 'HTTP/1.1 200 OK' (length=15)
'Server' => string 'Tengine' (length=7)
'Content-Type' => string 'image/jpeg' (length=10)
'Content-Length' => string '89234' (length=5)
'Connection' => string 'close' (length=5)
'Date' => string 'Mon, 29 Jul 2024 06:35:39 GMT' (length=29)
'Cache-Control' => string 'max-age=2592000' (length=15)
'Content-Security-Policy' => string 'block-all-mixed-content' (length=23)
'Last-Modified' => string 'Wed, 24 Jul 2024 02:56:09 GMT' (length=29)
'X-Xss-Protection' => string '1; mode=block' (length=13)
'Accept-Ranges' => string 'bytes' (length=5)
'Access-Control-Allow-Origin' => string '*' (length=1)
'Access-Control-Allow-Credentials' => string 'true' (length=4)
'Access-Control-Allow-Methods' => string 'GET, PUT, POST, DELETE, OPTIONS, HEAD' (length=37)
'Access-Control-Allow-Headers' => string 'Origin, Content-Type, Accept, Content-Length' (length=44)
'Access-Control-Max-Age' => string '1728000' (length=7)
'Edge-Copy-Time' => string '1721789769704' (length=13)
'Via' => string 'http/1.1 ctc.guangzhou.union.52 (ApacheTrafficServer/6.2.1 [cHs f ]), cache34.l2cn2180[45,45,200-0,M], cache27.l2cn2180[46,0], vcache7.cn6013[123,123,200-0,M], vcache24.cn6013[124,0]' (length=182)
'X-Via-CDN' => string 'f=aliyun,s=vcache24.cn6013,c=101.80.204.121;f=sinaedge,s=ctc.guangzhou.union.50.nb.sinaedge.com,c=111.62.140.54;f=Edge,s=ctc.guangzhou.union.52,c=10.31.54.50' (length=157)
'X-Via-Edge' => string '1722234939809368c3e6f32361f0a23e4e082' (length=37)
'Age' => string '445170' (length=6)
'Ali-Swift-Global-Savetime' => string '1722234939' (length=10)
'X-Cache' => string 'MISS TCP_MISS dirn:-2:-2' (length=24)
'X-Swift-SaveTime' => string 'Mon, 29 Jul 2024 06:35:39 GMT' (length=29)
'X-Swift-CacheTime' => string '2592000' (length=7)
'Timing-Allow-Origin' => string '*' (length=1)
'EagleId' => string '3daa512c17222349397244519e' (length=26)
BUG处理:
[app\admin\controller\routine\Attachment.php]
代码:
if(version_compare(PHP_VERSION,'8.0.0','<')) {
$head = get_headers($url,1);
} else {
$head = get_headers($url,true);
}
之后添加以下代码:
$headTmp = [];
foreach ($head as $key => $value) {
if($key != '0') $key = ucwords($key,'-');
$headTmp[$key] = $value;
}
$head = $headTmp;
问题解决!
发表评论 取消回复