1

我正在尝试设置 CUI,但是当我尝试发送请求时,出现代码错误

Uncaught CloudConvert\Exceptions\HttpClientException: tasks: The tasks field is required

我需要从我的计算机发送一个文件进行转换并接收 html 作为响应。我的错误在哪里?先感谢您!

$job = (new Job())
   ->addTask(
       (new Task('import/upload', 'import-my-file'))
       ->set('file', fopen($DocumentPath, 'r'))
     )
   ->addTask(
       (new Task('convert', 'convert-doc-to-html'))
         ->set('input_format', 'doc')
         ->set('output_format', 'html')
         ->set('engine', 'office')
         ->set('input', ["import-my-file"])
     )
   ->addTask(
       (new Task('export/url', 'export-my-file'))
         ->set('input', ["convert-doc-to-html"])
         ->set('inline', false)
         ->set('archive_multiple_files', false)
     ); 
     
$cloudconvert->jobs()->create($job);
$uploadTask = $job->getTasks()->whereName('import-my-file')[0];

$cloudconvert->tasks()->upload($uploadTask, fopen($DocumentPath, 'r'), 'myfile.doc');
4

2 回答 2

1

我遇到了同样的问题,结果发现是我打开的文件,我必须执行以下操作才能使其工作:

$file = rtrim(file_get_contents($file, FILE_TEXT)); $file = mb_convert_encoding($file, 'UTF-8', mb_detect_encoding($file, 'UTF-8, ISO-8859-1', true));

然后将 $file 放入 ->set('file', $file)

你可以看到,我在这里发现了什么:cloundconvert api v2 in php return The tasks field is required

于 2021-10-08T17:35:49.057 回答
0

我发现了这个问题,它帮助我解决了使用CloudConvert API v2将文件从 .docx 转换为 .pdf 的问题。这是一个适用于其他有麻烦的人的工作 php 示例。

public static function convertUsingCloudconvert($input_file_path, $output_file_path)
{
    $cloudconvert = new CloudConvert([
        'api_key' => 'YOUR_API_KEY_HERE',
        'sandbox' => false
    ]);


    //create process names
    $upload_process = 'your-upload-name';
    $convert_process = 'your-convert-name';
    $export_process = 'your-export-name';
    $upload_filename = 'your-file-upload-name.docx';

    $convert_job = (new Job())
        ->addTask(new Task('import/upload', $upload_process)) //task for file upload
        ->addTask( //task for conversion process
            (new Task('convert', $convert_process))
                ->set('input', $upload_process)
                ->set('input_format', 'docx')
                ->set('output_format', 'pdf')
        )->addTask( //task for file download
            (new Task('export/url', $export_process))
                ->set('input', $convert_process)
        );

    $cloudconvert->jobs()->create($convert_job); //create conversion process

    $uploadTask = $convert_job->getTasks()->whereName($upload_process)[0];
    $cloudconvert->tasks()->upload($uploadTask, fopen($input_file_path, 'r'), $upload_filename); //upload file to convert

    $cloudconvert->jobs()->wait($convert_job); //wait for process to finish

    $export_urls = $convert_job->getExportUrls(); //get generated files
    if (!empty($export_urls)) { 
        foreach ($export_urls as $file) {
            $source = $cloudconvert->getHttpTransport()->download($file->url)->detach();
            file_put_contents($output_file_path, $source); //copy source to output path
        }
    }
    else {
        //some error handling
    }

}
于 2021-11-11T09:44:55.877 回答