2015-06-18 119 views
0

我用下面的doc文件轉換爲PDF在PHP中:PHP代碼不工作

function CallToApi($fileToConvert, $pathToSaveOutputFile, $apiKey, &$message,$unique_filename) 
{ 
    try 
    { 


     $fileName = $unique_filename.".pdf"; 

     $postdata = array('OutputFileName' => $fileName, '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); 

     $header=ParseHeader(substr($result,0,$headers["header_size"])); 
     $body=substr($result, $headers["header_size"]); 

     curl_close($ch); 
     if (0 < $headers['http_code'] && $headers['http_code'] < 400) 
     { 
      // Check for Result = true 

      if (in_array('Result',array_keys($header)) ? !$header['Result']=="True" : true) 
      { 
       $message = "Something went wrong with request, did not reach ConvertApi service.<br />"; 
       return false; 
      } 
      // Check content type 
      if ($headers['content_type']<>"application/pdf") 
      { 
       $message = "Exception Message : returned content is not PDF file.<br />"; 
       return false; 
      } 
      $fp = fopen($pathToSaveOutputFile.$fileName, "wbx"); 

      fwrite($fp, $body); 

      $message = "The conversion was successful! The word file $fileToConvert converted to PDF and saved at $pathToSaveOutputFile$fileName"; 
      return true; 
     } 
     else 
     { 
     $message = "Exception Message : ".$result .".<br />Status Code :".$headers['http_code'].".<br />"; 
     return false; 
     } 
    } 
    catch (Exception $e) 
    { 
     $message = "Exception Message :".$e.Message."</br>"; 
     return false; 
    } 
} 

我現在想用同樣的PPT轉換爲PDF。對於我改變

$ch = curl_init("http://do.convertapi.com/word2pdf"); 

$ch = curl_init("http://do.convertapi.com/PowerPoint2Pdf"); 

,但我不知道爲什麼它不是轉換給定的輸入。有什麼我可能會失蹤?

+0

你有什麼錯誤嗎?請閱讀響應標題,它們將包含有關異常的其他數據。 – Tomas

回答

0

這是由於PHP 5.6。

您需要將此行添加到您的代碼以及

curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false); 

見:Backward incompatible changes(底部)。