Post

1 follower Follow
0
Avatar

Clarizen API: "Missing argument: FileInformation" when trying to upload file to clarizen project

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!

Tomasz Zacharczuk Answered

Please sign in to leave a comment.

7 comments

0
Avatar

According to our logs, the UploadMessage is coming in empty (without the FileInformation part).

I think this is a namespace issue, can you try the following change and let me know the results:

 

//$upload->FileInformation = new \stdClass();

 $upload->FileInformation = new \SoapVar($upload, SOAP_ENC_OBJECT,"GenericEntity", $this->soapApiUrll.'/files');

Eyal Post 0 votes
Comment actions Permalink
0
Avatar

Hi Eyal,

I changed my code as you suggested but still the same error. I tried also changing soapApiUrls and request structure but didn't help. Step 3 looks like this now:

//Step 3: Upload the file as a shortcut link

$upload = new \stdClass();

$upload->DocumentId = $new_id;

// $upload->FileInformation = new \stdClass();

// $FileUrl = 'http://4f58c8c3.ngrok.com/assets/pdf/'.$fileName.'.pdf';

$FileUrl = 'http://devass.loc/assets/pdf/'.$fileName.'.pdf';

$rawFile = fread(fopen($pdfPath, "r"), filesize($pdfPath));

$B64File = base64_encode($rawFile);

$FileInformation = new \stdClass();

$FileInformation->Storage = $FileUrl;

$FileInformation->FileName = $fileName;

$FileInformation->Content = new \SoapVar($B64File, XSD_BASE64BINARY, "base64binary", "http://www.w3.org/2001/XMLSchema");

$FileInformation->Value = new \SoapVar($FileUrl, XSD_STRING, "string", "http://www.w3.org/2001/XMLSchema");

$upload->FileInformation = new \SoapVar($FileInformation, SOAP_ENC_OBJECT,"FileInformation", $this->soapApiUrl.'/files');

$createMeesage = new \stdClass();

$createMeesage->Entity = new \SoapVar($upload, SOAP_ENC_OBJECT,"GenericEntity", $this->soapApiUrl.'/files');

$request = array();

$request[] = new \SoapVar($createMeesage, SOAP_ENC_OBJECT, 'UploadMessage', $this->soapApiUrl.'/files');

print_r($request);

// die();

$result = $this->client->Execute(array("request"=>$request));

$responseJSON = json_encode($result);

echo $responseJSON . "<HR color=black><br/>";

==output==

{"ExecuteResult":{"Result":{"Error":{"ErrorCode":"MissingArgument","Message":"Missing argument: FileInformation","ReferenceId":"_Bt2ToLR20KKQDXUbecKSg"},"Success":false}}}

Tomasz Zacharczuk 0 votes
Comment actions Permalink
0
Avatar

Do you have a way to extract the XML that is being sent before it goes out from your server? If so, can you post it?

 

Eyal Post 0 votes
Comment actions Permalink
0
Avatar

Should check XML at first place...

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://clarizen.com/api/files" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ns2="http://clarizen.com/api" xmlns:ns3="http://clarizen.com/api/queries">

SOAP-ENV:Header

ns2:Session

ns2:ID97cfe885-1994-40a8-902b-6ab10a88c163_249045/ns2:ID

/ns2:Session

ns3:Session

<item>

<key>ID</key>

<value>97cfe885-1994-40a8-902b-6ab10a88c163_249045</value>

</item>

/ns3:Session

/SOAP-ENV:Header

SOAP-ENV:Body

ns2:Execute

ns2:request

<ns2:BaseMessage xsi:type="ns1:UploadMessage"/>

/ns2:request

/ns2:Execute

/SOAP-ENV:Body

/SOAP-ENV:Envelope

but still don't know why it is empty... previous request are done the same way and xml looks fine

Tomasz Zacharczuk 0 votes
Comment actions Permalink
0
Avatar

I finally find out mistake. 

The right way to build request object is:

$FileInformation = new \stdClass();

$FileInformation->Storage = "Url";

$FileInformation->FileName = $fileName;

$FileInformation->Url = $FileUrl;

$uploadMeesage = new \stdClass();

$uploadMeesage->DocumentId = new \SoapVar($new_id, SOAP_ENC_OBJECT, "EntityId", $this->soapApiUrl);

$uploadMeesage->FileInformation = new \SoapVar($FileInformation, SOAP_ENC_OBJECT, "FileInformation", $this->soapApiUrl.'/files');

$request = array();

$request[] = new \SoapVar($uploadMeesage, SOAP_ENC_OBJECT, 'UploadMessage', $this->soapApiUrl.'/files');

$result = $this->client->Execute(array("request"=>$request));

 

but yet there is a problem. When I want to use "server" option:

$FileInformation->Storage = "Server";

then I get this error:

{"ExecuteResult":{"Result":{"Error":{"ErrorCode":"Internal","Message":"An internal error has occurred, the error was logged and will be examined.","ReferenceId":"B44mnf1bKU6Kq3TmLqVv6g"},"Success":false}}}

It gives me no clue what is wrong so I suppose the problem might be on Clarizen site.

Tomasz Zacharczuk 0 votes
Comment actions Permalink
0
Avatar

[SOLUTION]

When I'm using "server" option I must send content of the file in request to clarizen.

$FileInformation = new \stdClass();

$FileInformation->Storage = "Server";

$FileInformation->FileName = $fileName;

$FileUrl = 'http://devass.loc/assets/pdf/'.$fileName;

$FileInformation->Url = $FileUrl;

$pdf = fopen($pdfPath,'r');

$pdf_content = fread ($pdf,filesize($pdfPath));

$FileInformation->Content = $pdf_content;

 

Maybe It saves someone some time.

Tomasz Zacharczuk 0 votes
Comment actions Permalink