ConvertAPI / convertapi-library-php

A PHP library for the ConvertAPI
https://www.convertapi.com
Other
44 stars 22 forks source link

Operation timed out #64

Open tomasr78 opened 2 days ago

tomasr78 commented 2 days ago

Some conversion files are causing 'Operation timed out' errors in certain PHP solutions.

Uncaught PHP Exception ConvertApi\Error\Client: "Operation timed out after 60001 milliseconds with 171 bytes received" at /var/www/weddingp/vendor/convertapi/convertapi-php/lib/ConvertApi/Client.php line 135 {"exception":"[object] (ConvertApi\\Error\\Client(code: 28): Operation timed out after 60001 milliseconds with 171 bytes received at /var/www/weddingp/vendor/convertapi/convertapi-php/lib/ConvertApi/Client.php:135)"}

Could be related to HTML string in Header property https://www.convertapi.com/a/api/html-to-pdf#scrollTo=Header

tomasr78 commented 2 days ago

This is the vanilla code we send to users with problems with timeouts; it works 100% well.

$file_path = 'C:\my_file.docx';
$secret = 'XXXXXXXXXXXXXXXX';

if (file_exists($file_path)) {
  $curl = curl_init();
  curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($curl, CURLOPT_BINARYTRANSFER, true);
  curl_setopt($curl, CURLOPT_POST, true);
  curl_setopt($curl, CURLOPT_HTTPHEADER, array('Accept: application/octet-stream'));
  curl_setopt($curl, CURLOPT_URL, "https://v2.convertapi.com/convert/docx/to/pdf?secret=".$secret);
  curl_setopt($curl, CURLOPT_POSTFIELDS, array('file' => new CurlFile($file_path)), 'storefile' => 'false');
  $result = curl_exec($curl);
  if (curl_getinfo($curl, CURLINFO_HTTP_CODE) == 200) {
      file_put_contents("result.pdf", $result);
  } else {
      print("Server returned error:\n".$result."\n");
  }
} else {
  print('File does not exist: '.$file_path."\n");
}
lizardslair commented 1 day ago

The vanilla solution worked for me after the SDK stopped working all of a sudden earlier today (linux cPanel server w/ PHP). Just one mention, the line with POSTFIELDS has a parse error, should be: curl_setopt($curl, CURLOPT_POSTFIELDS, array('file' => new CurlFile($file_path), 'storefile' => 'false'));

Background: Linux cPanel VPS - x86_64 GNU/Linux PHP 5.6.40 (yes I know it's old)

ConvertAPI SDK 1.6.1

Front-end usage:

$result = ConvertApi::convert('pdf', [
          'File' => $file_path,
          'Footer' => '<table style="width:95%;"><tr>'.
            '<td style="width: 30%; font-size: 6pt;">'.$reportType.'</td>'.
            '<td style="width: 45%; font-size: 6pt; text-align: center;">Page <span class="pageNumber"></span> of <span class="totalPages"></span> - '.$reportDate.'</td>'.
            '<td style="width: 25%; font-size: 6pt; text-align: right;">'.$reportUser.'</td>'.
            '</tr></table>',
          'ViewportWidth' => 1024,
      ], 'html');

It was working fine then all of a sudden around 5:03AM USA PT time Sept 19th, the process was just hanging. After some debug statements, I found it was timing out on the curl request inside ConvertApi\Client::upload() when executing the curl_exec($ch) line to POST the file to the /upload endpoint. I reduced the $uploadTimeout to 30 seconds for faster debugging: Operation timed out after 30002 milliseconds with 0 bytes received