Post

1 follower Follow
0
Avatar

PHP API: Can anyone see this code and error message?

Hi,

I'm coding a PHP script to create a document (STEP 1) and link it to an existing task(STEP2) and add a URL shortcut to the document.(STEP3).

Step 1 and 2 are ok, but Step 3 gives me an error and I cannot find any PHP API help except from here.

Here are my code and error message in the web page.

Thanks for your help in advance!

Isaac.

//////////////////////

///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 shortcut for that document

 

  //Step 1:

      //Create an entity representing the new Doc

      $newTask = new stdClass();

      $newTask->Id = new stdClass();

      $newTask->Id->TypeName = 'Document';

      $newTask->Id->Value = null;

      //Set Doc name

      $nameField = new stdClass();

      $nameField->FieldName = "Name";

      $nameField->Value = new SoapVar("TEST3 for ECR-004531", 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 ECR 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

      $newTask->Values = array($nameField, $docTypeField,$descField);

      $createMeesage = new stdClass();

      $createMeesage->Entity = new SoapVar($newTask, SOAP_ENC_OBJECT, "GenericEntity", $soapApiUrl);

      $request[] = new SoapVar($createMeesage, SOAP_ENC_OBJECT, 'CreateMessage',$soapApiUrl);

      $result = $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 task

      $parent_task = array ("TypeName"=>"Task", "Value" => "fb2839d1-cd62-414c-b5c5-32323dce6778");

      ///fb2839d1-cd62-414c-b5c5-32323dce6778    Task  ECR-004531

      $entityField = new stdClass();

      $entityField->FieldName = 'Entity';

      $entityField->Value = new SoapVar($parent_task, SOAP_ENC_OBJECT, "EntityId",$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",$soapApiUrl);

 

      $link->Values = array($entityField, $documentId);

      $createMeesage = new stdClass();

      $createMeesage->Entity = new SoapVar($link, SOAP_ENC_OBJECT, "GenericEntity", $soapApiUrl);

 

      $request = array();

      $request[] = new SoapVar($createMeesage, SOAP_ENC_OBJECT, 'CreateMessage', $soapApiUrl);

      $result = $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 = $document->Id;

      $upload->FileInformation = new stdClass();

      $upload->FileInformation->Storage = $Stroage->Url;

      $FileUrl = "http://cnn.com/";

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

      $createMeesage = new stdClass();

      $createMeesage->Entity = new SoapVar($upload, SOAP_ENC_OBJECT,"GenericEntity", $soapApiUrl);

      $request = array();

      $request[] = new SoapVar($createMeesage, SOAP_ENC_OBJECT, 'UploadMessage', $soapApiUrl);

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

      $responseJSON = json_encode($result);

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

 

 

========== Error message ================

 

object(stdClass)#15 (1) { ["ExecuteResult"]=> object(stdClass)#16 (1) { ["Result"]=> object(stdClass)#30 (3) { ["Error"]=> NULL ["Success"]=> bool(true) ["Id"]=> object(stdClass)#31 (2) { ["TypeName"]=> string(22) "WorkItemAttachmentLink" ["Value"]=> string(36) "5e5971c3-331c-4404-a359-2cb89baf5427" } } } } string(725) "The formatter threw an exception while trying to deserialize the message: There was an error while trying to deserialize parameter http://clarizen.com/api:request. The InnerException message was 'Error in line 2 position 378. Element 'http://clarizen.com/api:BaseMessage' contains data from a type that maps to the name 'http://clarizen.com/api:UploadMessage'. The deserializer has no knowledge of any type that maps to this name. Consider using a DataContractResolver or add the type corresponding to 'UploadMessage' to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding it to the list of known types passed to DataContractSerializer.'. Please see InnerException for more details."

 

Isaac Jeong Answered

Please sign in to leave a comment.

1 comment

0
Avatar

Hi Issac.

The error you got points that UploadMessage is to be found in 'http://clarizen.com/api/files' namespace.

Please note that not all types of the API are in the same namespace.

Also note that the UploadMessage should be used directly, not encapsulated within a CreateMessage, something like:

 $request[] = new SoapVar($upload, SOAP_ENC_OBJECT, 'UploadMessage', $soapApiUrl+'/files');

 

Hope this helps.

Ophir Kenig 0 votes
Comment actions Permalink