Using Alfresco’s PHP API

Alfresco is starting to incorporate more and more PHP into the platform. This is exciting for us at Optaros because we’ve built some great PHP-based solutions for clients and we’ve had a lot of success with the productivity PHP brings to front-end developers. But, the PHP world lacks an enterprise ready, highly-scalable, robust content management repository.

Now hold on! Hear me out. Projects like Drupal and Joomla are great for certain types of requirements. There are many large, extremely high-volume sites built with Drupal. What I’m talking about is something more generic. Something that makes zero assumptions about the presentation. I’m talking about a back-end repository for rich content plus the back-end “library services” typically associated with document management systems such as workflow, versioning, security, extensible data model, etc. Think “Zope” without the ZODB that can run in any servlet container.
Alfresco has had a PHP API for a while but until today I hadn’t had a chance to do anything with it. On the plane home from Boston today I decided to take it for a spin (details at the bottom of this post).

Using PHP with Alfresco

If you want to write PHP applications that leverage content in Alfresco you can do that today with the PHP API. The PHP API leverages Alfresco Web Services. I have not yet confirmed whether you can do everything with the PHP API that you can with the Java-based Alfresco Web Services API but theoretically you should be able to. And, if you want to write your own Alfresco-centric web services you can–you could deploy them in the same Axis container as Alfresco’s and then hit them with PHP.

If you want to manage a web site using Alfresco WCM that uses PHP you can do that today. But when you use the Alfresco Virtualization Server your PHP scripts won’t run because the Virtualization Server is a customized version of Apache Tomcat and it doesn’t know how to run PHP. Alfresco may address that in the near future.

If Alfresco expands the use of PHP within the product, Alfresco may become more than just an ECM suite–it could become a powerful platform for Next Generation Internet applications. Until then, the next time your PHP application needs a loosely-coupled content repository, consider standing up Alfresco and hitting it with the PHP API.

Trying out the Alfresco PHP API

Once you get all of the pieces together it isn’t that hard to run the Alfresco PHP samples. But it’s not extremely well documented so I’ve included some pointers here. My stack is Ubuntu Dapper Drake, MySQL, Alfresco 2.0 Enterprise, PHP 5.2.1.

  1. Download PHP 5 from http://www.php.net. Follow the directions for building, configuring, and making PHP. When you configure php, make sure to include the –enable-soap flag. You’ll also need to uncomment the extension=php_soap.dll line in your php.ini file.
  2. Use Pear to install Pear::SOAP and its dependencies. (UPDATE: Per Lukas’ comment below, this step is not needed.)
  3. The Alfresco PHP API on the community download site is still at version 1.2. The latest and greatest Alfresco PHP API can be found in the source tree under “modules”.
  4. Follow the instructions in modules/php-sdk/source/php/remote/installation.txt to set up the samples. (See below for how my files were set up).
  • Created a directory called /usr/local/lib/alfresco.
  • Created an alias and a Directory entry in httpd.conf that points to /usr/local/lib/alfresco per the installation.txt file.
  • Copied the contents of modules/php-sdk/source/php/remote to /usr/local/lib/alfresco.
  • Added /usr/local/lib/alfresco to my include_path in my php.ini file.

If your Alfresco repository is running and you’ve restarted Apache with the updated httpd.conf and php.ini, you should be good to go. On my system, I can check /var/log/apache/error.log to troubleshoot. If everything is okay, when you point your browser to http://localhost/alfresco/Examples/SimpleBrowse you will see a list of the spaces and content in your Alfresco store. You can navigate the list, open documents, etc.

The other example is the QueryExecuter example. It performs a full-text query and presents a node list of the query results.

Both of these are very simple examples but by looking at the code, the PHP API source under the Alfresco directory, and the Alfresco Web Services SDK, you should be able to see how you can incorporate Alfresco into your PHP-based application.

8 comments

  1. Paul Holmes-Higgin says:

    Great pointer to the PHP features. If you look at the current Community code line, there’s also an AMP that enables PHP scripting and templating *within* the JVM – and you can use it just like the existing FreeMarker and Javascript.

  2. Lukas says:

    I am not sure why you are installing PEAR::SOAP along with the soap extension. Usually you should only need one of the two. PEAR:SOAP is on the way out, since its implemented in userland code, which means its slower than the soap extension.

  3. jpotts says:

    Lukas,

    You are right. The PEAR:SOAP module is not needed. One of the Alfresco readme’s had made it sound like it was required. I just uninstalled it and verified that the Alfresco PHP examples still work. Thanks for pointing this out. I’ll update the post.

  4. pradesh says:

    I don’t like the fact that you created an alias and a Directory entry in httpd.conf.

    Rather use an htaccess file instead.

  5. Imad says:

    Hi,

    Please help, i am getting this error:

    Fatal error: Uncaught SoapFault exception: [WSDL] SOAP-ERROR: Parsing WSDL: Couldn’t load from ‘http://localhost:8080/alfresco/api/AuthenticationService?wsdl’ : failed to load external entity “http://localhost:8080/alfresco/api/AuthenticationService?wsdl” in D:\wamp\www\alfresco\Alfresco\Service\WebService\AlfrescoWebService.php:36 Stack trace: #0 D:\wamp\www\alfresco\Alfresco\Service\WebService\AlfrescoWebService.php(36): SoapClient->SoapClient(‘http://localhos…’, Array) #1 D:\wamp\www\alfresco\Alfresco\Service\WebService\WebServiceFactory.php(29): AlfrescoWebService->__construct(‘http://localhos…’, Array) #2 D:\wamp\www\alfresco\Alfresco\Service\Repository.php(63): WebServiceFactory::getAuthenticationService(‘http://localhos…’) #3 D:\wamp\www\alfresco\auth.php(29): Repository->authenticate(‘admin’, ‘admin99’) #4 {main} thrown in D:\wamp\www\alfresco\Alfresco\Service\WebService\AlfrescoWebService.php on line 36

  6. Imad says:

    Also, this wsdl asks for username and password and it keeps on asking for credentials even if i enter the correct credentials.

Comments are closed.