12-17-2015 12:57 PM
03-28-2018 05:55 AM
Hi,
After some trial and error the following procedure works fine. Before you start:
1) be sure to have a valid user in Alfresco, preferably member of the group ALFRESCO_ADMINISTRATORS;
2) create an upload folder in the Alfesco repository and find the full nodeRef of it;
3) put a test-document in the PHP directory you are using for this test, named 'test-doc.txt'.
Find my full PHP code below (version 5.4 where CURLFile is not available, so use '@' before the name of the file to upload). Note the settings of 'content-type' as well as the POST-field types in the CURL definitions.
<?php
// file upload to Alfresco repo, PHP version 5.4
$debug = true;
set_error_handler("customError");
date_default_timezone_set('Europe/Berlin');
$ch = curl_init();
define ('USERNAME', '------');
define ('PASSWORD', '------');
define ('LOGINURL', 'http://--------:8080/alfresco/service/api/login');
define ('UPLOADURL', 'http://--------:8080/alfresco/service/api/upload');
define ('UPLOADDIR', 'workspace://SpacesStore/aa90950d-de65-4523-9adb-c2590a7d148d');
define ('TESTFILE', 'test-doc.txt');
define ('TESTTYPE', 'text/plain');
$logindata = array(
'username' => USERNAME,
'password' => PASSWORD
);
$data_str = json_encode($logindata);
// setup login (POST)
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, LOGINURL);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_str);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//execute the the login request
$result = curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
echo 'Login: '. $data_str . ' ;<br>Result: '. $result . ';<br>The status is ' . $status;
$ticket = json_decode($result)->data->ticket;
echo ';<br>The ticket is: ' . $ticket;
// setup upload
$uploadUrl = UPLOADURL . '?alf_ticket=' . $ticket;
// $args = new CURLFile($filename, $type, $filename); // does not exist PHP 5.4
$fields = array(
'nodeType'=>'cm:content',
'filedata'=>'@' . TESTFILE,
'type'=>TESTTYPE,
'destination'=>UPLOADDIR,
'uploaddirectory'=>'/',
'overwrite'=>true
);
curl_setopt($ch, CURLOPT_URL, $uploadUrl);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: multipart/form-data'));
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
$result = curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
echo '<br>End state: ' . $status . '<br>Alfresco response: ';
print_r($result);
curl_close($ch);
function customError($errno, $errstr, $errfl, $errln) {
//Merk op: geen melding in JSON response TODO?
global $debug;
if (is_bool($debug)) {
if ($debug) echo "<br><b>ERROR:</b> [$errno] $errstr in line $errln" ;
}
}
?>
12-18-2015 05:22 AM
11-03-2016 11:52 PM
hello ,i wanted to know it too.Did you solve your problem now?
11-17-2016 03:48 AM
hi ,i use your method successful upload some file to alfresco service, the key is that you must get $ticket by url(http://localhost:8080/alfresco/service/api/login?u=admin&pw=123456 ).
10-31-2017 08:03 AM
Hi,
I got the ticket id too but the problem remains same
my script is
$urlws = 'http://' . $user . ':' . $pw . '@' . $server . ':' . $port . '/alfresco/service/api/upload?alf_ticket=' . $ticket;
$fileName = '123.png';
$filepath = 'C:\xampp\htdocs\SuitCrmAlfresco_IssuesFixation\upload\123.png';
$postvars = array(
'filename' => $fileName,
'filedata' => '@' . getcwd() . "\\" . $filepath,
'destination' => $createFolderResponse,
'uploaddirectory' => '/',
'description' => 'File example',
'contenttype' => 'cm:content',
'overwrite' => 'true',
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_PORT, $port);
curl_setopt($ch, CURLOPT_URL, $urlws);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postvars);
$uploadFileResponse = json_decode(curl_exec($ch));
echo("<pre>");
print_r($uploadFileResponse);exit;
And I got this Error
stdClass Object ( [status] => stdClass Object ( [code] => 400 [name] => Bad Request [description] => Request sent by the client was syntactically incorrect. ) [message] => Required parameters are missing [exception] => [callstack] => Array ( ) [server] => Community v5.2.0 (re21f2be5-b22) schema 10,057 [time] => Oct 31, 2017 5:00:48 PM )
04-03-2017 07:11 AM
Hi, I'm struggling with the same issue. Did you resolve it in the meantime? Please let me know. Adrie.
10-31-2017 08:17 AM
hi adrie,
did you find any solution ?
11-30-2017 03:09 AM
I'm using alfresco 5.2 the url of the api is different but perhaps the way it works and parameters are the same, here is a working upload file in php :
for the test the file i upload is in the same directory than my script
03-28-2018 05:55 AM
Hi,
After some trial and error the following procedure works fine. Before you start:
1) be sure to have a valid user in Alfresco, preferably member of the group ALFRESCO_ADMINISTRATORS;
2) create an upload folder in the Alfesco repository and find the full nodeRef of it;
3) put a test-document in the PHP directory you are using for this test, named 'test-doc.txt'.
Find my full PHP code below (version 5.4 where CURLFile is not available, so use '@' before the name of the file to upload). Note the settings of 'content-type' as well as the POST-field types in the CURL definitions.
<?php
// file upload to Alfresco repo, PHP version 5.4
$debug = true;
set_error_handler("customError");
date_default_timezone_set('Europe/Berlin');
$ch = curl_init();
define ('USERNAME', '------');
define ('PASSWORD', '------');
define ('LOGINURL', 'http://--------:8080/alfresco/service/api/login');
define ('UPLOADURL', 'http://--------:8080/alfresco/service/api/upload');
define ('UPLOADDIR', 'workspace://SpacesStore/aa90950d-de65-4523-9adb-c2590a7d148d');
define ('TESTFILE', 'test-doc.txt');
define ('TESTTYPE', 'text/plain');
$logindata = array(
'username' => USERNAME,
'password' => PASSWORD
);
$data_str = json_encode($logindata);
// setup login (POST)
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, LOGINURL);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_str);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//execute the the login request
$result = curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
echo 'Login: '. $data_str . ' ;<br>Result: '. $result . ';<br>The status is ' . $status;
$ticket = json_decode($result)->data->ticket;
echo ';<br>The ticket is: ' . $ticket;
// setup upload
$uploadUrl = UPLOADURL . '?alf_ticket=' . $ticket;
// $args = new CURLFile($filename, $type, $filename); // does not exist PHP 5.4
$fields = array(
'nodeType'=>'cm:content',
'filedata'=>'@' . TESTFILE,
'type'=>TESTTYPE,
'destination'=>UPLOADDIR,
'uploaddirectory'=>'/',
'overwrite'=>true
);
curl_setopt($ch, CURLOPT_URL, $uploadUrl);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: multipart/form-data'));
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
$result = curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
echo '<br>End state: ' . $status . '<br>Alfresco response: ';
print_r($result);
curl_close($ch);
function customError($errno, $errstr, $errfl, $errln) {
//Merk op: geen melding in JSON response TODO?
global $debug;
if (is_bool($debug)) {
if ($debug) echo "<br><b>ERROR:</b> [$errno] $errstr in line $errln" ;
}
}
?>
Tags
Find what you came for
We want to make your experience in Hyland Connect as valuable as possible, so we put together some helpful links.