ARC: appmosphere RDF Classes for PHP Developers Benjamin Nowack appmosphere web applications, Kruppstr. 100, 45145 Essen, Germany bnowack@appmosphere.com Abstract. ARC is an open source collection of lightweight PHP scripts optimized for RDF development on hosted Web servers. It currently consists of a non-validating RDF/XML parser, an N-Triples Serializer, and a "Simple Model" class providing common methods for working with resource descriptions. The three main classes are stand-alone, single-file scripts, thus facilitating the bundling with existing PHP-based applications. By partly using arrays instead of objects, ARC offers speed improvements compared to toolkits that follow approaches completely based on PHP objects. 1 Motivation Despite the availability of the feature-rich and mature RAP - RDF API for PHP [1], there was ongoing demand for RDF libraries (especially for an RDF/XML parser) which could be bundled with existing PHP frameworks more easily. RAP is deployed as an integrated API, so that version changes usually require to completely replace a prior installation. ARC1 uses more lightweight, stand-alone classes in order to allow independent use and to facilitate upgrading tasks. 2 RDF Classes ARC currently consists of three basic class files for working with Semantic Web data: An RDF/XML parser, an N-Triples serializer, and a "Simple Model" class which provides common methods for working with resource descriptions. 2.1 ARC RDF/XML Parser The RDF/XML Parser class can be used to parse a passed string, to load and parse data from the local machine, or to load and parse data from the Web. The built-in Web reader does not follow HTTP redirects yet, but it accepts several parameters to e.g. work with proxies, send custom HTTP headers, or limit the number of lines to 1 ARC stands for appmosphere RDF classes. The classes and documentation are available online at http://www.appmosphere.com/en-arc 2 Benjamin Nowack parse. Additionally, the class can be configured to retrieve content-negotiated RDF/XML (via an HTTP Accept header) , or to keep the raw RDF/XML data and/or response headers of the remote server in memory. The code snippet below shows a usage example. /* class inclusion */ include_once("ARC_rdfxml_parser.php"); /* class configuration (optional) */ $args=array( "encoding"=>"auto",/* auto-detect UTF-8 etc */ "proxy_host"=>"192.168.27.1", "proxy_port"=>8080, "user_agent"=>"myParser v1.0" ); /* instantiation */ $parser=new ARC_rdfxml_parser($args); /* parsing a file from the Web */ $url="http://www.example.com/data.rdf"; $result=$parser->parse_web_file($url); if(is_array($result)){ echo count($result)." triples found"; } else{ echo "couldn't parse ".$url.": ".$result; } The parser does not check the parsed code for complete RDF-validity, only XML errors and some very basic RDF/XML syntax [2] errors are detected. The result of the parsing process is either an error message or an array of triples. 2.2 ARC N-Triples Serializer The serializer class generates an N-Triples [3] string from an array of triples. The structure of the passed triple array has to conform to the following ARC triple array structure: Each entry is an associative array with keys s, p, and o: - s (a subject node array) - p (a predicate URI string) - o (an object node array) ARC: appmosphere RDF Classes for PHP Developers 3 A subject node array may have the following keys: - type ("uri" or "bnode") - uri (if type is "uri") - bnode_id (if type is "bnode") An object node array may contain additional key-value pairs: - type ("uri", or "bnode", or "literal") - uri (if type is "uri") - bnode_id (if type is "bnode") - val (if type is "literal") - dt (datatype URI if available and type is "literal") - lang (language code if available and type is "literal") The serializer provides only a small number of configuration options. It is possible to set the linebreak and the space characters as illustrated below. /* class inclusion */ include_once("ARC_ntriples_serializer.php"); /* class configuration (optional) */ $args=array("linebreak"=>"\n", "spacer"=>"\t"); /* instantiation */ $ser=new ARC_ntriples_serializer($args); /* displaying the N-Triples code */ echo $ser->get_ntriples($triples); 2.3 ARC Simple Model ARC's Simple Model class indexes a triple array and offers methods to e.g. return a list of resources, find resources based on a provided identifier, parse RDF lists, or extract property values. A detailed usage example is given in section 3. 3 Use cases By combining the RDF/XML Parser (or an adjusted triple array queried from a basic RDF triple store) with the Simple Model class, it is easily possible to generate HTML views from RDF. Figure 1 shows a resource description summary which could be created by passing two Simple Model instances (one with vocabulary information, and one with data about individuals) to an HTML rendering script. The latter is not included in ARC but the PHP code snippets below Fig. 1 exemplify how data can be 4 Benjamin Nowack pre-processed by ARC in order to retrieve grouped property values and label information. Fig. 1. HTML view generated by combining information of two ARC Simple Models PHP code to instantiate the two ARC Simple Model classes: /* abbreviations */ $rdfs="http://www.w3.org/2000/01/rdf-schema#"; $foaf="http://xmlns.com/foaf/0.1/"; /* vocabulary model */ $v_triples=$parser->parse_web_file($foaf); $model_args=array( "triples"=>$v_triples, "ns_abbrs"=>array($foaf=>"foaf", $rdfs=>"rdfs") ); $v_model=new ARC_simple_model($model_args); /* data model */ $d_url="http://www.example.com/my_foaf.rdf"; $d_triples=$parser->parse_web_file($d_url); $model_args["triples"]=$d_triples; $d_model=new ARC_simple_model($model_args); ARC: appmosphere RDF Classes for PHP Developers 5 PHP code to find a foaf:Person resource in a foaf:PersonalProfileDocument: /* find PPD in data model */ $ppd_qname="foaf:PersonalProfileDocument"; if(!$ppds=$d_model->get_resources($ppd_qname)){ return false; } $ppd=$ppds[0]; /* get person identifier via primaryTopic */ if(!$id=$d_model->rpv2($ppd, "foaf:primaryTopic")){ return false; } $person=$d_model->get_resource($id); PHP code to display labeled property values: /* name value */ $name_val=$d_model->rpv($person, "foaf:name"); if(!$name_val){ /* try alternative naming properties */ } /* mbox values */ $cur_vals=array(); if($cur_props=$person["props"]["foaf:mbox"]){ foreach($cur_props as $cur_prop){ $cur_vals[]=$cur_prop["val"]; } } /* mbox label (use vocabulary model) */ if($terms=$v_model->get_resources("foaf:mbox")){ $term=$terms[0];/* an rdf:Property instance */ $cur_label=$v_model->rpv($term, "rdfs:label"); } /* display */ echo '