Questions and discussion about developing processes and programming in PHP, JavaScript, web services & REST API.

Moderator: amosbatto

Forum rules: Please search to see if a question has already asked before creating a new topic. Please don't post the same question in multiple forums.
#815127
Hi there,

Is there a way to upload files to a case without using an input document via the REST API?
Just like the way files are uploaded in a Grid when in the form/web entry.

i've tried leaving the values empty, but getting a 400 back:
Code: Select all
$path = $_FILES['file']['tmp_name'];
$type = $_FILES['file']['type'];
$filename = $_FILES['file']['name'];
rename($path, sys_get_temp_dir() . '/' . $filename);

$data = array(
'inp_doc_uid' => '',
'tas_uid' => '',
'app_doc_comment' => '',
'form' => (phpversion() >= "5.5") ? new CurlFile($path, $type) : '@'.$path
);

Any help is appreciated.
Thank you.
#815131
Thanks for that!
i've been using your version 1.

OK so i've disabled the old one and then imported this 1.6 version.
Code: Select all
$url = "$eformUrl/api/1.0/$eformWs/extrarest/case/$caseId/upload";
			
if ($eformUrl && $eformWs && $_FILES["file"]['name'] != '') {
	$path = $_FILES['file']['tmp_name'];
	$type = $_FILES['file']['type'];
	$filename = $_FILES['file']['name'];
	rename($path, sys_get_temp_dir() . '/' . $filename);
	$data = array(
		'file' => (phpversion() >= "5.5") ? new CurlFile($path, $type) : '@'.$path
	);
	if (curlThis($result, $url, $token, 'POST', $data)) {
		echo json_encode($result);
		exit();
	} else {
		echo $result;
		exit();
	}
My curlThis looks like:
Code: Select all
function curlThis(&$result, $url, $access_token = '', $method = 'GET', $data = array()) {
	
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		if ($access_token != '') {
			curl_setopt($ch, CURLOPT_HTTPHEADER, array("Authorization: Bearer " . $access_token));
		}
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		if ($method == 'POST') {
			curl_setopt($ch, CURLOPT_POST, true);
			// curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
			curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
		} else if ($method == 'PUT') {
			curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
			curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
		}
	
		$result = json_decode(curl_exec($ch), true);
		$statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
		curl_close($ch);
	
		if ($statusCode != 200) {
			if (isset($result) and isset($result->error)) 
				$result = "Error code: {$result->error->code}\nMessage: {$result->error->message}\n";
			else 
				$result = "Error: HTTP status code: $statusCode\n";
	
			return false;
		}
		else 
			return true;
		
	}
I'm getting a HTTP status code: 0 as a response.
Any ideas?

Using the same function get list of uploaded documents and those work fine.
#815133
You can't pass the file through http_build_query(), since it will corrupt it and I recommend using curl_file_create().
Use the code listed in the code example:
https://sourceforge.net/p/pmcommunity/c ... _uidupload

I don't know what will happen if you have the same plugin installed twice, since they use the same files.
I recommend deleting both versions of the extraRest plugin and then reimport extraRest version 1.6.
#815134
My PHP is 5.4 so it doesnt have the curl_file_create() function.
Code: Select all
$data = array(
	'file' => (phpversion() >= "5.5") ? new CurlFile($path, $type) : '@'.$path
);
I've edited the curlThis to accept another param to check whether it needs to do http_build_query.

Deleted the extraRest files, from shared/sites/workflow/files/input and reimported 1.6.
Still getting a Http status code: 0 response, any ideas?
#815136
You have changed the path of the file, so you are trying to use a path that no longer exists.
Do it this way:
Code: Select all
$newPath = sys_get_temp_dir() . '/' . $filename;
$data = array(
	'file' => '@'.$newPath
);
#815137
Got it!
Had to move it to a proper place first then add an @/ infront of the full path:
Code: Select all
$newFilePath =  '/usr/local/itapps/www/html/processmaker/shared/sites/workflow/files/' . basename($_FILES['file']['name']);
move_uploaded_file($_FILES['file']['tmp_name'], $newFilePath);

$data = array(
	'file' => "@/$newFilePath"
);
Thanks for the plugin! I use it alot!
Can I connect to firebird database?

If you want to connect the database remotely using[…]

Task with multiple users

Well i have worked for days on this project and th[…]

Multilple Flow Possibility

Hi Is it possible to have multiple flow in a sing[…]

Hi, Please tell to processmaker team for tran[…]