PHP EasyRDF:无法从构造查询中获取图形

时间:2018-09-07 11:05:53

标签: sparql virtuoso fuseki easyrdf

我正在尝试从通过EasyRdf_Graph发送的查询中获取EasyRdf_Sparql_Client::query对象。

EasyRDF doc says

  

SELECTASK查询将返回EasyRdfSparqlResult类型的对象。

     

CONSTRUCTDESCRIBE查询将返回EasyRdf_Graph类型的对象。

这是我的代码:

<?php
require APPPATH .'third_party/vendor/autoload.php';        
$endpointUrl = "http://data.bnf.fr/sparql";        
$dummyConstructQueryString = "construct { ?s ?q ?r } where { ?s ?p ?o . ?o ?q ?r } limit 1";        
$endPoint = new EasyRdf_Sparql_Client($endpointUrl);
$result = $endPoint->query($dummyConstructQueryString);                
var_dump($result);
echo $result;
?>

这是输出,如您所见,它是EasyRdf_Sparql_Result

object(**EasyRdf_Sparql_Result**)#47 (6) {
  ["type":"EasyRdf_Sparql_Result":private]=>
  string(8) "bindings"
  ["boolean":"EasyRdf_Sparql_Result":private]=>
  NULL
  ["ordered":"EasyRdf_Sparql_Result":private]=>
  NULL
  ["distinct":"EasyRdf_Sparql_Result":private]=>
  NULL
  ["fields":"EasyRdf_Sparql_Result":private]=>
  array(3) {
    [0]=>
    string(1) "s"
    [1]=>
    string(1) "p"
    [2]=>
    string(1) "o"
  }
  ["storage":"ArrayIterator":private]=>
  array(1) {
    [0]=>
    object(stdClass)#48 (3) {
      ["s"]=>
      object(EasyRdf_Resource)#49 (2) {
        ["uri":protected]=>
        string(56) "http://www.w3.org/ns/sparql-service-description#endpoint"
        ["graph":protected]=>
        NULL
      }
      ["p"]=>
      object(EasyRdf_Resource)#50 (2) {
        ["uri":protected]=>
        string(47) "http://www.w3.org/1999/02/22-rdf-syntax-ns#type"
        ["graph":protected]=>
        NULL
      }
      ["o"]=>
      object(EasyRdf_Resource)#51 (2) {
        ["uri":protected]=>
        string(42) "http://www.w3.org/2000/01/rdf-schema#Class"
        ["graph":protected]=>
        NULL
      }
    }
  }
}
+-------------+----------+------------+
| ?s          | ?p       | ?o         |
+-------------+----------+------------+
| sd:endpoint | rdf:type | rdfs:Class |

