Hi everyone,
I'm trying to upload a file attached to clarizen project. I'm trying to do the same thing in PHP as shown in C# "3 step" example in Clarizen Web Services API Guide.
Link to guide: https://success.clarizen.com/entries/24173642-Clarizen-Web-Services-API-Guide - Document section.
In step 3 i'm keep getting "Missing argument" error. I thnik I do exactly step by step as shown in example but apparently not.
Thanks for your help in advance!
Here is my code:
///Attaching new file to work item is a 3 step procedure:
///1. Create a Document entity
///2. Create a link between the entity and the WorkItem
///3. Upload a file for that document
public function uploadDocument ($pdfPath, $fileName) {
$this->login(null); //login to clarizen
echo '<pre>';
//Step 1:
//Create an entity representing the new Doc
$document = new \stdClass();
$document->Id = new \stdClass();
$document->Id->TypeName = 'Document';
$document->Id->Value = null;
//Set Doc name
$nameValue = new \stdClass();
$nameValue->FieldName = "Name";
$nameValue->Value = new \SoapVar($fileName, XSD_STRING, "string", "http://www.w3.org/2001/XMLSchema");
//Set Doc type
$docTypeField = new \stdClass();
$docTypeField->FieldName = "DocumentType";
$doctype = 'General';
$docTypeField->Value = new \SoapVar($doctype, XSD_STRING, "string", "http://www.w3.org/2001/XMLSchema");
//Set Description
$descField = new \stdClass();
$descField->FieldName = "Description";
$desc = 'This PDF is for Test in Clarizen. :-)';
$descField->Value = new \SoapVar($desc, XSD_STRING, "string", "http://www.w3.org/2001/XMLSchema");
//Assign the fields to the Doc
$document->Values = array($nameValue, $docTypeField, $descField);
$createDocument = new \stdClass();
$createDocument->Entity = new \SoapVar($document, SOAP_ENC_OBJECT, "GenericEntity", $this->soapApiUrl);
$request[] = new \SoapVar($createDocument, SOAP_ENC_OBJECT, 'CreateMessage',$this->soapApiUrl);
print_r($request);
$result = $this->client->Execute(array("request"=>$request));
$responseJSON = json_encode($result);
echo $responseJSON . "<hr color=red><br/>";
$new_id = $result->ExecuteResult->Result->Id;
$responseJSON = json_encode($new_id);
echo $responseJSON . "<hr color=green><br/>";
//Step 2: Create a link between WorkItem and Document
$link = new \stdClass();
$link->Id = new \stdClass();
$link->Id->TypeName = 'AttachmentLink';
//A reference to the project EST Internal Project
$parent_task = array("TypeName"=>"Project", "Value" => $projectsValue);
$entityIdField = new \stdClass();
$entityIdField->FieldName = 'Entity';
$entityIdField->Value = new \SoapVar($parent_task, SOAP_ENC_OBJECT, "EntityId",$this->soapApiUrl);
/// Reference to the Doc
$documentId = new \stdClass();
$documentId->FieldName = 'Document';
//A reference to the new document
$documentId->Value = new \SoapVar($new_id, SOAP_ENC_OBJECT, "EntityId",$this->soapApiUrl);
$link->Values = array($entityIdField, $documentId);
$createLink = new \stdClass();
$createLink->Entity = new \SoapVar($link, SOAP_ENC_OBJECT, "GenericEntity", $this->soapApiUrl);
$request = array();
$request[] = new \SoapVar($createLink, SOAP_ENC_OBJECT, 'CreateMessage', $this->soapApiUrl);
print_r($request);
$result = $this->client->Execute(array("request"=>$request));
$responseJSON = json_encode($result);
echo $responseJSON . "<HR color=blue><br/>";
//Step 3: Upload the file as a shortcut link
$upload = new \stdClass();
$upload->DocumentId = $new_id;
$upload->FileInformation = new \stdClass();
// $upload->FileInformation = new \SoapVar($upload, SOAP_ENC_OBJECT,"GenericEntity", $this->soapApiUrl);
$FileUrl = 'http://devass.loc/assets/pdf/'.$fileName.'.pdf';
$rawFile = fread(fopen($pdfPath, "r"), filesize($pdfPath));
$B64File = base64_encode($rawFile);
$upload->FileInformation->Storage = $FileUrl;
$upload->FileInformation->FileName = $fileName;
$upload->FileInformation->Content = new \SoapVar($B64File, XSD_BASE64BINARY, "base64binary", "http://www.w3.org/2001/XMLSchema");
// $upload->FileInformation->Value = new \SoapVar($B64File, XSD_STRING, "string", "http://www.w3.org/2001/XMLSchema");
$createMeesage = new \stdClass();
$createMeesage->Entity = new \SoapVar($upload, SOAP_ENC_OBJECT,"GenericEntity", $this->soapApiUrl);
$request = array();
$request[] = new \SoapVar($createMeesage, SOAP_ENC_OBJECT, 'UploadMessage', $this->soapApiUrl.'/files');
print_r($request);
$result = $this->client->Execute(array("request"=>$request));
$responseJSON = json_encode($result);
echo $responseJSON . "<HR color=black><br/>";
die();
}
======Output======
//I changed values for security reasons :)
{"ExecuteResult":{"Result":{"Error":null,"Success":true,"Id":{"TypeName":"Document","Value":"123asd-2as-2as-aa98-aaasd123"}}}}
{"TypeName":"Document","Value":"123asd-2as-2as-aa98-aaasd123"}
{"ExecuteResult":{"Result":{"Error":null,"Success":true,"Id":{"TypeName":"WorkItemAttachmentLink","Value":"123-asd-123-asd-123asd"}}}}
{"ExecuteResult":{"Result":{"Error":{"ErrorCode":"MissingArgument","Message":"Missing argument: FileInformation","ReferenceId":"UHI~sNjv~k66yV1ic67KpQ"},"Success":false}}}
Full output with shown requests objects:
http://pastebin.com/ycmGfVU3
Maybe anyone would see where I make a mistake.
Thanks!