无法使用PHP ARC2 SPARQL从Land Registry API数据中获取数据

时间:2019-09-15 20:09:48

标签: php api sparql arc2

我有一段用PHP编写的代码,它使用ARC2库通过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);
?>

这将连接到API端点,但我在浏览器中收到的错误消息是:

查询错误

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

Array
(
    [result] => 
    [query_time] => 1.1920928955078E-6
)

1 个答案:

答案 0 :(得分:0)

查询末尾出现错误,SPARQL端点被截断。

演示:http://linkedwiki.com/query/UK_House_Price_Index_of_a_region

项目Github BorderCloud/SPARQL

中的代码PHP
<?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";
 }
 ?>