LiquidPlanner Classic Forum

Ask a Question
ANSWERED

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
ANSWERED

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; } ```
ANSWERED
ANSWERED
ANSWERED
ANSWERED

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!!