感谢您查看我的问题。我创建了一个通过 DASH API 发送 API 请求的 PHP 脚本。脚本发送请求后,它应该在 DASH 上启动作业。但是,该脚本无法正确检索“已完成”状态,并最终在循环中运行,不断请求作业状态,直到 cron 作业被迫结束。可能的作业状态有“进行中”、“已完成”和“失败”。
以下是我的脚本的相关部分:
function create_batch_job($access_token, $asset_id, $batch_item_id)
{
$batch_job_url = 'https://api-v2.dash.app/embeddable-link-batch-jobs';
$headers = [
"Authorization: Bearer $access_token",
'Content-Type: application/json'
];
$body = json_encode([
"items" => [
[
"batchItemId" => "my-item-$batch_item_id",
"assetId" => $asset_id,
"assetFileId" => null,
"presetParameters" => []
]
],
"transformationDescription" => [
"type" => "CUSTOM",
"candidateTransformations" => [
[
"criteria" => [
[
"type" => "MATCHES_MEDIA_TYPES",
"mediaTypes" => [
["type" => "image", "subType" => "png"]
]
]
],
"transformation" => []
]
]
]
]);
list($status, $response) = http_request('POST', $batch_job_url, $headers, $body);
log_message("Request URL: $batch_job_url", $GLOBALS['responseLogFile']);
log_message("Request headers: " . json_encode($headers), $GLOBALS['responseLogFile']);
log_message("Request body: $body", $GLOBALS['responseLogFile']);
log_message("Response status: $status", $GLOBALS['responseLogFile']);
log_message("Response body: $response", $GLOBALS['responseLogFile']);
if ($status == 200) {
log_api_response($response);
$response_data = json_decode($response, true);
return $response_data['id'];
} else {
log_api_response($response);
throw new Exception('Batch job creation failed');
}
}
function check_job_status($access_token, $job_id, $batch_item_id)
{
$job_status_url = "https://api-v2.dash.app/embeddable-link-batch-jobs/$job_id";
$headers = [
"Authorization: Bearer $access_token",
'Content-Type: application/json'
];
for ($attempt = 0; $attempt < 3; $attempt++) {
sleep(5);
log_message("Checking job status for URL: $job_status_url (Attempt " . ($attempt + 1) . "/3)", $GLOBALS['responseLogFile']);
list($status_code, $response) = http_request('GET', $job_status_url, $headers);
log_message("Check job status response: $status_code $response", $GLOBALS['responseLogFile']);
if ($status_code == 200) {
$response_data = json_decode($response, true);
$status = $response_data['status'];
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 = $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);
return [$status, $response_data];
} else {
log_message("Error: No URL found in the job response.", $GLOBALS['responseLogFile']);
}
} elseif ($status === 'FAILED') {
log_message("Job $job_id failed.", $GLOBALS['responseLogFile']);
return [$status, $response_data];
} else {
log_message("Job $job_id in progress... Status: $status", $GLOBALS['responseLogFile']);
}
} else {
throw new Exception('Job status check failed');
}
}
throw new Exception("Job $job_id did not complete after 3 attempts");
}
function process_assets()
{
global $csvFile, $completedJobsFile, $jobIdsFile;
log_api_response("Cron job started successfully.");
$access_token = authenticate();
$csv = array_map('str_getcsv', file($csvFile));
$header = array_shift($csv);
$outfile = fopen($completedJobsFile, 'w');
fputcsv($outfile, ['jobId', 'status', 'url']);
$batch_item_id = 1;
foreach ($csv as $idx => $row) {
$asset_id = $row[0];
log_message("Processing asset " . ($idx + 1) . ": $asset_id", $GLOBALS['responseLogFile']);
try {
$job_id = create_batch_job($access_token, $asset_id, $batch_item_id);
if ($job_id) {
file_put_contents($jobIdsFile, "$job_id\n", FILE_APPEND);
list($status, $response_data) = check_job_status($access_token, $job_id, $batch_item_id);
if ($status === 'COMPLETED') {
$result_key = "my-item-$batch_item_id";
if (isset($response_data['result']['results'][$result_key]['result']['url'])) {
$url = $response_data['result']['results'][$result_key]['result']['url'];
fputcsv($outfile, ['jobId' => $job_id, 'status' => $status, 'url' => $url]);
log_message("Job $job_id completed and saved to file.", $GLOBALS['responseLogFile']);
}
}
$batch_item_id++;
}
} catch (Exception $e) {
log_message("Error processing asset ID $asset_id: " . $e->getMessage(), $GLOBALS['responseLogFile']);
}
}
fclose($outfile);
}
process_assets();
我希望脚本一次运行一个资产 id,提取我需要的链接并将链接 + id 写入 csv 文件。但脚本无法在 api 请求中找到“已完成”状态,因此以下所有过程也不会发生。为什么脚本无法从 api 作业中找到“已完成”语句?
改变
if ($status === 'COMPLETED') {
到
if ($status === 'completed') {
遵守正确完成的状态消息。确保调试代码并查看这里的情况是否已经按照您的预期发生,因此令牌正确,状态已检索,请求成功,唯一不起作用的确实是响应的正确解析。