bordercloud/sparql

PHP 库,非常易于使用 SPARQL 1.1

2.1.0 2021-05-14 15:51 UTC

This package is auto-updated.

Last update: 2024-08-29 03:23:46 UTC


README

Build Status

SPARQL 1.1 HTTP 客户端库

非常简单的 PHP SparqlClient。

感谢 贡献者

安装

本项目假设您已安装 composer。只需通过 Composer 添加新的依赖项

composer require bordercloud/sparql

到您的 composer.json 中,然后您可以简单地安装

composer install

使用 PHP 脚本测试库:查询

您可以通过命令行测试您的第一个 sparql 查询,通过 DBPEDIA

./bin/query -r -e http://dbpedia.org/sparql -f ./example/queryReadDBpedia.rq

此脚本与 virtuoso、4store、Allegrograph、Fuseki 和 Sesame 的文档

USAGE : query [-r|-w][-e URL|--endpointQueryAndUpdate=URL]
		[--file=FILE|-f FILE]
        [-v|-verbose]

    -r                                  READ ONLY
    -w                                  WRITE ONLY
    -e, --endpointQueryAndUpdate=URL    Put url of endpoint to do query or
                                        update :
                                            URL/sparql/?query=...
                                            URL/update/?update=... (POST)
    -q, --endpointQueryOnly=URL         Put url of endpoint to do query :
                                            URL?query=...
    -u, --endpointUpdateOnly=URL        Put url of endpoint to do query :
                                            URL?update=... (POST)
    --nameParameterQuery=PARAMETER      Change the name of parameter in
                                        the request http to read.
                                        (by default : query)
    --nameParameterUpdate=PARAMETER     Change the name of parameter in
                                        the request http to write.
                                        (by default : update)
    -f,--file=File                      File of the query.
    -t, --typeOutput=TYPE               Type of response: table,txt,csv,tsv,ttl,srx,srj
                                        (by default : table)

    -l, --login=LOGIN                  Server login
    -p, --password=PASSWORD            Server password

    -v, --verbose                       Mode verbose
    -d, --debug                         Mode debug

EXAMPLE : Virtuoso
./query -w -e https:///tests/ -f ./example/queryWrite1.rq

./query -r -e https:///tests/ -f ./example/queryRead1.rq

EXAMPLE : 4Store
./query -w -e https:/// -f ./example/queryWrite1.rq

./query -r -e https:/// -f ./example/queryRead1.rq

EXAMPLE : Sesame
./query -w -q https:///openrdf-sesame/repositories/tests \
 -u https:///openrdf-sesame/repositories/tests/statements \
-f ./example/queryWrite1.rq

./query -r -q https:///openrdf-sesame/repositories/tests \
 -u https:///openrdf-sesame/repositories/tests/statements \
-f ./example/queryRead1.rq

EXAMPLE : Fuseki
./query -w -q https:///tests/query \
-u https:///tests/update \
-f ./example/queryWrite1.rq

./query -r -q https:///tests/query \
-u https:///tests/update \
-f ./example/queryRead1.rq

EXAMPLE : Allegrograph
./query -w -q https:///repositories/tests \
-u https:///repositories/tests \
--nameParameterUpdate=query \
-f ./example/queryWrite1.rq

./query -r -q https:///repositories/tests \
-u https:///repositories/tests \
--nameParameterUpdate=query \
-f ./example/queryRead1.rq

示例

向 Wikidata 发送简单查询

<?php
use BorderCloud\SPARQL\SparqlClient;

require_once ('../vendor/autoload.php');

$endpoint = "https://query.wikidata.org/sparql";
$sc = new SparqlClient();
$sc->setEndpointRead($endpoint);
//$sc->setMethodHTTPRead("GET");
$q = "select *  where {?x ?y ?z.} LIMIT 5";
$rows = $sc->query($q, 'rows');
$err = $sc->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";
}

向 DBpedia 发送简单查询

<?php
use BorderCloud\SPARQL\SparqlClient;

require_once ('../vendor/autoload.php');

$endpoint = "http://dbpedia.org/sparql";
$sc = new SparqlClient();
$sc->setEndpointRead($endpoint);
$q = "select *  where {?x ?y ?z.} LIMIT 5";
$rows = $sc->query($q, 'rows');
$err = $sc->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";
}

通过 sparql-auth 端点发送简单查询(使用 OpenLink Virtuoso Open-Source Edition)

<?php
use BorderCloud\SPARQL\SparqlClient;

require_once ('../vendor/autoload.php');

$endpoint = "https://example.com/sparql-auth";
$sc = new SparqlClient();
$sc->setEndpointRead($endpoint);
//$sc->setEndpointWrite($endpoint);
$sc->setLogin("login");
$sc->setPassword("password");

$q = "select *  where {?x ?y ?z.} LIMIT 5";
$rows = $sc->query($q, 'rows');
$err = $sc->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";
}

文档

复制源代码和测试

git clone http://github.com/BorderCloud/SPARQL.git
composer install

在执行测试之前,您需要启动数据库实例。例如,Virtuoso 7

systemctl start docker
docker pull bordercloud/tft-virtuoso7-stable
docker run --privileged --name instance.tft_virtuoso7_stable -h tft_virtuoso7_stable -d bordercloud/tft-virtuoso7-stable

执行 PHPUnit

phpunit --configuration phpunit.xml --coverage-text

联系

如果您有任何评论、问题或建议,请通过 karima.rafes@bordercloud.com 发送

发行说明

  • V2.1.0 ** 添加检测 SPARQL 更新查询的工具 ** 在发送查询时添加超时参数

  • V2.0.9 ** 修复:SPARQL 客户端中的错误

  • V2.0.8 ** 修复:存在 SPARQL 服务错误消息时的错误

  • V2.0.7 ** 修复:在 HTTP 标头中插入 User-agent 参数(针对 Wikidata)

  • V2.0.6 ** 修复:解析器和 ASK 查询结果中的错误

  • V2.0.5 ** 兼容性:PHP 7.1 和 psr-4 ** 将类 Endpoint 重命名为 SparqlClient 并简化构造函数。您只能通过设置器设置端点。 ** 重命名多个函数(PHP Lint) ** 更新 PHPDoc ** 添加函数 SparqlClient->getLastErreur():可以直接读取 SPARQL 语法错误,如果存在错误模式(添加 Wikidata 和 Virtuoso 的模式) ** 移动文件并添加测试 + phpunit.xml。目前 SparqlClient 的覆盖率为 82%(使用 Virtuoso 和 Wikidata 进行覆盖) ** 在 GitHub 上启用 Travis

  • V1.2.1.0 为 Wikidata 和其他添加修复

  • V1.1.0.0 版本 SPARQL.Pro lib PHP 由 Karima Rafes 制作 karima.rafes@bordercloud.com

许可协议

SPARQL.Pro lib PHP (c)2019 由 Karima Rafes - BorderCloud 制作

SPARQL.Pro lib PHP 在 Creative Commons Attribution-ShareAlike 4.0 国际许可协议下许可。

您应该已收到此作品的许可证副本。如果没有,请参阅 http://creativecommons.org/licenses/by-sa/4.0/

编译文档

php vendor/clean/phpdoc-md/bin/phpdoc-md

Git...

同时修改 composer.json 中的版本

git pull
git push
git tag -a 2.0.8@dev -m "version dev"
git push --tags