2017-06-15 87 views
0

我有一個html表單,允許用戶上傳文件,然後使用IBM Watson的文檔轉換API將文檔文本轉換爲標準文本,然後插入到數據庫中。IBM Watson文檔轉換響應415錯誤,即使我攝入PDF?

在測試中,我已收到以下錯誤多次:

{「代碼」:415,「錯誤」:輸入文檔的「媒體類型[文本/無格式]不支持自動校正被支持的媒體類型有:application/msword,application/vnd.openxmlformats-officedocument.wordprocessingml.document,application/pdf,text/html,application/xhtml + xml「。 }

這裏是我的形式(testform.html):

<form action="testform.php" method="post" enctype="multipart/formdata"> 
    <input type="file" name="newdoc" id="newdoc"> Upload New Doc: 
    </input> 
    <button type="submit" name="submit">Submit</button> 
    </form> 

這裏是我的PHP腳本(testform.php):

<?php 
    $filename = $_FILES['newdoc']['name']; 
    $filetype = $_FILES['newdoc']['type']; 
    $filesize = $_FILES['newdoc']['size']; 
    $filetmp = $_FILES['newdoc']['tmp_name']; 

    // Watson Document Conversion 
    $dcuser = 'arbitrary_user'; 
    $dcpass = 'arbitrary_pwd'; 
    $userpwd = $dcuser . ":" . $dcpass; 

    // Initialize cURL 
    $documentconversion = curl_init(); 

    // Set POST 
    curl_setopt($documentconversion, CURLOPT_POST, true); 

    // Set DC API URL 
    curl_setopt($documentconversion, CURLOPT_URL, 
    'https://gateway.watsonplatform.net/document- 
    conversion/api/v1/convert_document?version=2015-12-15'); 

    // Set Username:Password 
    curl_setopt($documentconversion, CURLOPT_USERPWD, $userpwd); 

    // Set conversion units, file, and file type 
    curl_setopt($documentconversion, CURLOPT_POSTFIELDS, array(
    'config' => "{\"conversion_target\":\"normalized_text\"}", 
    'file' => '@' . realpath($filetmp) . ';type=' . $filetype 
    )); 

    // Set return value 
    curl_setopt($documentconversion, CURLOPT_RETURNTRANSFER, true); 

    // Execute and get response 
    $response = curl_exec($documentconversion); 

    // Close cURL 
    curl_close($documentconversion); 
    ?> 

通常情況下,$響應變量將包含轉換文字,但我一直沒有得到上述415錯誤,即使我只上傳PDF文件。

任何想法爲什麼它不工作?

回答

0

從錯誤,似乎你的PHP腳本傳遞text/plain文件類型,這是不支持的服務。相反,請嘗試傳入application/pdf作爲文件類型。

您也可以嘗試運行一個簡單的curl命令請求:

curl -X POST -u "YOUR_USERNAME":"YOUR_PASSWORD" -F config="{\"conversion_target\":\"normalized_text\"}" -F "[email protected];type=application/pdf" " https://gateway.watsonplatform.net/document-conversion/api/v1/convert_document?version=2015-12-15 "

正如你可以在API reference發現,所支持的類型有: text/htmltext/xhtml+xmlapplication/pdfapplication/mswordapplication/vnd.openxmlformats-officedocument.wordprocessingml.document

+0

我剛剛嘗試過,並且仍然收到415錯誤= \ –

+0

您可以使用curl命令嘗試它:curl -X POST -u「{username}」:「{password}」-F config =「{\」conversion_target \「:\」normalized_text \「}」-F「[email protected]; type = application/pdf」「https://gateway.watsonplatform.net/document-conversion/api/v1/convert_document?version = 2015-12-15「 –

+0

獲得401錯誤:{」code「:401,」error「:」Not Authorized「,」description「:」2017-06-15T16:17:17-04: 00,訪問https://gateway.watsonplatform.net/document-conversion/api/v1/convert_document?version=2015-12-15,Tran-Id時發生錯誤ERCDPLTFRM-INVLDCHR:網關-dp02-1290491232 - 「} –