We are using the API to add documents and when I use the end point document_types/products/documents.json we manage to get success. However anytime I try to use document_types/pages/documents/bulk_create_or_update_verbose I have not got anything other than the error given below:
Unexpected error saving document. Contact support@swiftype.com if the problem persists
I have contacted but we haven’t got anywhere (I would have thought there would a log of which could be looked at). All I have had come back was to use json_encode() which was already being used (see the first option in the switch statement). Just to clarify, the request works fine for all calls other than this one, the data is formatted as per the documentation so I can’t see any reason why that would cause it. Has anyone else had this issue?
public function addPageDocument()
{
$data = ["documents" =>
[
"external_id" => "1346",
"fields" => [
[
"name" => "title",
"value" => "Sample page",
"type" => "string"
],[
"name" => "url",
"value" => "https://www.thespicery.com",
"type" => "string"
]
]
]
];
return $this->curlWrap("document_types/pages/documents/bulk_create_or_update_verbose", $data, "POST");
}
public function curlWrap($endPoint, $data, $action)
{
try {
$credentials = $this->getSwiftypeCredentials();
$endPoint = "https://api.swiftype.com/api/v1/engines/{$credentials["engine_slug"]}/{$endPoint}";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $endPoint);
curl_setopt($ch, CURLOPT_USERPWD, $credentials["auth_token"]);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
// See the JSON encode below
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json'));
curl_setopt($ch, CURLOPT_USERAGENT, "MozillaXYZ/1.0");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$output = curl_exec($ch);
if ($output == false) {
throw new \Exception(curl_error($ch), curl_errno($ch));
}
curl_close($ch);
$decoded = json_decode($output);
return $decoded;
} catch (\Exception $e) {
$this->auditHelper->addLogToRollbar(
\Rollbar\Payload\Level::ERROR,
"Swiftype Curl Failed {$e->getCode()} {$e->getMessage()}");
return false;
}
}