PHP gearing up for semantic web

The idea of “semantic web” is not new; In 2001, Berner’s Lee described it as “an extension of the current web in which information is given well-defined meaning, better enabling computers and people to work in cooperation.” However, in even after six years, web industry has rarely put into practise. One of the reason for the lacking enthusiasm and popularity of “semantic web” with “web industry” is the lack of libraries in languages that commonly power web applications such as PHP, Perl, Ruby. However, last week I came across an interesting open source project, RAP – RDF Api for PHP. RAP provides all the necessary PHP classes to compose, query, manipulate, and serialize RDF Models. Being a zealot of PHP and working on semantic web technology, I wasn’t able to resist my urge to try out this library. Below I briefly describe what I felt about RAP, my initial experiment and also provide a sample code for composing and serializing ontologies.

My initial experiments with RAP went very well. I was able to easily set it up and recreate some of the examples provided on RAP website. However, when I strated to use RAP for creating ontologies, things started to fall apart. RAP provides an ontology model, “OntModel”, to deal with ontologies. RAP documentation introduces OntModel but lacks coding details. After some email exchanges with their developers and some peeking inside the code, I finally managed to get OntModel working. So I think now its time to be an altruist. Below is a sample code for building and serializing ontologies using RAP’s OntModel

define(“RDFAPI_INCLUDE_DIR”, “D:\\phpscripts\\courses\\rdfapi-php\\api\\”); 
include(RDFAPI_INCLUDE_DIR . “RdfAPI.php”);
include(RDFAPI_INCLUDE_DIR . “resModel\\ResModelP.php”);
include(RDFAPI_INCLUDE_DIR . “ontModel\\OntModelP.php”);
include( RDFAPI_INCLUDE_DIR . “vocabulary\\RDF_RES.php”);
 // Initialize ontModel using ModelFactory Class — a good example of using Factory Design Pattern
$ontModel = ModelFactory::getOntModel(MEMMODEL,RDFS_VOCABULARY);

//Create two classes – Department and Courses

$clsDepartment = $ontModel->createOntClass(;
$clsCourses = $ontModel->createOntClass(;

//Create Some Properties and specify domain and Range
// Domain tells to which class this property should be assigned
// Range tells what are acceptable values for this property
$credit = $ontModel->createOntProperty(;);

$statisfy = $ontModel->createOntProperty(;);

$requires = $ontModel->createOntProperty(;);

//Create a sublcass “Geog10”  and Geog410
$geog10 = $ontModel->createOntClass(;);
$geog10->setPropertyValue($credit, new ResLiteral(“3”, “en”));
$clsCourses->addSubClass($geog10);         //Add Geog10 as subclass of “Courses”


$geog410 = $ontModel->createOntClass(;);
$geog410->setPropertyValue($credit, new ResLiteral(“3”, “en”));
$geog410->setPropertyValue($requires, $geog10);


//serialize the ontology – save it in a file with a name test.owl

//echo $ontModel->writeRdfToString();  //use this if you print the ontology





Good work RAP


18 thoughts on “PHP gearing up for semantic web

  1. Some more tips on RAP API
    To set default namespace use
    $ontModel->addNamespace(”, “http://your_default_namespace#”);

    To import other ontologies
    $ontModel->addNamespace(“rdf”, “http://url_to_rdf_syntax”);


    1. Dear sebastian,

      Hope this will help you.


      $ontModel = ModelFactory::getOntModelForBaseModel($model, RDFS_VOCABULARY);

      $array = $ontModel->listClasses();
      foreach($array as $resource){

      echo “SuperClass: ” . $resource->getURI() . “”;
      $ontClass = $ontModel->createOntClass($resource->getURI());
      $arrayClass = $ontClass->listSubClasses(true);

      foreach ($arrayClass as $subclass) {
      echo $subclass->getLabel() . “”;


      1. Hi Imran

        i tried with load() but show me the following error

        Call to undefined function load()

        any suggest?

  2. hi Sebastian,

    I did a quick search of the api, but didn’t find any method to read an existing owl file. But I am sure there should be one as this is one of the basic thing anyone would like to do. Try posting a question on RAP’s sourceforge forum.


  3. Hi there thanks for the tutorial!
    Do you know how to create instances as well?
    I tried to add:
    to the code, but then the instance doesnt show up in the resulting serialization.. shall I add it as a statement? if so what predicate I’m supposed to use?
    thanks a lot!

  4. Hallo Ragrawal,
    I need your help. I am implementing an example of an e-commerce
    website for my final project. Keep in mind that I am a PHP rookie.
    I read the ontology example you posted above (I did not quite undertsand it).

    My project theory goes like this:
    I am using RDF to represent content in a website that
    is going to be used by people who want to buy second class
    laptops from people who will be selling.

    The plan is to store details of sellers of buyers in a MySQL
    database, use RAP PHP API to represent and access the content
    in the normal way i.e. pages accessed in a normal web browser
    making the RDF technology used transparent to the user.

    Will I need an ontology (like the you explained above) for my project?
    How do I construct the site using RAP-RDF API?

  5. Hi,

    I am using rap to create a semantic application. My problem is that i create an ontology and I want to save it in a rdf file. I am using the saveAs function as above but the file is not created. Is there a solution that i can use to save the file?

  6. Hi all ..
    i’m looking for a complete example that using rap api to build semantic web application and using inference engine..
    i’m new to semantic web so can i find such example written in php source code ..
    and thanx advance

  7. Hi,

    I have built a user interface which collects data on users using PHP. I need to model user profiles using RAP API based on:

    1. Date and time
    2. Location (room number or proximity to somewhere)
    3. Nearby devices settings

    These 3 items will collectively be tagged as activities. Kindly could you please help me how to achieve this?

    Many thanks!

  8. hi
    i am doing a project on publishing linked data. i am new to semantic web. i need to import foaf ontology, using RAPPHP. how can i import ontology. need some sample codes.
    thanx in advance

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s