Recommended:

  • phpclasses.org
  • jsclasses.org
  • jsmag.com
  • siteapps.com
  • View our reviews on Hot Scripts
  • JS Tutorial
  • scripts.com
  • securesignup.com




Recent Comments

Powered by Disqus




Back to articles

Implementing RDF and Sparql using ARC solution

There aren't many good solutions for RDF and Sparql implementation using PHP. ARC by Semsol is one of the best I've seen for now.

So what can you do with it? First thing you can add many RDF structures into one database and then browse them using SPARQL query language. Then you can create SPARQL endpoint and allow others to browse your RDF database remotely.

So to create your own relational database for RDF and then use SPARQL to browse it, you'll need this piece of code:
<?php

//include needed files
include_once("./arc/ARC2.php");

//create configuration array
$config = array(
  /* db */
  'db_host' => 'localhost',
  'db_name' => 'dbname',
  'db_user' => 'user',
  'db_pwd' => 'password',
  /* store */
  'store_name' => 'arc_tests',
  /* stop after 100 errors */
  'max_errors' => 100,
   'sem_html_formats' => 'rdf',
);

//create instance with your configuration
@ $store = ARC2::getStore($config);
if (!$store->isSetUp()) {
  $store->setUp();
}

//here is how you add new RDFs to database
//just change url of rdf instead of http://domain.com/rdfname.rdf
//uncomment if you want to add new RDF resource
//@ $store->query('LOAD <http://domain.com/rdfname.rdf>');


//or query what you already have in database

//create query
$q = "SELECT * WHERE {?a ?b ?c}";
    
    //execute query
    if ($rows = @$store->query($q, 'rows')) 
    {
        //output results
        foreach ($rows as $row)
        {
            $r .= '<li>' . $row['name'] . '</li>';
        }
    }
    else
    {
        //or output errors
        $errs = $store->getErrors();
        if($errs = $store->getErrors())
        {
            foreach($errs as $err)
            {
                echo "<p>".$err."</p>";
            }
        }
    }
?>

And to create SPARQL endpoint you can use this piece of code:

<?php
//include needed files
include_once('./arc/ARC2.php');

//mysql and endpoint configuration
$config = array(
  /* db */
  'db_host' => 'localhost',
  'db_name' => 'dbname',
  'db_user' => 'user',
  'db_pwd' => 'password',

  /* store name */
  'store_name' => 'my_endpoint_store',

  /* endpoint */
  //possible features 'select', 'construct', 'ask', 'describe', 'load', 
  //'insert', 'delete', 'dump'
  //but you should allow only one that won't modify your database
  'endpoint_features' => array(
    'select', 'construct', 'ask', 'describe'
  ),
  'endpoint_timeout' => 60, 
  'endpoint_max_limit' => 250
);

//create instance
@ $ep = ARC2::getStoreEndpoint($config);

if (!$ep->isSetUp()) {
  $ep->setUp();
}

//handle http requests automatically
$ep->go();
?>

You can use Sparql Query Builder Class to create SPARQL Queries


You may also be interested in:

Powered by BlogAlike.com

blog comments powered by Disqus