还尝试了不同的终结点(http://dbpedia.org/sparql http://localhost:3030/testFuseki ),因为我看到了这个问题:https://github.com/njh/easyrdf/issues/226,但这是相同的。

有关安装的信息

  • 与Composer一起安装的EasyRDF 0.9.1版

  • PHP版本7.0.30-0 + deb9u1 + Apache / 2.4.25(Debian)

任何线索将不胜感激,在此先感谢您。


编辑

实际上它与我在本地安装的Fuseki一起使用。由于http://data.bnf/fr/sparqlhttp://dbpedia.org/sparql都是Virtuoso端点,所以我想知道问题是否仅在于Virtuoso。

2 个答案:

答案 0 :(得分:1)

EasyRDF发出并张贴在您的答案中的HTTP请求标头揭示了该问题(所有其他详细信息实际上都应编辑到您的问题中)。

EasyRDF请求标头-

Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8

-表示EasyRDF同样乐意接收text/htmlapplication/xhtml+xml,然后更喜欢application/xml,后跟*/*或“任何媒体类型”。 / p>

实际上并非如此! text/html不是EasyRDF想要的,并且肯定不像application/xhtml+xml那样好,在这种情况下也不比application/xml好。

也就是说,Virtuoso完全按照EasyRDF的要求进行操作,并提供text/html ...,这并不奇怪EasyRDF不会将其视为图形。

从Fuseki(它是application/n-quads,这不是EasyRDF要求的任何特定MIME类型)中得到的回馈,您都很幸运

EasyRDF 应该正在请求某些RDF MIME类型(例如text/turtleapplication/rdf+xmlapplication/trigapplication/n-triples,{{ 1}},application/n-quadsapplication/ld+json ... EasyRDF并非实际上支持所有这些,因此,不应简单地将此列表放入该sparql-results+xml标头中)。 EasyRDF还应指出,这些(可能具有内部优先顺序)优于任何非RDF MIME类型(包括EasyRDF当前请求的所有特定MIME类型)或通配符(Accept:)。

这应该是reported as a bug in EasyRDF。随时在您的报告中标记我(@TallTed on github)。

答案 1 :(得分:0)

感谢Ted关于标题和更新EasyRDF的建议,在阅读了有关EasyRDF问题#226的更多内容后,我发现a solution。问题在于,它需要重新编写php代码才能使用新的命名空间等。如果有人知道0.9.1版本的解决方案,那将非常有帮助!

似乎返回的对象类型(EasyRdf_Graph / EasyRdf_Sparql_Result)取决于标题的Accept参数。我试图通过Firebug对其进行修改(添加了Virtuoso doc中列出的不同内容类型)并重新发送了请求,但Virtuoso仍未获得任何EasyRdf_Graph对象。


正如Ted所问,这是请求标头的相关部分(带有上述php代码和EasyRDF 0.0.1):

Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: fr,fr-FR;q=0.8,en-US;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
Connection: keep-alive

为了获得端点响应头,我添加了这一行:

print_r(get_headers($endpointUrl));

以下是Fuseki(给出EasyRdf_Graph对象)的响应标头:

[0] => HTTP/1.1 200 OK
[1] => Date: Tue, 11 Sep 2018 06:14:56 GMT
[2] => Fuseki-Request-ID: 13
[3] => Vary: Accept,Accept-Encoding,Accept-Charset
[4] => Content-Type: application/n-quads
[5] => Vary: User-Agent

对于维塔士(http://data.bnf.fr

[0] => HTTP/1.1 302 Found
[1] => Date: Tue, 11 Sep 2018 06:12:29 GMT
[2] => Server: Apache
[3] => Location: http://data.bnf.fr/sparql/
[4] => Content-Length: 210
[5] => Content-Type: text/html; charset=iso-8859-1
[6] => Vary: Accept-Encoding,User-Agent
[7] => Connection: close
[8] => HTTP/1.1 200 OK
[9] => Date: Tue, 11 Sep 2018 06:12:29 GMT
[10] => Server: Apache
[11] => Last-Modified: Mon, 19 Feb 2018 10:34:21 GMT
[12] => ETag: "200036-3a99-5658e3ca8a540"
[13] => Accept-Ranges: bytes
[14] => Content-Length: 15001
[15] => Vary: Accept-Encoding,User-Agent
[16] => Access-Control-Allow-Origin: *
[17] => Content-Type: text/html
[18] => Content-Language: fr
[19] => Connection: close

:对于data.bnf.fr,代码为302(重定向),但似乎没有什么区别,请参阅dbpedia响应:

[0] => HTTP/1.1 200 OK
[1] => Date: Tue, 11 Sep 2018 07:17:23 GMT
[2] => Content-Type: text/html; charset=UTF-8
[3] => Content-Length: 14172
[4] => Connection: close
[5] => Vary: Accept-Encoding
[6] => Server: Virtuoso/07.20.3229 (Linux) i686-generic-linux-glibc25-64  VDB
[7] => Expires: Tue, 18 Sep 2018 07:17:23 GMT
[8] => Cache-Control: max-age=604800
[9] => Access-Control-Allow-Origin: *
[10] => Access-Control-Allow-Credentials: true
[11] => Access-Control-Allow-Methods: GET, POST, OPTIONS
[12] => Access-Control-Allow-Headers: DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Accept-Encoding
[13] => Accept-Ranges: bytes

所以这是解决方案,其中包含新的php代码和作曲家更新,以获取EasyRDF开发人员主版本:

1)编辑composer.json:

{
    "require": {
        "easyrdf/easyrdf": "dev-master"
    }
}

2)运行

php composer.phar update

3)重写php代码:

require_once './third_party/vendor/autoload.php';
$endpointUrl = "http://data.bnf.fr/sparql";        
$dummyConstructQueryString = "construct { ?s ?q ?r } where { ?s ?p ?o . ?o ?q ?r } limit 1";        
$endPoint = new \EasyRdf\Sparql\Client($endpointUrl); // <-- here use the namespace
$result = $endPoint->query($dummyConstructQueryString);                
var_dump($result);

编辑

最后,我不确定http请求标头参数“ Accept”是否是获取EasyRdf_Graph对象的条件。这是带有更新的Easyrdf代码(“ dev-master”版本)的请求和响应头。也许EasyRDF解析查询和结果以构建EasyRdf_Graph对象。

  • 请求

     Host: localhost
     User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:62.0) Gecko/20100101 Firefox/62.0
     Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
     Accept-Language: fr,fr-FR;q=0.8,en-US;q=0.5,en;q=0.3
     Accept-Encoding: gzip, deflate
     DNT: 1
     Connection: keep-alive
     Upgrade-Insecure-Requests: 1
     Cache-Control: max-age=0
    
  • 来自http://data.bnf.fr/sparql/的响应(通过print_r(get_headers($endpointUrl));获得)

     [0] => HTTP/1.1 302 Found
     [1] => Date: Wed, 12 Sep 2018 06:21:48 GMT
     [2] => Server: Apache
     [3] => Location: http://data.bnf.fr/sparql/
     [4] => Content-Length: 210
     [5] => Content-Type: text/html; charset=iso-8859-1
     [6] => Vary: Accept-Encoding,User-Agent
     [7] => Connection: close
     [8] => HTTP/1.1 200 OK
     [9] => Date: Wed, 12 Sep 2018 06:21:48 GMT
     [10] => Server: Apache
     [11] => Last-Modified: Mon, 19 Feb 2018 10:34:21 GMT
     [12] => ETag: "200036-3a99-5658e3ca8a540"
     [13] => Accept-Ranges: bytes
     [14] => Content-Length: 15001
     [15] => Vary: Accept-Encoding,User-Agent
     [16] => Access-Control-Allow-Origin: *
     [17] => Content-Type: text/html
     [18] => Content-Language: fr
     [19] => Connection: close