I am making a simple .webm to .mp4 conversion page called upload.html
I am using CloudConvert API and my backend is using PHP. I have a convert.php to do the operation.
The upload.html only has 2 buttons: “Choose File” & “Upload & Convert”.
When I click Choose File and select a 5seconds .webm file, then hit the “Upload & Convert” button, it says:
*Error: Unexpected token '<', "<br />
<b>"... is not valid JSON*
Opening up the developer tool > Network > Response, there’s message:
*<br />
<b>Fatal error</b>: Uncaught Error: Call to undefined method CloudConvertModelsJob::setTasks() in .../public_html/convert.php:37
Stack trace:
#0 {main}
thrown in <b>.../public_html/convert.php</b> on line <b>37</b><br />*
Here’s the convert.php Code:
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
header('Content-Type: application/json');
require 'vendor/autoload.php';
use CloudConvertCloudConvert;
use CloudConvertModelsJob;
use CloudConvertModelsTask;
try {
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
throw new Exception('Only POST allowed');
}
if (!isset($_FILES['file']) || $_FILES['file']['error'] !== UPLOAD_ERR_OK) {
throw new Exception('No valid video uploaded');
}
$apiKey = getenv('CLOUDCONVERT_API_KEY');
if (!$apiKey) {
throw new Exception('API key not set in environment');
}
$cloudConvert = new CloudConvert(['api_key' => $apiKey]);
// Save uploaded file temporarily
$tempFile = tempnam(sys_get_temp_dir(), 'upload_') . '.webm';
if (!move_uploaded_file($_FILES['file']['tmp_name'], $tempFile)) {
throw new Exception('Failed to save uploaded file');
}
// Create job with tasks
$job = new Job();
$job->setTasks([
(new Task('import/upload'))->setName('import-my-file'),
(new Task('convert'))
->setName('convert-my-file')
->setInput(['import-my-file'])
->setOutputFormat('mp4')
->setVideoCodec('h264'),
(new Task('export/url'))
->setName('export-my-file')
->setInput(['convert-my-file'])
->setInline(false)
->setArchiveMultipleFiles(false),
]);
$createdJob = $cloudConvert->jobs()->create($job);
// Upload file to import task
$uploadTask = $createdJob->getTasks()->whereName('import-my-file')[0];
$cloudConvert->tasks()->upload($uploadTask, fopen($tempFile, 'rb'));
// Wait for job to finish (conversion)
$cloudConvert->jobs()->wait($createdJob->getId());
// Get updated job info after completion
$finishedJob = $cloudConvert->jobs()->get($createdJob->getId());
// Get export task to fetch converted file URL
$exportTask = $finishedJob->getTasks()->whereName('export-my-file')[0];
$files = $exportTask->getResult()->files;
if (empty($files)) {
throw new Exception('No converted file returned');
}
// Cleanup temporary upload
unlink($tempFile);
// Return JSON with job ID and download URL
echo json_encode([
'job_id' => $finishedJob->getId(),
'file_url' => $files[0]->url
]);
} catch (Exception $e) {
if (isset($tempFile) && file_exists($tempFile)) {
unlink($tempFile);
}
http_response_code(400);
echo json_encode(['error' => $e->getMessage()]);
}
I don’t know what is the problem, and don’t know how to fix it. I just want a simple upload .webm file, convert and download in .mp4 file.
I checked the log.txt, it says nothing about the error.
I requested new API key from CloudConvert and replaced the old one with the new one. Problem not fixed.
I’ve make sure the API Key in .htaccess in my web hosting is readable, I’ve tested it by writing a simple php script to ECHO the API Key on the browser.
I have make sure the API Key requested from CloudConvert, the scopes are user.read, user.write, task.read, task.write.
I have checked my web hosting to make sure curl, SSL are enabled.
The CloudConvert PHP SDK is v3.4.2