AttributesValues
type
Description
  • What? A simple guide usable by any PHP developer seeking to exploit SPARQL without hassles. Why? SPARQL is a powerful query language, results serialization format, and an HTTP based data access protocol from the W3C. It provides a mechanism for accessing and integrating data across Deductive Database Systems (colloquially referred to as triple or quad stores in Semantic Web and Linked Data circles) -- database systems (or data spaces) that manage proposition oriented records in 3-tuple (triples) or 4-tuple (quads) form. How? SPARQL queries are actually HTTP payloads (typically). Thus, using a RESTful client-server interaction pattern, you can dispatch calls to a SPARQL compliant data server and receive a payload for local processing e.g. local object binding re. PHP. Steps: From your command line execute: aptitude search '^PHP26', to verify PHP is in place Determine which SPARQL endpoint you want to access e.g. DBpedia or a local Virtuoso instance (typically: http://localhost:8890/sparql). If using Virtuoso, and you want to populate its quad store using SPARQL, assign "SPARQL_SPONGE" privileges to user "SPARQL" (this is basic control, more sophisticated WebID based ACLs are available for controlling SPARQL access). Script: #!/usr/bin/env php "", "should-sponge" => "soft", "query" => $query, "debug" => "on", "timeout" => "", "format" => $format, "save" => "display", "fname" => "" ); $querypart="?"; foreach($params as $name => $value) { $querypart=$querypart . $name . '=' . urlencode($value) . "&"; } $sparqlURL=$baseURL . $querypart; return json_decode(file_get_contents($sparqlURL)); }; # Setting Data Source Name (DSN) $dsn="http://dbpedia.org/resource/DBpedia"; #Virtuoso pragmas for instructing SPARQL engine to perform an HTTP GET #using the IRI in FROM clause as Data Source URL $query="DEFINE get:soft \"replace\" SELECT DISTINCT * FROM <$dsn> WHERE {?s ?p ?o}"; $data=sparqlQuery($query, "http://localhost:8890/sparql/"); print "Retrieved data:\n" . json_encode($data); ?> Output Retrieved data: {"head": {"link":[],"vars":["s","p","o"]}, "results": {"distinct":false,"ordered":true, "bindings":[ {"s": {"type":"uri","value":"http:\/\/dbpedia.org\/resource\/DBpedia"},"p": {"type":"uri","value":"http:\/\/www.w3.org\/1999\/02\/22-rdf-syntax-ns#type"},"o": {"type":"uri","value":"http:\/\/www.w3.org\/2002\/07\/owl#Thing"}}, {"s": {"type":"uri","value":"http:\/\/dbpedia.org\/resource\/DBpedia"},"p": {"type":"uri","value":"http:\/\/www.w3.org\/1999\/02\/22-rdf-syntax-ns#type"},"o": {"type":"uri","value":"http:\/\/dbpedia.org\/ontology\/Work"}}, {"s": {"type":"uri","value":"http:\/\/dbpedia.org\/resource\/DBpedia"},"p": {"type":"uri","value":"http:\/\/www.w3.org\/1999\/02\/22-rdf-syntax-ns#type"},"o": {"type":"uri","value":"http:\/\/dbpedia.org\/class\/yago\/Software106566077"}}, ... Conclusion JSON was chosen over XML (re. output format) since this is about a "no-brainer installation and utilization" guide for a PHP developer that already knows how to use PHP for HTTP based data access. SPARQL just provides an added bonus to URL dexterity (delivered via URI abstraction) with regards to constructing Data Source Names or Addresses. Related SPARQL Guide for the Python Developer SPARQL Guide for the Ruby Developer Simple Guide for using SPARQL with Virtuoso General SPARQL Tutorial Collection Virtuoso Specific SPARQL Tutorial Collection The URI, URL, and Linked Data Meme's Generic HTTP URI.
Creator
  • Kingsley Uyi Idehen
Date
  • 2011-01-20T21:25:49Z
Title
  • SPARQL Guide for the PHP Developer
Link
content:encoded
  • What?

    A simple guide usable by any PHP developer seeking to exploit SPARQL without hassles.

    Why?

    SPARQL is a powerful query language, results serialization format, and an HTTP based data access protocol from the W3C. It provides a mechanism for accessing and integrating data across Deductive Database Systems (colloquially referred to as triple or quad stores in Semantic Web and Linked Data circles) -- database systems (or data spaces) that manage proposition oriented records in 3-tuple (triples) or 4-tuple (quads) form.

    How?

    SPARQL queries are actually HTTP payloads (typically). Thus, using a RESTful client-server interaction pattern, you can dispatch calls to a SPARQL compliant data server and receive a payload for local processing e.g. local object binding re. PHP.

    Steps:

    1. From your command line execute: aptitude search '^PHP26', to verify PHP is in place
    2. Determine which SPARQL endpoint you want to access e.g. DBpedia or a local Virtuoso instance (typically: http://localhost:8890/sparql).
    3. If using Virtuoso, and you want to populate its quad store using SPARQL, assign "SPARQL_SPONGE" privileges to user "SPARQL" (this is basic control, more sophisticated WebID based ACLs are available for controlling SPARQL access).

    Script:

    #!/usr/bin/env php
    <?php
    #
    # Demonstrating use of a single query to populate a # Virtuoso Quad Store via PHP. 
    #
    
    # HTTP URL is constructed accordingly with JSON query results format in mind.
    
    function sparqlQuery($query, $baseURL, $format="application/json")
    
      {
    	$params=array(
    		"default-graph" =>  "",
    		"should-sponge" =>  "soft",
    		"query" =>  $query,
    		"debug" =>  "on",
    		"timeout" =>  "",
    		"format" =>  $format,
    		"save" =>  "display",
    		"fname" =>  ""
    	);
    
    	$querypart="?";	
    	foreach($params as $name => $value) 
      {
    		$querypart=$querypart . $name . '=' . urlencode($value) . "&";
    	}
    	
    	$sparqlURL=$baseURL . $querypart;
    	
    	return json_decode(file_get_contents($sparqlURL));
    };
    
    
    
    # Setting Data Source Name (DSN)
    $dsn="http://dbpedia.org/resource/DBpedia";
    
    #Virtuoso pragmas for instructing SPARQL engine to perform an HTTP GET
    #using the IRI in FROM clause as Data Source URL
    
    $query="DEFINE get:soft \"replace\"
    SELECT DISTINCT * FROM <$dsn> WHERE {?s ?p ?o}"; 
    
    $data=sparqlQuery($query, "http://localhost:8890/sparql/");
    
    print "Retrieved data:\n" . json_encode($data);
    
    ?>
    

    Output

    Retrieved data:
      {"head":
      {"link":[],"vars":["s","p","o"]},
      "results":
    		{"distinct":false,"ordered":true,
    		"bindings":[
    			{"s":
    			{"type":"uri","value":"http:\/\/dbpedia.org\/resource\/DBpedia"},"p":
    			{"type":"uri","value":"http:\/\/www.w3.org\/1999\/02\/22-rdf-syntax-ns#type"},"o":
    			{"type":"uri","value":"http:\/\/www.w3.org\/2002\/07\/owl#Thing"}},
    			{"s":
    			{"type":"uri","value":"http:\/\/dbpedia.org\/resource\/DBpedia"},"p":
    			{"type":"uri","value":"http:\/\/www.w3.org\/1999\/02\/22-rdf-syntax-ns#type"},"o":
    			{"type":"uri","value":"http:\/\/dbpedia.org\/ontology\/Work"}},
    			{"s":
    			{"type":"uri","value":"http:\/\/dbpedia.org\/resource\/DBpedia"},"p":
    			{"type":"uri","value":"http:\/\/www.w3.org\/1999\/02\/22-rdf-syntax-ns#type"},"o":
    			{"type":"uri","value":"http:\/\/dbpedia.org\/class\/yago\/Software106566077"}},
    ...
    

    Conclusion

    JSON was chosen over XML (re. output format) since this is about a "no-brainer installation and utilization" guide for a PHP developer that already knows how to use PHP for HTTP based data access. SPARQL just provides an added bonus to URL dexterity (delivered via URI abstraction) with regards to constructing Data Source Names or Addresses.

    Related

wfw:comment
wfw:commentRss
is rdf:_6 of
Alternative Linked Data Views: Sponger | iSPARQL | ODE     Raw Data in: CXML | CSV | RDF ( N-Triples N3/Turtle JSON XML ) | OData ( Atom JSON )    About   
This material is Open Knowledge   W3C Semantic Web Technology [RDF Data] This material is Open Knowledge Creative Commons License Valid XHTML + RDFa
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 Unported License.
OpenLink Virtuoso version 06.01.3127, on Linux (x86_64-pc-linux-gnu), Standard Edition
Copyright © 2009-2011 OpenLink Software