LiquidPlanner Classic Forum
Upload Documents via API
Hello,
I'm having issues uploading files up to Liquid Planner through the use of the API.
In an effort to solve this, I even created a simple form that uploads a data file to a temporary file then attempts the upload by calling the API.
So far I found a complete example that should work provided in this form but still seem to not be having any luck.
https://developer.liquidplanner.com/discuss/5d5c31a3f2fb1d038ed1c396
Code Snip it:
//https://developer.liquidplanner.com/discuss/5d5c31a3f2fb1d038ed1c396
$url = "https://app.liquidplanner.com/api/workspaces/" . $workspaceID . "/tasks/" . $taskId . "/documents";
$file = realpath($fileLocation);
$data = array(
"document[file_name]" => $fileName,
"document[description]" => $fileDesc,
"document[attached_file]" => "@".$fileLocation,
);
var_dump($data);
$conn = curl_init();
curl_setopt($conn, CURLOPT_HEADER, 0);
curl_setopt($conn, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($conn, CURLOPT_ENCODING, "");
curl_setopt($conn, CURLOPT_USERPWD, $email . ":" . $password );
curl_setopt($conn, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($conn, CURLOPT_POST, true);
curl_setopt($conn, CURLOPT_URL, $url);
curl_setopt($conn, CURLOPT_POSTFIELDS, $data);
$response = curl_exec($conn);
print_r( $response."\n" );
curl_close($conn);
This is what var_dump is reporting:
C:\wamp64\www\EIS\LP_Success.php:88:
array (size=3)
'document[file_name]' => string 'ckt2.pdf' (length=8)
'document[description]' => string 'application/pdf' (length=15)
'document[attached_file]' => string '@C:\wamp64\www\EIS\uploads\ckt2.pdf' (length=35)
{"type":"Error","error":"BadArgument","message":"You must POST a document[attached_file] part when creating a document."}
I even went as far as to create a submit form coupled with a process file form to try to get this to work outside of my PHP App. If you would like me to send you the full source which is 2 files I'll be happy to do so.
Thanks
Miguel Grajeda
Posted by Miguel Grajeda almost 5 years ago
How to get upload file path? PHP
*Posted on behalf of Feng. Original posting date 2016-12-09.*
Here is the code. And the question is how to get the correct $file_path? The file supposed to be located on the local folder. Thanks!
```
$attachment_filename = "test.jpg";
$file_path = "@$attachment_filename";
```
OR something like this:
```
$attachment_filename= $_FILES['userfile']['name'] ;
$tmp_name = $_FILES['userfile']['tmp_name'];
$dest_dir = 'upLoad';
$file_path = $dest_dir.'/'.$attachment_filename;
```
...
OR
.... your suggestion code
```
//----------------------------------------------
$upload_file_response = $lp->attachDocument($file_path,$attachment_filename, $task_id);
```
```
function attachDocument($file_path,$filename, $taskid) {
```
```
$Data = array('document[attached_file]' => $file_path, 'document[file_name]'=> $filename);
```
```
$url = $this->serviceurl . '/tasks/' . $taskid . '/documents';
$response = $this->lp_attach_file($url, $Data);
return($response);
}
```
```
function lp_attach_file($endpoint_url, $Data) {
```
```
$curl = curl_init($endpoint_url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_USERPWD, trim($this->email).":".trim($this->password));
curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-type: multipart/form-data"));
curl_setopt($curl, CURLOPT_ENCODING, ""); // Prevent GZIP compression of response from LP
```
```
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_URL, $endpoint_url);
curl_setopt($curl, CURLOPT_POSTFIELDS, $Data);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // Accept any SSL certificate
$response = curl_exec($curl);
$error = curl_error($curl);
```
```
$results = json_decode($response, true);
```
```
return $results;
}
```
Posted by LiquidPlanner Support about 5 years ago
Why can't I seem to upload a document to a project? Help please!
*Posted on behalf of Daniel. Original posting date 2014-08-29.*
Here is an excerpt of my code, for some reason it is not working, I get no response...
```
$files = array(
"document[file_name]" => "REF_test.jpg",
"document[attached_file]" => "@http://staff:8888/wp-content/uploads/gravity_forms/1-3bab614b6c004fcb7b90a7b24fea4764/2014/08/test.jpg",
);
```
```
curl_setopt($conn, CURLOPT_URL, $url."/projects/".$projectID."/documents" );
curl_setopt($conn, CURLOPT_POST, true);
curl_setopt($conn, CURLOPT_POSTFIELDS, $files);
```
```
$responseDocument = json_decode(curl_exec($conn));
```
I know the project ID works because I am attaching a Note tot he project just fine. Do you see anythign that should make this not work?
Posted by LiquidPlanner Support about 5 years ago
Document Update through the API
*Posted on behalf of Damian Sima. Original posting date 2013-05-16.*
Hi guys,
Could someone please explain me how the update process of a document should work through the API and what implies?
For instance the creation of a document is different from the rest of the entities, so:
Does the update of a document allow you to change the file related to that document for a new one?
Does it allow you only to change the properties of the document entity such as the name, the description or the file name?
What should the payload of the PUT operation looks like?
Cheers, Damian.
Posted by LiquidPlanner Support about 5 years ago
Adding Document to Task
*Posted on behalf of a customer. Original posting date 2013-12-27*
Hi There,
I have a form creating a task then adding a note and a comment to it after creation and it is working perfectly. I am using php and also trying to upload a file after the task is created and can't seem to get it to work. The error in the response I am getting is "You must POST a document[attached_file] part when creating a document." which seems like it means it isen't getting the file but if I echo the value I am putting in there it seems to work. Below is my code, what am I doing wrong?
```
//Document Creation
//Fields to fill in the document json format
$doc = array(
"document" => array(
"file_name" => $_FILES['file']['name'],
"attached_file" => file_get_contents("files/".$_FILES['file']['name'])
)
);
curl_setopt($conn, CURLOPT_URL, $url."/tasks/".$responseOne->id."/documents" );
curl_setopt($conn, CURLOPT_POST, true);
curl_setopt($conn, CURLOPT_POSTFIELDS, json_encode( $doc ) );
$responseFour = json_decode(curl_exec($conn)); //Get the data returned by LP and reformat from JSON
```
Thanks for the help!
Posted by LiquidPlanner Support about 5 years ago
Uploading a file to the task. [PHP] [cURL] [REST API]
*Posted on behalf of John Z. Original posting date 2012-11-26.*
Hello, I am using the php client that you can get on [github](https://github.com/jonoxer/php-liquidplanner). I am having trouble uploading a file to a task.
Well, this file is actually a string, and I want to upload that string as the file to the task. Below are two functions that I am using that I added to the client.
```
$data = the string that I want uploaded as a txt file.
$taskid is the correct id for the task.
public function attachDocument($data, $taskid) {
$encodedData = array('attached_file' => $data, 'file_name'=>'testing.txt');
$url = $this->serviceurl . '/tasks/' . $taskid . '/documents';
$response = $this->lp_attach_file($url, $data);
return($response);
}
private function lp_attach_file($url, $data) {
/* Set up the CURL object and execute it */
$conn = curl_init();
curl_setopt($conn, CURLOPT_HEADER, false); // Suppress display of the response header
curl_setopt($conn, CURLOPT_HTTPHEADER, array("Content-type: multipart/form-data"));
curl_setopt($conn, CURLOPT_RETURNTRANSFER, true); // Return result as a string
curl_setopt($conn, CURLOPT_POST, true); // Submit data as an HTTP POST
curl_setopt($conn, CURLOPT_POSTFIELDS, $data); // Set the POST field values
curl_setopt($conn, CURLOPT_ENCODING, ""); // Prevent GZIP compression of response from LP
curl_setopt($conn, CURLOPT_USERPWD, $this->email . ":" . $this->password); // Authenticate
curl_setopt($conn, CURLOPT_URL, $url); // Set the service URL
curl_setopt($conn, CURLOPT_SSL_VERIFYPEER, false); // Accept any SSL certificate
$response = curl_exec($conn);
curl_close($conn);
/* The response is JSON, so decode it and return the result as an array */
$results = json_decode($response, true);
/* Check for Throttling from the API */
if ((isset($results['type']) && $results['type'] == "Error") && (isset($results['error']) && $results['error'] == "Throttled")) {
//We're being throttled. Waith 15 seconds and call it again.
$this->throttle_message();
sleep($this->throttlewait);
return $this->lp_post($url, $data);
}
return $results;
}
```
I get a response from curl_exec, which is: ```
"{"type":"Error","error":"BadArgument","message":"Expected a hash of attribute values for document parameter"}"
```
I have been messing with this a while now, if anyone could help me out or give me some examples I would really appreciate it! Let me know if I can provide you with any more info about the program. Thanks!!
Posted by LiquidPlanner Support about 5 years ago