cancel
Showing results for 
Search instead for 
Did you mean: 

access alfresco through my php site

cristianfrog
Champ in-the-making
Champ in-the-making
Hi all,

I need to communicate with alfresco through a PHP client, in my case is a web site. My goal is to access the repository, get an HTML file and load it in my website, so basically I will use Alfresco as admin panel backend where I create my html pages.

I know there is some material about this, I read it and test but didn't make it working.

https://forums.alfresco.com/forum/general/non-technical-alfresco-discussion/possible-ways-build-enga...
jpotts here writes some solutions, I tried the ifresco php library and the php-alf-cmis-api from here:
https://code.google.com/p/php-alf-cmis-api/

But they doesn't work with Alfresco 5, giving me php errors about connection. So I read that the best solution may be using CMIS PHP Client but didn't found an example about it… is the cmis client this one:

http://chemistry.apache.org/php/phpclient.html
???

How can I connect with this and download/get a file and his properties?
<strong>Can someone provide me a working example in php about this? </strong>
13 REPLIES 13

cristianfrog
Champ in-the-making
Champ in-the-making
I tried the apache chemistry php client and I successfully connected to the cmis.alfresco.com with this command:
php -f cmis_ls.php http://cmis.alfresco.com/service/api/cmis admin admin /

But it doesn't seem to work with my alfresco 5.0.b, why? the new alfresco doesn't allow connection through cmis anymore? how I can read the files then??

P.S. for my alfresco 5.0.b I tried all the urls but nothing working Smiley Sad
[domain]/alfresco/api/-default-/public/cmis/versions/1.1/atom
[domain]/alfresco/api/-default-/public/cmis/versions/1.1/browser
[domain]/alfresco/api/-default-/public/cmis/versions/1.0/atom
[domain]/alfresco/cmisws/cmis?wsdl

blacksylo
Champ in-the-making
Champ in-the-making
Hi cristianfrog, have you tried with http://localhost:8080/alfresco/cmisatom

rjohnson
Star Contributor
Star Contributor
I have used CMIS and CURL to access Alfresco from PHP and it works well. We are using 4.2e.

Dependent upon need, I think that CURL is somewhat simpler to use. It limits you to calling webscripts but as the Alfresco API is largely REST then you can pretty much do anything you need that way and you can certainly retrieve document contents very easily. I do.

Are you familiar with CURL? I have a class I can share with you if you like.

@Blacksylo: I tried that one too but it doesn't work …

@rjohnson: Yes sure! it would be great if you can share with me the class and an example! you can attach it here or send it to me by PM

rjohnson
Star Contributor
Star Contributor
As I can only attach txt files I have renamed all the .php to .txt.

Alfresco.txt is the class file. If you include this and have CURL installed then you can call any of the REST API.

