我有一个 html 表单,允许用户上传文件,然后使用 IBM Watson 的文档转换 API 将文档文本转换为规范化文本,然后将其插入数据库。
经过测试,我多次收到以下错误:
{ "code" : 415, "error" : "不支持输入文档的媒体类型 [text/plain]。已尝试自动更正,但也不支持自动检测到的媒体类型 [text/plain]。支持媒体类型为: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);
?>
通常 $response 变量将包含转换后的文本,但即使我只上传 PDF,除了上面提到的 415 错误,我什么也没得到。
关于为什么它不起作用的任何想法?