0

I'm trying to call ConvertAPI.com/Word2Pdf, but with no success.

My sample code is:

$fileToConvert = 'test.docx';
$apiKey = *******;
$postdata = array('OutputFileName' => 'test.pdf', 'ApiKey' => $apiKey, 'File' => $fileToConvert);
$ch = curl_init("http://do.convertapi.com/Word2Pdf");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
$result = curl_exec($ch);
$headers = curl_getinfo($ch);
curl_close($ch);
var_dump($result);

I'm getting as a result something like: "HTTP/1.1 100 Continue HTTP/1.1 400 The 'File' parameter cannot be null. Please set value. Cache-Control: no-cache, no-store Pragma: no-cache Content-Type: text/html..."

What I'm doing wrong here?

Zoran Kalinić
  • 317
  • 2
  • 10
  • 23
  • Does the test.docx file exist? Maybe try using an absolute path for that file? – WillardSolutions May 13 '14 at 17:59
  • yes. it exists, and it is located in the same folder as this php file is... – Zoran Kalinić May 13 '14 at 18:03
  • i've tried with absolute path, but got the same result... – Zoran Kalinić May 13 '14 at 18:09
  • Since my host upgraded from php 5.4 to php 5.6 I get the same errors. Did you find any solution? (not the unirest way). I have send mulitple emails to their support with no response at all.... – COBIZ webdevelopment Apr 02 '16 at 11:34
  • @COBIZwebdevelopment just as I wrote in the comment to the accepted answer, I haven't used unirest, and the solution was to add @ to the file name: $postdata = array('OutputFileName' => 'test.pdf', 'ApiKey' => $apiKey, 'File' => "@" . $fileToConvert); – Zoran Kalinić Apr 03 '16 at 21:10
  • My error was due to the php 5.6 upgrade. This solved it: http://stackoverflow.com/questions/25934128/curl-file-uploads-not-working-anymore-after-upgrade-from-php-5-5-to-5-6 – COBIZ webdevelopment Apr 05 '16 at 12:08

1 Answers1

0

Try this using http://unirest.io/php.html library

$response = Unirest::post(
  "http://do.convertapi.com/Word2Pdf?ApiKey=<Your api key>",
  array(
    "File" => "@/tmp/file.path",
    "OutputFormat" => "pdf",
  )
);
Tomas
  • 17,551
  • 43
  • 152
  • 257