简体   繁体   English

使用 PHP 创建带有 api 的自然语言分类器时出错,返回数据太小错误但在 Postman 中有效

[英]Error creating natural language classifier with api using PHP, returns data too small error but works in Postman

The cURL request works in Postman with the following: cURL 请求在 Postman 中工作,具有以下内容:

curl -i -u "apikey:12345" \
-F training_data=@rtcu.csv \
-F training_metadata="{\"language\":\"en\",\"name\":\"RTCU\"}" \
"https://gateway.watsonplatform.net/natural-language-classifier/api/v1/classifiers/v1/classifiers"

Postman's generated cURL code for PHP returns { "code": 400, "error": "Data too small", "description": "The number of training entries received = 0, which is smaller than the required minimum of 5" } Postman 为 PHP 生成的 cURL 代码返回 { "code": 400, "error": "Data too small", "description": "The number of training entries received = 0, which is less than the required minimum of 5" }

<?php

 $curl = curl_init();

 curl_setopt_array($curl, array(
 CURLOPT_URL => "https://gateway.watsonplatform.net/natural-language-classifier/api/v1/classifiers",
 CURLOPT_RETURNTRANSFER => true,
 CURLOPT_ENCODING => "",
 CURLOPT_MAXREDIRS => 10,
 CURLOPT_TIMEOUT => 30,
 CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
 CURLOPT_CUSTOMREQUEST => "POST",
 CURLOPT_POSTFIELDS => "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"training_data\"; filename=\"rtcu.csv\"\r\nContent-Type: text/csv\r\n\r\n\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"training_metadata\"\r\n\r\n{\"language\":\"en\",\"name\":\"RTCU\"}\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--",
 CURLOPT_HTTPHEADER => array(
"Authorization: Basic 12345",
"cache-control: no-cache",
"content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW"
),
));

 $response = curl_exec($curl);
 $err = curl_error($curl);

 curl_close($curl);

 if ($err) {
 echo "cURL Error #:" . $err;
} else {
 echo $response;
}

I've tried adding the "@" in front of the filename like suggested in other posts with no success.我尝试在文件名前添加“@”,就像其他帖子中建议的那样,但没有成功。 I haven't had this problem with other IBM Watson services and their cURL calls.我对其他 IBM Watson 服务及其 cURL 调用没有这个问题。 What could be the issue?可能是什么问题?

Using CURLFile worked for me使用 CURLFile 对我有用

$uploadFilePath = 'rtcu.csv';

 $name = "RTCU";
 $lang = "en";

 $uploadFileMimeType = "text/csv";
 $uploadFilePostKey = 'training_data';
 $metaPostKey = "training_metadata";

 $uploadFile = new CURLFile(
 $uploadFilePath,
 $uploadFileMimeType,
 $uploadFilePostKey
);

 $curlHandler = curl_init();

 curl_setopt_array( $curlHandler, [
 CURLOPT_URL => 'https://gateway.watsonplatform.net/natural-language-classifier/api/v1/classifiers',
 CURLOPT_RETURNTRANSFER => true,

 CURLOPT_POST => true,
 CURLOPT_HTTPHEADER => array(
"Authorization: Basic 12345",
"cache-control: no-cache"
),

 CURLOPT_POSTFIELDS => [
 $uploadFilePostKey => $uploadFile,
 $metaPostKey => "{\"language\":\"{$lang}\",\"name\":\"{$name}\"}"
],
] );

 $response = curl_exec( $curlHandler );
 $err = curl_error( curlHandler );

 curl_close( curlHandler );

 if ( $err ) {
 echo "cURL Error #:" . $err;
} else {

 echo $response;

}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM