0

I have a piece of code that is written in PHP and uses the ARC2 library to fetch data from an endpoint via SPARQL.

<?php
include 'includes/config.php';
include_once('semsol/ARC2.php'); /* ARC2 static class inclusion */ 

$dbpconfig = array(
  "remote_store_endpoint" => "http://landregistry.data.gov.uk/",
);

$store = ARC2::getRemoteStore($dbpconfig); 

if ($errs = $store->getErrors()) {
  echo "<h1>getRemoteSotre error<h1>" ;
}

$query = 'prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
prefix owl: <http://www.w3.org/2002/07/owl#>
prefix xsd: <http://www.w3.org/2001/XMLSchema#>
prefix sr: <http://data.ordnancesurvey.co.uk/ontology/spatialrelations/>
prefix ukhpi: <http://landregistry.data.gov.uk/def/ukhpi/>
prefix lrppi: <http://landregistry.data.gov.uk/def/ppi/>
prefix skos: <http://www.w3.org/2004/02/skos/core#>
prefix lrcommon: <http://landregistry.data.gov.uk/def/common/>

PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX vcard: <http://www.w3.org/2001/vcard-rdf/3.0#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX app: <http://example.com#>

SELECT DISTINCT
  ?regionName ?date ?ukhpi ?volume
WHERE
{
  values ?newport {<http://landregistry.data.gov.uk/data/ukhpi/region/newport/month/2013-10>}

  ?newport
    ukhpi:refRegion ?region;
    ukhpi:refMonth ?date;
    ukhpi:housePriceIndex ?ukhpi.

  OPTIONAL { 
    ?newport ukhpi:salesVolume ?volume 
  }

  ?region rdfs:label ?regionName .
  FILTER (langMatches( lang(?regionName), "EN") )
';

  $rows = $store->query($query, 'rows'); /* execute the query */

  if ($errs = $store->getErrors()) {
     echo "Query errors" ;
     pr($errs);
  }

$rows = $store->query($query); /* execute the query */
pr($rows);
?>

This connects to the API endpoint OK, but the error message I get in the browser is:

Query errors

Array
(
    [0] => Incomplete or invalid Group Graph pattern. Could not handle "   values ?newport {

Array
(
    [result] => 
    [query_time] => 1.1920928955078E-6
)
Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
  • 1
    **1.)** are you sure that `http://landregistry.data.gov.uk/` is the correct SPARQL endpoint URL? For me it is just a web page but not the SPARQL service. The correct SPARQL service URL is `http://landregistry.data.gov.uk/landregistry/query` **2.)** Your query is missing a closing curly brace `}`. – UninformedUser Sep 16 '19 at 03:11
  • Thank you for the reply, I have chnaged the endpoint URL and added a closing curely brace, but the error is still the same. – Pete Naylor Sep 16 '19 at 06:34
  • 1
    I don't know ARC2, but I guess the query is parsed before sending it to the remote endpoint. If that is the case, `VALUES` keyword isn't supported as no full SPARQL 1.1 i supported. See https://github.com/semsol/arc2/issues/57 - the good thing, some people extended the parser see the Github forks referred to in the Github issue https://github.com/semsol/arc2/issues/57#issuecomment-295079401 – UninformedUser Sep 16 '19 at 08:31

1 Answers1

0

There is an error at the end of your query and the SPARQL endpoint is truncated.

Demo : http://linkedwiki.com/query/UK_House_Price_Index_of_a_region

The code PHP with the project Github BorderCloud/SPARQL

<?php
require __DIR__ . '/../vendor/autoload.php';
use BorderCloud\SPARQL\SparqlClient;

$endpoint ="http://landregistry.data.gov.uk/landregistry/query";
$sp_readonly = new SparqlClient();
$sp_readonly->setEndpointRead($endpoint);
$q = <<<EOD
prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
prefix owl: <http://www.w3.org/2002/07/owl#>
prefix xsd: <http://www.w3.org/2001/XMLSchema#>
prefix sr: <http://data.ordnancesurvey.co.uk/ontology/spatialrelations/>
prefix ukhpi: <http://landregistry.data.gov.uk/def/ukhpi/>
prefix lrppi: <http://landregistry.data.gov.uk/def/ppi/>
prefix skos: <http://www.w3.org/2004/02/skos/core#>
prefix lrcommon: <http://landregistry.data.gov.uk/def/common/>

PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX vcard: <http://www.w3.org/2001/vcard-rdf/3.0#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX app: <http://example.com#>

SELECT DISTINCT
  ?regionName ?date ?ukhpi ?volume
WHERE
{
  values ?newport {<http://landregistry.data.gov.uk/data/ukhpi/region/newport/month/2013-10>}

  ?newport
    ukhpi:refRegion ?region;
    ukhpi:refMonth ?date;
    ukhpi:housePriceIndex ?ukhpi.

  OPTIONAL { 
    ?newport ukhpi:salesVolume ?volume 
  }

  ?region rdfs:label ?regionName .
  FILTER (langMatches( lang(?regionName), "en") )
  }
LIMIT 10
EOD;
$rows = $sp_readonly->query($q, 'rows');
$err = $sp_readonly->getErrors();
if ($err) {
      print_r($err);
      throw new Exception(print_r($err,true));
}

foreach($rows["result"]["variables"] as $variable){
        printf("%-20.20s",$variable);
        echo '|';
 }
 echo "\n";

foreach ($rows["result"]["rows"] as $row){
        foreach($rows["result"]["variables"] as $variable){
                printf("%-20.20s",$row[$variable]);
        echo '|';
        }
        echo "\n";
 }
 ?>
Karima Rafes
  • 1,016
  • 10
  • 19