Git Product home page Git Product logo

sparql's Introduction

Build Status

Lib Sparql 1.1 HTTP Client

Very simple SparqlClient for PHP.

Thanks to contributors.

Installation

This project assumes you have composer installed. Simply add new dependency via Composer:

composer require bordercloud/sparql

To your composer.json, and then you can simply install with:

composer install

Test the lib with a php script : query

You can test your first query sparql with DBPEDIA via a command line :

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

And the doc of this script with virtuoso, 4store, Allegrograph, Fuseki and 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 http://localhost/tests/ -f ./example/queryWrite1.rq

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

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

./query -r -e http://localhost/ -f ./example/queryRead1.rq

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

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

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

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

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

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

Examples

Send a simple query to 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";
}

Send a simple query to 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";
}

Send a simple query via an endpoint sparql-auth (with 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";
}

Documentation

Copy Sources and tests

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

Before to execute tests, you need to start database's instances. For example, 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

Execute PHPUnit

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

Contact

If you have remarks, questions, or suggestions, please send them to [email protected]

Release-Notes

  • V2.1.0 ** Add tools to detect SPARQL update queries ** Add the timeout parameter at the send of the query

  • V2.0.9 ** Fix : bugs in SPARQL client

  • V2.0.8 ** Fix : bugs when there are error messages of SPARQL services

  • V2.0.7 ** Fix : Insert the parameter User-agent in the header HTTP (for Wikidata)

  • V2.0.6 ** Fix : bug with the parser and the ASK query's results

  • V2.0.5 ** Compatibility : PHP 7.1 and psr-4 ** Rename the class Endpoint to SparqlClient and simplify the constructor. You can set the endpoints only by their setters. ** Rename several functions (PHP Lint) ** Update PHPDoc ** Add the function SparqlClient->getLastErreur() : can read the SPARQL syntax error directly, if the pattern of error exists (Add the pattern of Wikidata and Virtuoso) ** Move files and add tests + phpunit.xml. SparqlClient is coverage to 82% for the moment (coverage with Virtuoso and Wikidata). ** Enable Travis in GitHub

  • V1.2.1.0 Add fix for Wikidata and other

  • V1.1.0.0 version SPARQL.Pro lib PHP by Karima Rafes [email protected]

license

SPARQL.Pro lib PHP (c)2019 by Karima Rafes - BorderCloud

SPARQL.Pro lib PHP is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.

You should have received a copy of the license along with this work. If not, see http://creativecommons.org/licenses/by-sa/4.0/.

Compile DOC

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

Git...

Modify also the version in composer.json

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

sparql's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

sparql's Issues

Problem with insert data query

I'm trying to insert data into my RDF database using PHP. This is my code:

$query = "PREFIX vc: <http://www.fbk.eu/ontologies/2016/virtualcoach/v1#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>

INSERT DATA {
    vc:AnimalLipid_6666661 vc:amountNutrient 6666.66 .
    vc:AnimalLipid_6666661 vc:unit  \"g\" .
    vc:AnimalLipid_6666661 rdf:type vc:AnimalLipid .
   vc:AnimalLipid_6666661 rdf:type owl:NamedIndividual . 
}";

$sp_write = new Endpoint($end);
$res = $sp_write->query($query);
$err = $sp_write->getErrors();
if ($err) {
                print_r($err);
                throw new Exception(print_r($err,true));
}

The result is an error saying "Sorry, you have no permission to access the database". Why? There is no autentication in my RDF database. My server is graphDB. How can I make it work? Where I'm wrong?

Thanks in advance for your help.

Cache

What do you think of this possible new improvement?

When enable, the cache saves the SPARQL results, if the service SPARQL didn't respond before X seconds, the lib uses the results in the cache.

Parameters in function of SPARQL endpoint and the query

  • maximum waiting time of the SPARQL result
  • size of cache
  • lasp of time before trying again with the SPARQL service

Problem when trying to use "group_concat"

Hi, I got this error when i try to use group_concat or concat or contains...

Parse error:

        prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
        prefix fn: <https://www.w3.org/TR/xpath-functions-3/>
        
        SELECT ?movieLabel ?date ?runtime (group_concat(?actorLabel;separator="|") as ?actorsL) WHERE {
          ?movie rdfs:label ?movieLabel.
          ?actors rdfs:label ?actorLabel.
          ?movie <http://data.linkedmdb.org/resource/movie/actor> ?actors.
          ?movie <http://data.linkedmdb.org/resource/movie/runtime> ?runtime.
          ?movie <http://purl.org/dc/terms/date> ?date.
          ?movie  <http://purl.org/dc/terms/title> "The Shining".
        }
        group by ?movieLabel ?date ?runtime

Encountered "group" at line 5, column 44.
Was expecting one of:
...
<PNAME_NS> ...
<PNAME_LN> ...
...
...

Thanks

Update PHP requirement to ^7.0

The composer.json file shows a PHP requirement of >=5.2, but since you are using features only in PHP 7 (strict types, etc.), you should update the requirement to ^7.0.

Leaving it at >=5.2 will allow users to install it on PHP 5, but then it won’t work.

Write a regular expression to to identify the good protocol

This code is not sufficient to identify without error a query for the protocol "update" or "select".

In SparqlClient.php :

   if (preg_match("/(INSERT|DELETE|CLEAR|LOAD)/i", $q)) {
                    $response = $this->queryUpdate($q);
                } else {
                    $response = $this->queryRead($q);
                }

Somebody can improve this regular expression ?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.