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
Update Task Description - task_update
*Posted on behalf of a customer. Original posting date 2017-01-12.*
What is the correct URL and JSON format?
Here is my code, and don't worry about the $super_user_lp, it is working. Only the task_update is not working, and has no response by thisAPI call.
```
$response_update_task_prefix = $super_user_lp->task_update($task_id, $update_data);
/*
$update_data['name'] = "Prefix"."Original Task Name";
$update_data['description'] = "Prefix"."Original Description";
//By the way, may I update the Note like this:?
$update_data['note'] = "New Note"."Original Note";
*/
```
```
function task_update($taskid, $data){
$encodedTask = json_encode(array('task' => $data));
$url = 'https://app.liquidplanner.com/api/workspaces/{space ID}/tasks/'.$taskid;
$response = $this->lp_put($url, $encodedTask);
return($response);
}
```
```
function lp_put($endpoint_url, $encodedTask){
$curl = curl_init($endpoint_url);
curl_setopt($curl, CURLOPT_HEADER, false); //$conn
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: application/json"));
curl_setopt($curl, CURLOPT_ENCODING, "gzip");
```
```
// curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($curl, CURLOPT_URL, $endpoint_url);
// curl_setopt($curl, CURLOPT_POSTFIELDS, $encodedTask);
curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query($encodedTask));
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec( $curl );
$error = curl_error($curl);
// curl_close($curl);
```
```
$results = json_decode($response, true);
return $results;
}
```
Posted by LiquidPlanner Support 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 almost 5 years ago
Creating project | php
*Posted on behalf of Jorn Smits. Original posting date 2014-11-19.*
Hi
I'm trying to add a project in Liquidplanner with php, but i can't get it to work. I'm always getting the 500 error ( Internal Server Error)
I'm using the liquidplanner class from github
```
https://github.com/jonoxer/php-liquidplanner/blob/master/liquidplanner.php
```
Haven't adapted anything, this is the code i'm using
```
<?php
require_once('liquidplanner.php');
$name = "Test project";
$parent_id = xxxxxxx;
$client_id = xxxxxx;
$description = "Test";
/* Create an instance of the Liquid Planner object */
$lp = new LiquidPlanner("xxxxx", "xxxxxxx", "xxxxxxx");
/* Create a new task in Liquid Planner */
$response = $lp->projects_create($name,$client_id,$parent_id,$description);
var_dump($response);
// echo $response
?>
```
I've replaced all parameters with correct data.
Can someone please tell me what I'm doing wrong.
**Projects_create function:**
```
public function projects_create($name, $client_id, $parent_id, $description = '', $is_done = false, $done_on = '', $external_reference = '')
```
```
{
$encodedClient = json_encode(array('project' => array(
'name' => $name,
'client_id' => $client_id,
'parent_id' => $parent_id,
'description' => $description,
'is_done' => $is_done,
'done_on' => $done_on,
'external_reference' => $external_reference
)));
var_dump($encodedClient);
$url = $this->serviceurl.'/projects';
var_dump($url);
$response = $this->lp_post($url, $encodedClient);
return($response);
}
```
**LP_post function:**
```
private function lp_post($url, $encodedTask)
```
```
{
var_dump($encodedTask);
var_dump($this->email);
var_dump($this->password);
/* 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: application/json")); // Must submit as JSON
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, $encodedTask); // 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);
var_dump($response);
curl_close($conn);
/* The response is JSON, so decode it and return the result as an array */
$results = json_decode($response, true);
var_dump($results);
```
```
/* Check for Throttling from the API */
if((isset($results['type']) && $results['type'] == "Error") && (isset($results['error']) && $results['error'] == "Throttled"))
```
```
{
//We're being throttled. Wait the right amount of time and call it again.
$this->throttle_message($results);
sleep($this->get_wait_time($results['message']));
return $this->lp_post($url, $encodedTask);
}
return $results;
}
```
**Contructor:**
```
public function __construct($workspaceID, $email, $password)
```
```
{
$this->email = $email;
$this->password = $password;
$this->baseurl = "https://app.liquidplanner.com/api";
$this->serviceurl = $this->baseurl . "/workspaces/".$workspaceID;
}
```
Posted by LiquidPlanner Support about 5 years ago
Can I add assignment on initial API task creation
*Posted on behalf of Vince Farina. Original posting date 2014-10-31.*
After reading the READ ME file, it appears that adding assignments without knowing the task ID is possible.
EXCERPT
Creating a treeitem and setting ownership in one request
Since there is no longer, an owner_id attribute on Treeitems, you will need to update the way you set ownership when creating a Treeitem. Instead of setting “owner_id”, you can set the “assignments” to an array of hashes with “person_id” keys. Like this:
(...)
“name”: “Task Name”,
“assignments”: [ { “person_id”: 5} ]
(...)
END EXCERPT
However my php code creates the task, but leaves the task 'unassigned'.
$task = array(
'name' => $lp->dgTaskName,
'parent_id' => $lp->dgTaskFolder, // Specific to Client
'description' => $lp->dgTaskDesc, // Project Type
'promise_by' => $lp->dgTaskDeadline,
'max_effort' => $lp->dgTaskMaxHrs,
'activity_id' => '42587', // constant - client work billable
'custom_field_values' => array('Status' => 'Requested', 'Priority' => $lp->dgTaskPriority),
'package_id' => $lp->dgOwnerPackage ,
'external_reference' => $lp->dgTaskRefID ,
'assignments' => array('person_id' => $lp->dgTaskOwner)
);
// create new task
$result = $lp->create_task($task);
Thanks.
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
Getting a 500 error when creating a task with PHP
*Posted on behalf of Mark Langridge. Original post date 2014-06-30*
This is pretty much the same problem as the JAVA one posted earlier. However I am using double quotes, and can't see anything else that could be causing this.
Target Url
https://app.liquidplanner.com/api/workspaces/120300/tasks
Posted String
`` {"task":{"name":"API Task","parent_id":15821159}} ``
The cURL trace is
```
* Adding handle: conn: 0x8eb06b0
* Adding handle: send: 0
* Adding handle: recv: 0
* Curl_addHandleToPipeline: length: 1
* - Conn 0 (0x8eb06b0) send_pipe: 1, recv_pipe: 0
* About to connect() to app.liquidplanner.com port 443 (#0)
* Trying 93.184.219.117...
* Connected to app.liquidplanner.com (93.184.219.117) port 443 (#0)
* SSL connection using ECDHE-RSA-AES128-GCM-SHA256
* Server certificate:
* subject: C=US; ST=Washington; L=Seattle; O=LiquidPlanner Inc.; OU=Technology; CN=*.liquidplanner.com
* start date: 2014-02-07 00:00:00 GMT
* expire date: 2017-02-17 12:00:00 GMT
* issuer: C=US; O=DigiCert Inc; OU=www.digicert.com; CN=DigiCert SHA2 High Assurance Server CA
* SSL certificate verify result: unable to get local issuer certificate (20), continuing anyway.
* Server auth using Basic with user '**removed**'
> POST /api/workspaces/120300/tasks HTTP/1.1
Authorization: Basic **removed**
Host: app.liquidplanner.com
Accept: */*
Accept-Encoding: gzip
User-Agent: KsCogs Curl Handler 1.0
content-type: application/json
Content-Length: 120
* upload completely sent off: 120 out of 120 bytes
< HTTP/1.1 500 Internal Server Error
< Content-Encoding: gzip
< Cache-Control: no-cache
< Cache-Control: max-age=900
< Content-Type: application/json; charset=utf-8
< Date: Mon, 30 Jun 2014 12:56:20 GMT
< Expires: Mon, 30 Jun 2014 13:11:20 GMT
* Server Apache is not blacklisted
< Server: Apache
< Status: 500 Internal Server Error
< Strict-Transport-Security: max-age=31536000
< Vary: Accept-Encoding
< X-Frame-Options: SAMEORIGIN
< X-Rack-Cache: invalidate, pass
< X-Request-Id: 3684a601bdbd063a94fed827228082c2
< X-UA-Compatible: IE=Edge,chrome=1
< Content-Length: 118
<
* Connection #0 to host app.liquidplanner.com left intact
```
And the response is
```
stdClass Object
(
[type] => Error
[error] => InternalServerError
[message] => Internal error; try again and perhaps it will work (no promises).
)
```
anyone spot anything obvious?
Posted by LiquidPlanner Support about 5 years ago
Using PHP to add tasks through the API
*Posted on behalf of Chris Adkins. Original post date 2014-06-19*
I am using PHP to add tasks through the API.
I am having trouble adding a line break to the description, everything I have tried has been escaped.
Is there a way I can add a line break?
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