How do you upload a file using the API?
Files are uploaded as form-encoded data. Here is an example using PHP:
function uploadFile($parentId, $fileName)
{
$restVerb = self::RESTVERB_POST;
$uri = '/' . self::ITEM_CONTACTS . '/' . self::ACTIVITY_FILE . '/';
$boundary = "---------------------".substr(md5(rand(0,32000)), 0, 10);
$data = "--$boundary\n";
$data .= "Content-Disposition: form-data; name=\"parent\"\n\n" . $parentId . "\n";
$data .= "--$boundary\n";
$data .= "Content-Disposition: form-data; name=\"file\"; filename=\"$fileName\"\n";
$data .= "Content-Type: application/pdf\n";
$data .= "Content-Transfer-Encoding: binary\n\n";
$data .= file_get_contents($fileName)."\n";
$data .= "--$boundary--\n";
// get the connection handler
$connection = $this->_connect();
// Prepare request body
$request = "$restVerb $uri HTTP/1.1\r\n"
. "Host: " . $this->getHost() . "\r\n"
// Authorization header
. "Authorization: Basic " . $this->getCredentials() . "\r\n"
// We inform the server that we're sending data in xml format
. "Content-Type: multipart/form-data; boundary=$boundary\r\n"
// We inform the server that we are waiting for xml in response
. "Accept: application/xml\r\n"
. "Content-Length: " . strlen($data) . "\r\n"
. "Connection: close\r\n\r\n"
. $data;
// Send request to the host
fwrite($connection, $request);
// Get the xml from the response
$xml = $this->_getXmlResponse($connection);
// Disconnection
fclose($connection);
if ($xml) {
// Convert it to the SimpleXmlElement
$xml = simplexml_load_string($xml);
} else {
// Something went wrong and we haven't got xml in the response
throw new Exception('System error while working with Solve360 service');
}
return $xml;
}