PHP code example of drewlabs / http-query

1. Go to this page and download the library: Download drewlabs/http-query library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

drewlabs / http-query example snippets


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);

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

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

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

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

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

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

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

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

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', '<', '2025-02-01')
    ->exists('comments', fn($b) => $b->in('tags', [1, 4]));

// Executing the query
$result = $b->limit($limit)->execute(); // Calling execute runs the query against the HTTP api and return an instance of \Drewlabs\Query\Http\QueryResult object

print_r($result->getBody()); // returns the actual body of the query

print_r($result->first()); // return the first element of the list of items returned by the query