PHPackages                             drewlabs/http-query - 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. [HTTP &amp; Networking](/categories/http)
4. /
5. drewlabs/http-query

ActiveLibrary[HTTP &amp; Networking](/categories/http)

drewlabs/http-query
===================

HTTP Api query client sdk

v0.3.16(1y ago)035MITPHPPHP &gt;=7.2

Since Feb 11Pushed 1y agoCompare

[ Source](https://github.com/azlabsphp/http-query)[ Packagist](https://packagist.org/packages/drewlabs/http-query)[ RSS](/packages/drewlabs-http-query/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (5)Versions (7)Used By (0)

HTTP Query
==========

[](#http-query)

This library provides a client side implementation of HTTP query (similar to graphql) that allows developpers to easily customize data requested from web resources (that support the server side language implementation) and directly sending complex query parameters.

Usage
-----

[](#usage)

### Query builder

[](#query-builder)

The library comes with a query builder class that provides a fluent interface for building and compiling queries. An example is as follow:

- Creating a query object

```
use Drewlabs\Query\Http\Query;

// Instruct the query object to use `http://127.0.0.1:8080` as base domain
$b = Query::new('http://127.0.0.1:8080')

            // Select the api path (path to the resource being queried)
            ->from('api/v1/examples')

            // Add a Bearer Authorization header to the request
            ->withAuthorization(BEARER_TOKEN);
```

As seen above, the API comes with numerous fluent methods for defining query intention:

- eq

The `eq` method allow developpers to build a `COLUMN=VALUE` like query:

```
$b = $b->eq('title', 'Lorem Ipsum');
```

- neq

The `neq` is the inverse of the `eq` query method:

```
$b = $b->neq('title', 'Lorem Ipsum');
```

- lte / lt

The `lte` and `lt` respectively allow developper to construct a query that check if a column value is less than (less than or equal to) a given value.

```
$b = $b->lt('title', 'Lorem Ipsum')
       ->lte('title', 'Lorem Ipsum');
```

- gte / gt

The `gte` and `gt` respectively allow developper to construct a query that check if a column value is greater than (greater than or equal to) a given value.

```
$b = $b->gt('title', 'Lorem Ipsum')
       ->gte('title', 'Lorem Ipsum');
```

- in

The `in` clause search for value that exists in a list of provided values:

```
$b = $b->in('rates', [3, 3.5, 9]);
```

- exists

The `exists` clause allow to query for relationship existance in the database.

```
$b = $b->exists('comments')
        ->exists('comments', new SubQuery('where', ['likes', '>', 1000]));
```

- sort

The `sort` clause allow developpers to apply a sort by `column` on the result set:

**Note** The sort order is defined by an integer value. Any value greater than `0` get converted to `ASC` while any value less than `0` is converted to `DESC`

```
$b = $b->sort('created_at', -1); // ['sort' => ['by' => 'created_at', 'order' => 'DESC']]
```

- select

The `select` clause allows developpers to specify the list of columns to select from the query server:

```
$b = $b->eq('title', 'Lorem Ipsum')->select('*', 'comments');
```

**Note** As shown in the examples above, `query` object provides a fluent `api` for building complex queries using PHP function calls.

### Executing composed query

[](#executing-composed-query)

After building query using the fluent API, developpers can send query requests to backend servers using the `execute` method:

```
use Drewlabs\Query\Http\Query;

$b = Query::new('http://127.0.0.1:8080')
            ->from('api/v1/examples')
            ->withAuthorization(BEARER_TOKEN);

$b->date('created_at', '>', '2023-12-01')
    ->date('created_at', '
