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

PHP脚本无法正确识别DASH API返回的completed状态问题排查

脚本无法识别任务"completed"状态的原因及修复方案

1. 状态值大小写不匹配

代码中判断完成状态用的是全大写的'COMPLETED',但你描述API返回的完成状态是小写的"completed",严格相等判断(===)会因为大小写不一致导致匹配失败。

对应代码问题点:

if ($status === 'COMPLETED') { // 此处为大写,与API返回的小写"completed"不匹配
    // ...
} elseif ($status === 'FAILED') { // 同理,API返回的是小写"failed"
    // ...
}

修复:将判断值改为与API一致的小写,或统一转换为相同大小写后再判断:

// 方案1:直接使用小写匹配
if ($status === 'completed') {
    // ...
} elseif ($status === 'failed') {
    // ...
}

// 方案2:统一转换为大写(或小写),兼容API返回格式波动
$status = strtoupper($response_data['status']);
if ($status === 'COMPLETED') {
    // ...
}

2. 状态检查次数不足、间隔太短

check_job_status函数仅尝试3次状态检查,每次间隔5秒,总时长仅15秒。如果DASH API的任务实际需要更长时间完成,脚本会直接抛出超时异常,根本没机会检测到后续的"completed"状态。

对应代码问题点:

for ($attempt = 0; $attempt < 3; $attempt++) { // 仅3次尝试
    sleep(5); // 每次间隔5秒
    // ...
}

修复:根据任务实际耗时调整尝试次数和间隔,比如增加到10次,每次间隔10秒:

for ($attempt = 0; $attempt < 10; $attempt++) {
    sleep(10);
    // ...
}

3. 任务完成但无URL时未正确返回

当任务状态为"completed"但未找到URL时,脚本仅打印错误日志,未返回结果,会继续进入下一次循环,直到3次尝试结束后抛出异常,导致无法正确识别任务已完成的状态。

对应代码问题点:

if ($status === 'COMPLETED') {
    log_message("Job $job_id completed successfully.", $GLOBALS['responseLogFile']);

    $result_key = "my-item-$batch_item_id";
    if (isset($response_data['result']['results'][$result_key]['result']['url'])) {
        // 仅找到URL时才返回
        return [$status, $response_data];
    } else {
        log_message("Error: No URL found in the job response.", $GLOBALS['responseLogFile']);
        // 此处无返回,会继续循环
    }
}

修复:即使未找到URL,只要状态为完成就返回结果,避免无效循环:

if ($status === 'COMPLETED') {
    log_message("Job $job_id completed successfully.", $GLOBALS['responseLogFile']);

    $result_key = "my-item-$batch_item_id";
    $url = null;
    if (isset($response_data['result']['results'][$result_key]['result']['url'])) {
        $url = $response_data['result']['results'][$result_key]['result']['url'];
        log_message("Job completed, URL: $url", $GLOBALS['responseLogFile']);
        file_put_contents('completed_jobs_urls.txt', "Job $job_id URL: $url\n", FILE_APPEND);
    } else {
        log_message("Error: No URL found in the job response.", $GLOBALS['responseLogFile']);
    }
    // 无论是否有URL,状态完成即返回
    return [$status, $response_data];
}

4. 未处理JSON解析失败的情况

如果API返回的响应不是有效的JSON,json_decode($response, true)会返回null,此时访问$response_data['status']会报错,导致脚本进入异常分支,无法正确判断状态。

修复:增加JSON解析的校验逻辑:

$response_data = json_decode($response, true);
if (json_last_error() !== JSON_ERROR_NONE) {
    log_message("Failed to parse API response: " . json_last_error_msg(), $GLOBALS['responseLogFile']);
    continue; // 跳过本次尝试,继续下一次检查
}
if (!isset($response_data['status'])) {
    log_message("API response does not contain 'status' field.", $GLOBALS['responseLogFile']);
    continue;
}
$status = $response_data['status'];

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.18 07:59:53