Help me convert Postman to PHP CURL

cancel
Showing results for 
Search instead for 
Did you mean: 
coisox
Active Member

Help me convert Postman to PHP CURL

Jump to solution

I've tried so much codes found on the internet but can't get it to work. Maybe due to different Alfresco version or PHP version, I don't know.

I successfully upload new file through POSTMAN. Now I want to upload file using Dropzone.js and pass it to PHP file.

Here's my POSTMAN:

When uploading using Dropzone.js, the PHP file receive the file through $_FILES. Here's the var_dump($_FILES):

array(5)
{
  ["name"] => string(12)"contacts.csv"
  ["type"] => string(24)"application/vnd.ms-excel"
  ["tmp_name"] => string(14)"/tmp/phpGJagTg"
  ["error"] => int(0)
  ["size"] => int(2767)
}

Here're two of my latest codes that are not working:

CODE 1:

$data = http_build_query(array(
"filedata" => $_FILES["file"]["tmp_name"],
"destination" => "workspace://SpacesStore/$myNodeRef",
"uploadDirectory" => "p",
"description" => "",
"contenttype" => "cm:content",
"overwrite" => "true",
"thumbnails" => "doclib"
));

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "$alfrescoUrl/alfresco/service/api/upload?alf_ticket=$ticket");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$result = json_decode(curl_exec($ch), true);
echo var_dump($result);

CODE 2:

Note that dropzone.css is located on same folder as the php file which received the upload.

$data = "
  filedata=dropzone.css
  &destination=workspace://SpacesStore/$myNodeRef
  &uploadDirectory=p
  &description=
  &contenttype=cm:content
  &overwrite=true
  &thumbnails=doclib
"
;

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "$alfrescoUrl/alfresco/service/api/upload?alf_ticket=$ticket");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$result = json_decode(curl_exec($ch), true);
echo var_dump($result);
1 Solution

Accepted Solutions
mehe
Senior Member II

Re: Help me convert Postman to PHP CURL

Jump to solution

have you seen this old thread:  

View solution in original post

6 Replies
mehe
Senior Member II

Re: Help me convert Postman to PHP CURL

Jump to solution

I don't use PHP anymore, but when using curl in bash scripts, I have to use a '@' as prefix for the filepath to let curl know that filedata is a filename and not the content of the file.

Have you tried (for example) filedata=@dropzone.css or "filedata"=>"@" . $_FILES... ?

coisox
Active Member

Re: Help me convert Postman to PHP CURL

Jump to solution

I've tried these two as your suggestion but fail:
$data = http_build_query(array('filedata' => "@"."dropzone.css", ...));

$data = array('filedata' => "@"."dropzone.css", ...);

Got this error:
An error inside the HTTP server which prevented it from fulfilling the request

mehe
Senior Member II

Re: Help me convert Postman to PHP CURL

Jump to solution

have you seen this old thread:  

View solution in original post

coisox
Active Member

Re: Help me convert Postman to PHP CURL

Jump to solution

Yes. i have look at it. His $_FILES is same as me but his approach seems totally different. He include "Alfresco/Service/xxx" library which I didn't. I successfully login and create new folder without "Alfresco/Service/xxx". I'm trying to do this:
File upload | Alfresco Documentation 
Which didn't suggest me to use "Alfresco/Service/xxx"

coisox
Active Member

Re: Help me convert Postman to PHP CURL

Jump to solution

I don't know whats happening when all replies gone missing (Can I even delete replied message?). Anyway, I found the solution after being lead by your URL suggestion. I thank you for all your help Martin Ehe

Here the final code:

$cfile = curl_file_create(
  $_FILES["file"]["tmp_name"],
  $_FILES["file"]["type"],
  $_FILES["file"]["name"]
);

$data = array(
  "filedata" => $cfile,
  "destination" => "workspace://SpacesStore/$myNodeRef",
  "uploadDirectory" => "subfolder",
  "description" => "",
  "contenttype" => "cm:content",
  "overwrite" => "true",
  "thumbnails" => "doclib"
);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "$myAlfrescoUrl/alfresco/service/api/upload?alf_ticket=$myTicket");
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: multipart/form-data"));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$result = json_decode(curl_exec($ch), true);
curl_close($ch);

if($result["nodeRef"]) {
  echo "\n\nUpload success. NodeRef: ".$result["nodeRef"];
}
else {
  echo "\n\nUpload error: ".$result["message"];
}
ni0ni0s
Member II

Re: Help me convert Postman to PHP CURL

Jump to solution

could you pls send the whole code for this?

thanks