【问题标题】:CloudConvert API v2 The tasks field is requiredCloudConvert API v2 任务字段为必填项
【发布时间】:2021-11-11 17:44:55
【问题描述】:

我正在尝试设置 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');

【问题讨论】:

    标签: php cloudconvert


    【解决方案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

    【讨论】:

    • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
    【解决方案2】:

    我发现了这个问题,它帮助我解决了使用 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-12-14
      • 2015-07-20
      • 2019-04-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-04
      • 2018-01-07
      相关资源
      最近更新 更多