LiquidPlanner Classic Forum
Uploading a file to the task. [PHP] [cURL] [REST API]
over 5 years ago by LiquidPlanner Support(edited)
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. 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!!