PHPackages                             sweetrdf/sparql-client - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. sweetrdf/sparql-client

ActiveLibrary

sweetrdf/sparql-client
======================

0.6.3(1y ago)02061MITPHPPHP &gt;=8.0CI passing

Since Mar 29Pushed 7mo ago2 watchersCompare

[ Source](https://github.com/sweetrdf/sparqlClient)[ Packagist](https://packagist.org/packages/sweetrdf/sparql-client)[ Docs](https://github.com/sweetrdf/sparqlClient)[ RSS](/packages/sweetrdf-sparql-client/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (11)Versions (14)Used By (1)

sparqlClient
============

[](#sparqlclient)

[![Latest Stable Version](https://camo.githubusercontent.com/e4a09930658baa206899346b3e49b241a7adc9e71d57869fa3ad09a87e52f843/68747470733a2f2f706f7365722e707567782e6f72672f73776565747264662f73706172716c2d636c69656e742f762f737461626c65)](https://packagist.org/packages/sweetrdf/sparql-client)[![Build status](https://github.com/sweetrdf/sparqlClient/workflows/phpunit/badge.svg?branch=master)](https://github.com/sweetrdf/sparqlClient/workflows/phpunit/badge.svg?branch=master)[![Coverage Status](https://camo.githubusercontent.com/30da3a9a62b43902a2449d90a60e3b958bff75d4a37bf4666ba69f0690fc60f6/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f73776565747264662f73706172716c436c69656e742f62616467652e7376673f6272616e63683d6d6173746572)](https://coveralls.io/github/sweetrdf/sparqlClient?branch=master)[![License](https://camo.githubusercontent.com/8d3fdb14725e3c7545472ce274a8ea3e6662745a45e078b1c19d208956ae6d7b/68747470733a2f2f706f7365722e707567782e6f72672f73776565747264662f73706172716c2d636c69656e742f6c6963656e7365)](https://packagist.org/packages/sweetrdf/sparql-client)

A SPARQL client library for the [rdfInterface](https://github.com/sweetrdf/rdfInterface/) ecosystem with the API inspired by the [PDO](https://www.php.net/manual/en/book.pdo.php).

- It can work with any PSR-17 / PSR-18 compliant HTTP libraries.
- It can work with huge query results.
    The response is parsed in a lazy way as a stream (the next row is parsed only when you try to read it). This assures iterating through results without acumulating them in an array, which has neglectable memory footprint.

Installation
------------

[](#installation)

- Obtain the [Composer](https://getcomposer.org)
- Run `composer require sweetrdf/sparql-client`
- Run `composer require guzzlehttp/guzzle` to install an HTTP client (but you can use any client supporting PSR-18).
- Run `composer require http-interop/http-factory-guzzle` to install PSR-17 bindinds for Guzzle (but you can use any PSR-17 implementation)
- Run `composer require sweetrdf/quick-rdf` to install RDF terms factory (you can use any terms factory compatible with the [rdfInterface](https://github.com/sweetrdf/rdfInterface/)).

Automatically generated documentation
-------------------------------------

[](#automatically-generated-documentation)

It's very incomplete but better than nothing.
[RdfInterface](https://github.com/sweetrdf/rdfInterface/) documentation is included which provides documentation for terms (objects representing RDF named nodes, literals, etc.).

Usage
-----

[](#usage)

```
include 'vendor/autoload.php';
$connection = new \sparqlClient\StandardConnection('https://query.wikidata.org/sparql', new \quickRdf\DataFactory());
$results    = $connection->query('select * where {?a ?b ?c} limit 10');
foreach ($results as $i) {
    print_r($i);
}
```

### Parameterized queries

[](#parameterized-queries)

The `StandardConnection` class provides a [PDO](https://www.php.net/manual/en/book.pdo.php)-like API for parameterized queries (aka prepared statements). Parameterized queries:

- Are the right way to assure all named nodes/blank nodes/literals/quads/etc. in the SPARQL query are properly escaped.
- Protect against [SPARQL injections](https://www.google.com/search?q=sparql+injection).
- Don't provide any speedup (in contrary to SQL parameterized queries).

```
include 'vendor/autoload.php';
$factory    = new \quickRdf\DataFactory();
$connection = new \sparqlClient\StandardConnection('https://query.wikidata.org/sparql', $factory);

// pass parameters using execute()
// the single `?` is a positional parameter while the `:sf` is a named parameter
$query      = $connection->prepare('SELECT * WHERE {?a ? ?c . ?a :sf ?d .} LIMIT 10');
$query->execute([
    $factory::namedNode('http://creativecommons.org/ns#license'),
    'sf' => $factory::namedNode('http://schema.org/softwareVersion'),
]);
foreach ($query as $i) {
    print_r($i);
}

// bind a (positional) parameter to a variable
$query = $connection->prepare('SELECT * WHERE {?a ? ?c .} LIMIT 2');
$value = $factory::namedNode('http://creativecommons.org/ns#license');
$query->bindParam(0, $value);
$query->execute();
foreach ($query as $i) {
    print_r($i);
}
$value = $factory::namedNode('http://schema.org/softwareVersion');
$query->execute();
foreach ($query as $i) {
    print_r($i);
}
```

There are some differences comparing to the PDO API:

- Mixing positional and named parameters in one query is allowed (see the example above).
- Mixing `bindParam()`/`bindValue()` and passing (some or all) parameters trough `execute()` is allowed.
    - The only constraint is that all parameters have to be set.
    - Parameters passed to `execute()` overwrite ones set with `bindParam()`/`bindValue()`.
- As strong typing is used parameter values have to be of type `rdfInterface\Term`.
    - It makes unnecessary specifying the type in `bindParam()`/`bindvalue()`.

### Advanced usage

[](#advanced-usage)

- You may also provide any PSR-18 HTTP client and/or PSR-17 HTTP request factory to the `\sparqlClient\StandardConnection` constructor. E.g. let's assume your SPARQL endpoint requires authorization and you want to benefit from Guzzle connection allowing to set global request options: ```
    $connection = new \sparqlClient\StandardConnection(
        'https://query.wikidata.org/sparql',
        new \quickRdf\DataFactory(),
        new \GuzzleHttp\Client(['auth' => ['login', 'pswd']])
    );
    ```
- If your SPARQL endpoint doesn't follow the de facto standard of accepting the SPARQL query as the `query` request parameter, you may use the `\sparqlClient\Connection` class which takes PSR-7 requests instead of a query string and allows you to use any specific HTTP request you need.

FAQ
---

[](#faq)

- **What about integration of INSERT/UPDATE/DELETE queries with the \\rdfInterface\\Dataset or \\rdfInterface\\QuadIterator?**
    Will be added in the future.

###  Health Score

35

—

LowBetter than 80% of packages

Maintenance53

Moderate activity, may be stable

Popularity11

Limited adoption so far

Community12

Small or concentrated contributor base

Maturity54

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 89.2% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~148 days

Recently: every ~189 days

Total

10

Last Release

531d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/f1f662e3acb95efd9208cdcd2c97437db366044aea705985251760efcbd23070?d=identicon)[zozlak](/maintainers/zozlak)

---

Top Contributors

[![zozlak](https://avatars.githubusercontent.com/u/6503177?v=4)](https://github.com/zozlak "zozlak (33 commits)")[![k00ni](https://avatars.githubusercontent.com/u/381727?v=4)](https://github.com/k00ni "k00ni (4 commits)")

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

![Health badge](/badges/sweetrdf-sparql-client/health.svg)

```
[![Health](https://phpackages.com/badges/sweetrdf-sparql-client/health.svg)](https://phpackages.com/packages/sweetrdf-sparql-client)
```

###  Alternatives

[kreait/firebase-php

Firebase Admin SDK

2.4k39.7M72](/packages/kreait-firebase-php)[aporat/store-receipt-validator

PHP receipt validator for Apple App Store and Amazon Appstore

6503.9M9](/packages/aporat-store-receipt-validator)[drupal/core-recommended

Locked core dependencies; require this project INSTEAD OF drupal/core.

6939.5M343](/packages/drupal-core-recommended)[tempest/framework

The PHP framework that gets out of your way.

2.1k23.1k9](/packages/tempest-framework)[flow-php/flow

PHP ETL - Extract Transform Load - Data processing framework

81733.7k](/packages/flow-php-flow)[theodo-group/llphant

LLPhant is a library to help you build Generative AI applications.

1.5k311.5k5](/packages/theodo-group-llphant)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
