试图将顾客发布到KOHA LMS,但我一直收到错误400,如下所示;
{
//Here is missing some code.
"errors": [{
"message": "Missing property.",
"path": "/body/address"
},
{
"message": "Missing property.",
"path": "/body/category_id"
},
{
"message": "Missing property.",
"path": "/body/city"
},
{
"message": "Missing property.",
"path": "/body/library_id"
},
{
"message": "Missing property.",
"path": "/body/surname"
}],
"status": 400
}
我已经尽我所能绕过这个错误,但似乎没有关于KOHA集成的具体文件。如果有人让它工作,请提供善意的帮助。
下面是显示curl代码的部分代码;
{
//Here is missing some code.
$url2 = "http://example.com:8000/api/v1/patrons";
$username = "user";
$password = "pass";
$data = array(
'addPatron' => array(
'address' => "",
'city' => "",
'cardnumber' => $num,
'firstname' => $fname,
'surname' => $sname,
'other_name' => $mname,
'phone' => $phone,
'email' => $email,
'category_id' => $category,
'library_id' => $library_id,
)
);
/*$data = array(
'address' => "",
'city' => "",
'cardnumber' => $num,
'firstname' => $fname,
'surname' => $sname,
'other_name' => $mname,
'phone' => $phone,
'email' => $email,
'category_id' => $category,
'library_id' => $library_id
);*/
$ch3 = curl_init($url2);
curl_setopt($ch3, CURLOPT_USERPWD, $username. ":".$password);
curl_setopt($ch3, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch3, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch3, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch3, CURLOPT_POSTFIELDS, http_build_query($data));
$headers = array();
$headers[] = 'Cookie: CGISESSID='.$cookies['CGISESSID'];
$headers[] = 'Pragma: no-cache';
$headers[] = 'Upgrade-Insecure-Requests: 1';
curl_setopt($ch3, CURLOPT_HTTPHEADER, $headers);
$d = curl_exec($ch3);
curl_close($ch3);
echo "<pre>";
print_r(json_decode($d));
echo "</pre>";
}
上面的代码基本上是试图将Koha的顾客发布到图书馆管理系统中。到目前为止,我已经能够获得顾客了。事实证明,使用相同的场景来发布是很困难的。如有任何帮助或提示,我们将不胜感激。
根据https://demo.bibkat.no/api/v1/.html#op-后客户(顺便说一下,你应该链接到API在你的问题中的文档(,这个API想要JSON请求,但你用http_build_query((发送application/x-www-form-urlencoded
请求
更换
curl_setopt($ch3, CURLOPT_POSTFIELDS, http_build_query($data));
带有
curl_setopt($ch3, CURLOPT_POSTFIELDS, json_encode($data));
并添加
$headers[] = 'Content-Type: application/json';
也取代
curl_setopt($ch3, CURLOPT_CUSTOMREQUEST, "POST");
带有
curl_setopt($ch3, CURLOPT_POST, 1);
引用libcurl CURLOPT_CUSTOMREQUEST文档@https://curl.se/libcurl/c/CURLOPT_CUSTOMREQUEST.html:
许多人错误地使用此选项将整个请求替换为自己的请求,包括多个标头和POST内容。虽然这在许多情况下可能有效,但它会导致libcurl发送无效请求,并且可能会严重混淆远程服务器。使用CURLOPT_POST和CURLOPT_POSTFIELDS设置POST数据。
严格来说,你应该向api维护人员做一个错误报告,当收到Content-Type: application/x-www-form-urlencoded
而期望JSON时,正确的响应不是Http 400 Bad Request
(尽管它很接近(,正确的反应是
HTTP 415不支持的媒体类型
- https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/415