Php 如何获取SPARQL';s使用sparqllib完成的查询的问答?

Php 如何获取SPARQL';s使用sparqllib完成的查询的问答?,php,sparql,Php,Sparql,对下面代码执行的SPARQL的ASK查询在执行时返回true。同样的结果也是我想要返回到脚本中的结果。如何调整sparqllib的使用 <?php require_once('./sparqllib.php'); $db = sparql_connect('http://dbpedia.org/sparql'); $query = " ASK { ?book a dbpedia-owl:Book . ?book dbppro

对下面代码执行的
SPARQL
ASK
查询在执行时返回
true
。同样的结果也是我想要返回到脚本中的结果。如何调整sparqllib的使用

<?php
    require_once('./sparqllib.php');
    $db = sparql_connect('http://dbpedia.org/sparql');

    $query = "
    ASK {
        ?book a dbpedia-owl:Book .
        ?book dbpprop:author ?author .
        ?book dbpprop:name ?bookname .
        ?author dbpprop:name ?authorname .
        FILTER regex(?bookname, 'A Tale of Two Cities', 'i') .
        FILTER regex(?authorname, 'Dickens', 'i')
    }";

    $result = sparql_query($query);
    var_dump($result);
简短回答(解决方法) sparqllib.php似乎不支持处理
ask
查询的结果,因此您可能只需要使用

select * where {
    ?book a dbpedia-owl:Book .
    ?book dbpprop:author ?author .
    ?book dbpprop:name ?bookname .
    ?author dbpprop:name ?authorname .
    FILTER regex(?bookname, 'A Tale of Two Cities', 'i') .
    FILTER regex(?authorname, 'Dickens', 'i')
}
limit 1
并检查是否得到结果

长答案(sparqllib不支持
ask
results) 除了
select
查询之外,我认为sparqllib不支持其他查询。如果查看,您可以看到查询是如何执行的:

function query( $query, $timeout=null )
{   
    $prefixes = "";
    foreach( $this->ns as $k=>$v )
    {
        $prefixes .= "PREFIX $k: <$v>\n";
    }
    $output = $this->dispatchQuery( $prefixes.$query, $timeout );
    if( $this->errno ) { return; }
    $parser = new xx_xml($output, 'contents');
    if( $parser->error() ) 
    { 
        $this->errno = -1; # to not clash with CURLOPT return; }
        $this->error = $parser->error();
        return;
    }
    return new sparql_result( $this, $parser->rows, $parser->fields );
}
现在,如果您看一下,它定义了结果应该是什么样子,特别是,我们看到ASK查询的结果应该是什么样子

function query( $query, $timeout=null )
{   
    $prefixes = "";
    foreach( $this->ns as $k=>$v )
    {
        $prefixes .= "PREFIX $k: <$v>\n";
    }
    $output = $this->dispatchQuery( $prefixes.$query, $timeout );
    if( $this->errno ) { return; }
    $parser = new xx_xml($output, 'contents');
    if( $parser->error() ) 
    { 
        $this->errno = -1; # to not clash with CURLOPT return; }
        $this->error = $parser->error();
        return;
    }
    return new sparql_result( $this, $parser->rows, $parser->fields );
}
function startXML($parser, $name, $attr)    
{
    if( $name == "sparql" ) { $this->looks_legit = true; }
    if( $name == "result" )
    {
        $this->result = array();
    }
    if( $name == "binding" )
    {
        $this->part = $attr["name"];
    }
    if( $name == "uri" || $name == "bnode" )
    {
        $this->part_type = $name;
        $this->chars = "";
    }
    if( $name == "literal" )
    {
        $this->part_type = "literal";
        if( isset( $attr["datatype"] ) )
        {
            $this->part_datatype = $attr["datatype"];
        }
        if( isset( $attr["xml:lang"] ) )
        {
            $this->part_lang = $attr["xml:lang"];
        }
        $this->chars = "";
    }
    if( $name == "variable" )
    {
        $this->fields[] = $attr["name"];
    }
}
<?xml version="1.0"?>
<sparql xmlns="http://www.w3.org/2005/sparql-results#">
  ...  head ...

  <boolean>true</boolean>

</sparql>
###############################
# Christopher Gutteridge 2010
#  cjg@ecs.soton.ac.uk
#  LGPL License 
#  http://graphite.ecs.soton.ac.uk/sparqllib/
#  https://github.com/cgutteridge/PHP-SPARQL-Lib
###############################