A code snippet showing how you call this for a POST webscript is below


   $progress = $alfrescourl . "/alfresco/service/farthest-gate/fg-lga-progress-ticket?noderef=" . $row["noderef"];
   $payloaddata = array("newstate" => "full", "nextstate" =>"$nextstate","nextstatedate" =>"$nextstagedate","message" => "Full rate uplift");
   $payloaddataJSON = json_encode($payloaddata);
   if($noupdate == false) {
      $alfresco = new Alfresco($postUIDPWD, ALFRESCOURL."/share/page/dologin");
      $data = $alfresco->get();
      $headers = $alfresco->getHeaders();   
      if(!$alfresco->runpostwebscript($progress, $headers["JSESSIONID"], $getUIDPWD, $payloaddataJSON)) {
         die("Failed to progress ticket discount to full in Alfresco. Code: " . $alfresco->httpstatuscode . " Message: " . $alfresco->errorMessage . "\n");      
      }
      if($alfresco->httpstatuscode != 200) {
         die("Failed to progress ticket discount to full. Code: " . $alfresco->httpstatuscode . "\n");      
      }      
      echo "d2f: Progressed " . $row["ticketserialnumber"] . " with noderef " . $row["noderef"] . "\n";


$alfrescourl is a variable that holds the http://www……… that gets you to Alfresco

$postUIDPWD = sprintf("username=%s&password=%s",$repo_username, $repo_password);


For a Get


$alfresco = new Alfresco($postUIDPWD, $alfrescourl."/share/page/dologin");
$data = $alfresco->get();
$headers = $alfresco->getHeaders();
print $getMetaData = $alfrescourl . "/alfresco/service/api/metadata?shortQNames=true&nodeRef=" . $nodeRef;
print '<br/>';
if(!$alfresco->rungetwebscript($getMetaData, $headers["JSESSIONID"], $getUIDPWD)) {
die("Failed to get metadata from Alfresco. Code: " . $alfresco->httpstatuscode . " Message: " . $alfresco->errorMessage);
}
if($alfresco->httpstatuscode != 200) {
die("Failed to get metadata from Alfresco. Code: " . $alfresco->httpstatuscode);
}
$metadata = json_decode($alfresco->websrciptreponse);


$getUIDPWD = sprintf("%s:%s",$repo_username, $repo_password);

Hope this helps

I tried the class with the help of your snippet but it gives me errors:

<strong>Warning: array_merge(): Argument #2 is not an array in /web/htdocs/MY SITE/home/cris/alfresco.php on line 62

Fatal error: Uncaught exception 'Exception' with message 'No connection' in /web/htdocs/MY SITE/home/cris/alfresco.php:71 Stack trace: #0 /web/htdocs/MY SITE/home/cris/test.php(15): Alfresco->get() #1 {main} thrown in /web/htdocs/MY SITE/home/cris/alfresco.php on line 71</strong>

Here is my curl settings from phpinfo():

cURL support    enabled
cURL Information    7.19.7
Age    3
Features
AsynchDNS    No
CharConv    No
Debug    No
GSS-Negotiate    Yes
IDN    Yes
IPv6    Yes
krb4    No
Largefile    Yes
libz    Yes
NTLM    Yes
SPNEGO    No
SSL    Yes
SSPI    No
Protocols    tftp, ftp, telnet, dict, ldap, ldaps, http, file, https, ftps, scp, sftp
Host    x86_64-redhat-linux-gnu
SSL Version    NSS/3.16.2.3 Basic ECC
ZLib Version    1.2.3
libSSH Version    libssh2/1.4.2


Here is what my <strong>test.php</strong> contains:

<php>
<?php
include "alfresco.php";

$$alfrescourl = "http://MYALFRESCOURL.com:8080";

$repo_username = "admin";
$repo_password = "password123";
$postUIDPWD = sprintf("username=%s&password=%s",$repo_username, $repo_password);
$nodeRef = "workspace://SpacesStore/a6310747-e2a0-47c6-a571-ac6436f6afd3";

$alfresco = new Alfresco($postUIDPWD, $alfrescourl."/share/page/dologin");
$data = $alfresco->get();
$headers = $alfresco->getHeaders();
print $getMetaData = $alfrescourl . "/alfresco/service/api/metadata?shortQNames=true&nodeRef=" . $nodeRef;
print '<br/>';
if(!$alfresco->rungetwebscript($getMetaData, $headers["JSESSIONID"], $getUIDPWD)) {
die("Failed to get metadata from Alfresco. Code: " . $alfresco->httpstatuscode . " Message: " . $alfresco->errorMessage);
}
if($alfresco->httpstatuscode != 200) {
die("Failed to get metadata from Alfresco. Code: " . $alfresco->httpstatuscode);
}
$metadata = json_decode($alfresco->websrciptreponse);
?>
</php>


Any help?

I don't know if the PHP is cut 'n paste or typed but….


$$alfrescourl = "http://MYALFRESCOURL.com:8080";


is wrong as you are indirectly addressing a variable using a variable because of the $$ notation. You need


$alfrescourl = "http://MYALFRESCOURL.com:8080";


This fits with the "No connection" as the content of $alfrescourl will not be "http://MYALFRESCOURL.com:8080" because of the $$ notation.

No the code is right I've just typed it wrong, the code is:

<php>
<?php
include "alfresco.php";

$alfrescourl = "http://doc.gruppofrog.it:8080";

$repo_username = "admin";
$repo_password = "2155753";
$postUIDPWD = sprintf("username=%s&password=%s",$repo_username, $repo_password);
$nodeRef = "workspace://SpacesStore/a6310747-e2a0-47c6-a571-ac6436f6afd3";

$alfresco = new Alfresco($postUIDPWD, $alfrescourl."/share/page/dologin");
$data = $alfresco->get();
$headers = $alfresco->getHeaders();
print $getMetaData = $alfrescourl . "/alfresco/service/api/metadata?shortQNames=true&nodeRef=" . $nodeRef;
print '<br/>';
if(!$alfresco->rungetwebscript($getMetaData, $headers["JSESSIONID"], $getUIDPWD)) {
die("Failed to get metadata from Alfresco. Code: " . $alfresco->httpstatuscode . " Message: " . $alfresco->errorMessage);
}
if($alfresco->httpstatuscode != 200) {
die("Failed to get metadata from Alfresco. Code: " . $alfresco->httpstatuscode);
}
$metadata = json_decode($alfresco->websrciptreponse);
?>
</php>

And giving the same error:

Warning: array_merge(): Argument #2 is not an array in /web/htdocs/MY SITE/home/cris/alfresco.php on line 62

Fatal error: Uncaught exception 'Exception' with message 'No connection' in /web/htdocs/MY SITE/home/cris/alfresco.php:71 Stack trace: #0 /web/htdocs/MY SITE/home/cris/test.php(12): Alfresco->get() #1 {main} thrown in /web/htdocs/MY SITE/home/cris/alfresco.php on line 71

My guess is that the login is not working. do a print_r on $headers and see what in it. Sounds like its returned an error from the login call not an array.