You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为PHP图片CDN检测脚本添加多分支IF/ELSE IF判断?

问题描述

我现在用一段能正常运行的PHP代码检测在线图片CDN服务器是否返回缓存图片,按优先级显示:先显示Weserv的图片;如果该链接返回404、403等错误,就显示Aratech的图片;要是Aratech也失败,最后显示原始图片地址。

我想新增2个图片CDN链接,让脚本检测并显示首个返回HTTP 200状态码的图片,但我对PHP不熟悉,误以为IF/ELSE IF/ELSE只能支持3个分支。想问问怎么添加更多IF/ELSE IF语句?如果这种方式不可行,有没有其他实现方法?

现有代码

<?php
// $thumbnail['url'] = 原始图片地址
$weserv = rtrim(preg_replace ('/https:\/\/|http:\/\//','https://images.weserv.nl/?w=100&output=webp&url=',$thumbnail['url'],1),'/');
$header_weserv = @get_headers($weserv);

$aratech = rtrim(preg_replace ('/https:\/\/|http:\/\//','https://imagex.aratech.co/?w=100&q=75&il&output=webp&url=',$thumbnail['url'],1),'/');
$header_aratech = get_headers($aratech);

if ($header_weserv && strpos( $header_weserv[0], "200" ))
{
// 第一个选项
  echo $weserv; //若Weserv链接返回200状态码,则显示该链接
} 
elseif  ($header_aratech &&  strpos( $header_aratech[0], "200" ))
{
//第二个选项
  echo $aratech; //若Weserv失败且Aratech链接返回200状态码,则显示该链接
}
//需要在下方的else前添加第三个和第四个选项
else
{
  echo $thumbnail['url']; //若Weserv和Aratech均失败,则显示原始链接
}
?>
解决方案

方法一:直接扩展IF/ELSE IF分支

IF/ELSE IF并没有分支数量限制,你可以继续添加elseif块来处理新增的CDN链接。比如新增两个CDN(假设分别是cdn3和cdn4),用链式写法更整洁:

<?php
// $thumbnail['url'] = 原始图片地址
// 生成各CDN链接
$weserv = rtrim(preg_replace ('/https:\/\/|http:\/\//','https://images.weserv.nl/?w=100&output=webp&url=',$thumbnail['url'],1),'/');
$aratech = rtrim(preg_replace ('/https:\/\/|http:\/\//','https://imagex.aratech.co/?w=100&q=75&il&output=webp&url=',$thumbnail['url'],1),'/');
$cdn3 = rtrim(preg_replace ('/https:\/\/|http:\/\//','https://cdn3.example.com/?w=100&format=webp&url=',$thumbnail['url'],1),'/');
$cdn4 = rtrim(preg_replace ('/https:\/\/|http:\/\//','https://cdn4.example.com/?width=100&output=webp&src=',$thumbnail['url'],1),'/');

// 逐个检测状态码
$header_weserv = @get_headers($weserv);
if ($header_weserv && strpos($header_weserv[0], "200")) {
    echo $weserv;
} elseif (($header_aratech = get_headers($aratech)) && strpos($header_aratech[0], "200")) {
    echo $aratech;
} elseif (($header_cdn3 = get_headers($cdn3)) && strpos($header_cdn3[0], "200")) {
    echo $cdn3;
} elseif (($header_cdn4 = get_headers($cdn4)) && strpos($header_cdn4[0], "200")) {
    echo $cdn4;
} else {
    echo $thumbnail['url'];
}
?>

方法二:用数组管理CDN(更易维护)

如果后续还要新增更多CDN,用数组循环的方式更高效,不用每次修改条件判断。步骤如下:

  1. 按优先级定义CDN模板数组
  2. 遍历数组生成每个CDN的完整链接
  3. 检测每个链接的状态码,找到第一个返回200的就输出
  4. 所有CDN都失败时,输出原始地址

代码示例:

<?php
// $thumbnail['url'] = 原始图片地址
// 按优先级定义CDN模板,{url}会替换为去掉http/https前缀的原始图片地址
$cdnTemplates = [
    'https://images.weserv.nl/?w=100&output=webp&url={url}',
    'https://imagex.aratech.co/?w=100&q=75&il&output=webp&url={url}',
    'https://cdn3.example.com/?w=100&format=webp&url={url}', // 新增的CDN3
    'https://cdn4.example.com/?width=100&output=webp&src={url}'  // 新增的CDN4
];

// 处理原始图片地址,去掉http/https前缀并清理末尾斜杠
$cleanOriginalUrl = preg_replace('/https?:\/\//', '', $thumbnail['url']);
$cleanOriginalUrl = rtrim($cleanOriginalUrl, '/');

$finalImageUrl = $thumbnail['url']; // 默认用原始地址

foreach ($cdnTemplates as $template) {
    $cdnUrl = str_replace('{url}', $cleanOriginalUrl, $template);
    $headers = @get_headers($cdnUrl);
    if ($headers && strpos($headers[0], "200")) {
        $finalImageUrl = $cdnUrl;
        break; // 找到第一个可用的CDN,停止循环
    }
}

echo $finalImageUrl;
?>

这种方式后续新增CDN,只需在$cdnTemplates数组里加一行模板即可,代码结构更清晰,维护成本更低。

内容的提问来源于stack exchange,提问作者Johnny Bravo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.27 18:24:57