Source for file SparqlClient.php

Documentation is available at SparqlClient.php

  1. <?php
  2.  
  3. // ----------------------------------------------------------------------------------
  4. // Class: SparqlEngine
  5. // ----------------------------------------------------------------------------------
  6.  
  7. /**
  8. * Client for querying a sparql server.
  9. *
  10. @version  $Id: fsource_sparql__sparqlSparqlClient.php.html 443 2007-06-01 16:25:38Z cax $
  11. @author   Tobias Gauß <tobias.gauss@web.de>
  12. *
  13. @package sparql
  14. */
  15.  
  16. class SparqlClient extends Object {
  17.  
  18.     var $server;
  19.     var $output;    
  20.  
  21.     /**
  22.     * Constructor of SparlClient.
  23.     *
  24.     * @param String  $server   server address.
  25.     */
  26.     function SparqlClient($server){
  27.         $this->server = $server;
  28.         $this->output = "array";
  29.     }
  30.  
  31.  
  32.     /**
  33.     * Sets the output format for a SELECT or ASK query. Possible formats are "xml" for
  34.     * Sparql Query Results XML Format (http://www.w3.org/TR/rdf-sparql-XMLres/) or array
  35.     * for the format described in our SparqlEngine.
  36.     *
  37.     * @param String  $format   the format.
  38.     */
  39.     function setOutputFormat($format){
  40.         if(strtolower($format)=="xml")
  41.         $this->output = "xml";
  42.         if(strtolower($format)=="array")
  43.         $this->output = "array";
  44.  
  45.     }
  46.  
  47.     /**
  48.     * Main function of SparqlClient.
  49.     *
  50.     * @param ClientQuery  $query   the ClientQuery object.
  51.     * @return mixed returns an array that contains the variables an their bindings or a MemModel
  52.     *
  53.     */
  54.     function query($query){
  55.         if(!is_a($query,"ClientQuery"))
  56.         die;//ErrorHandling
  57.  
  58.         $url    $this->_buildurl($query);
  59.         $result $this->_http_get($url);
  60.         return $this->returnResult($result);
  61.  
  62.     }
  63.  
  64.  
  65.     /**
  66.     * Helper function that builds the url.
  67.     *
  68.     * @param ClientQuery  $query   the ClientQuery Object.
  69.     */
  70.  
  71.     function _buildurl($query){
  72.         $url "";
  73.         $url $this->server."?query=".urlencode($query->query);
  74.         foreach($query->default as $defaultg){
  75.             $url $url."&default-graph-uri=".$defaultg;
  76.         }
  77.         foreach($query->named as $namedg){
  78.             $url $url."&named-graph-uri=".$namedg;
  79.         }
  80.  
  81.         return $url;
  82.  
  83.     }
  84.  
  85.     /**
  86.     * Returns the query result.
  87.     *
  88.     * @param String  $result  the result.
  89.     * @return mixed 
  90.     */
  91.     function returnResult($result){
  92.  
  93.         if(strpos($result,"<rdf:RDF")){
  94.             include_once(RDFAPI_INCLUDE_DIR.PACKAGE_SYNTAX_RDF);
  95.             $parser new RdfParser();
  96.             return $parser->generateModel(substr($result,strpos($result,"<rdf:RDF")));
  97.         }
  98.  
  99.         if($this->output == "xml"){
  100.             $pos strpos($result,"<?xml");
  101.             return substr($result,$pos);
  102.         }
  103.         if($this->output == "array"){
  104.             //    $pos = strpos($buffer,"<?xml");
  105.             return $this->parseResult($result);
  106.         }
  107.         return $result;
  108.     }
  109.  
  110.  
  111.     function parseResult($buffer){
  112.         include_once(RDFAPI_INCLUDE_DIR.PACKAGE_SYNTAX_SPARQLRES);
  113.         $parser new SparqlResultParser();
  114.         return $parser->parse($buffer);
  115.     }
  116.  
  117.  
  118.     /**
  119.     * Executes the GET Request.
  120.     *
  121.     * @param String  $url  the url.
  122.     * @return String result.
  123.     */
  124.     function _http_get($url)
  125.     {
  126.         $url parse_url($url);
  127.         $port = isset($url['port']$url['port'80;
  128.  
  129.         $fp fsockopen($url['host']$port);
  130.  
  131.         $replace $url['path'];
  132.  
  133.         fputs($fp"GET ".$replace."?".$url['query']." HTTP/1.0\n");
  134.         fputs($fp"Host:"$url['host']." \r\n");
  135.         fputs($fp"Accept: application/sparql-results+xml, application/rdf+xml\r\n");
  136.         fputs($fp"Connection: close\n\n");
  137.  
  138.         $buffer "";
  139.         while ($tmp fread($fp1024))
  140.         {
  141.             $buffer .= $tmp;
  142.         }
  143.  
  144.         $pos1 strpos($buffer,"\r\n\r\n");
  145.         $pos2 strpos($buffer,"\n\n");
  146.         if ($pos1 === false{
  147.             $pos $pos2;
  148.         else {
  149.             $pos $pos1;
  150.         }
  151.             
  152.         return substr($buffer,$pos);
  153.  
  154.     }
  155. }
  156.  
  157.  
  158.  
  159. ?>

Documentation generated on Fri, 1 Jun 2007 16:51:59 +0200 by phpDocumentor 1.3.2