Code Monkey home page Code Monkey logo

gov-info's Introduction

GOVINFO: PHP library for accessing api.govinfo.gov

 _______  _______            _________ _        _______  _______ 
(  ____ \(  ___  )|\     /|  \__   __/( (    /|(  ____ \(  ___  )
| (    \/| (   ) || )   ( |     ) (   |  \  ( || (    \/| (   ) |
| |      | |   | || |   | |     | |   |   \ | || (__    | |   | |
| | ____ | |   | |( (   ) )     | |   | (\ \) ||  __)   | |   | |
| | \_  )| |   | | \ \_/ /      | |   | | \   || (      | |   | |
| (___) || (___) |  \   /    ___) (___| )  \  || )      | (___) |
(_______)(_______)   \_/     \_______/|/    )_)|/       (_______)
                                                                 

CircleCI Maintainability Test Coverage Software License

This alpha stage package provides a very simple way to access api.govinfo.gov

See console demo

Install

You can install this package via composer.

composer require cnizzardini/gov-info

Usage

Retrieve an index of all collections available

use GovInfo\Api;
use GovInfo\Collection;

$api = new Api(
    new \GuzzleHttp\Client(), 
    'DEMO_KEY'
);
$collection = new Collection($api);
$result = $collection->index();

After running this code $result will contain a non-truncated version of collections:

Array
(
    [collections] => Array
        [
            [0] => Array
                [
                    [collectionCode] => USCOURTS
                    [collectionName] => United States Courts Opinions
                    [packageCount] => 1134933
                    [granuleCount] => 2525240
                ]
        ...
)

Retrieve all packages in a collection

use GovInfo\Api;
use GovInfo\Collection;
use GovInfo\Requestor\CollectionAbstractRequestor;

$api = new Api(
    new \GuzzleHttp\Client(), 
    'DEMO_KEY'
);
$collection = new Collection($api);
$requestor = new CollectionAbstractRequestor();

$requestor
    ->setStrCollectionCode('BILLS')
    ->setObjStartDate(new DateTime('2019-01-01'));

$result = $collection->item($requestor);

After running this code $result will contain a non-truncated version of packages:

Array
(
    [count] => 202767
    [message] => 
    [nextPage] => https://api.govinfo.gov/collections/BILLS/2018-01-01T00:00:00Z/?offset=100&pageSize=100
    [previousPage] => 
    [packages] => Array
        [
            [0] => Array
                [
                    [packageId] => BILLS-115hr2740rfs
                    [lastModified] => 2018-11-16T00:33:17Z
                    [packageLink] => https://api.govinfo.gov/packages/BILLS-115hr2740rfs/summary
                    [docClass] => hr
                    [title] => Rabbi Michoel Ber Weissmandl Congressional Gold Medal Act of 2017
                ]
        ...

Retrieve a specific package in a collection.

use GovInfo\Api;
use GovInfo\Collection;
use GovInfo\Requestor\CollectionAbstractRequestor;

$api = new Api(
    new \GuzzleHttp\Client(), 
    'DEMO_KEY'
);
$collection = new Collection($api);
$requestor = new CollectionAbstractRequestor();

$requestor
    ->setStrCollectionCode('BILLS')
    ->setObjStartDate(new \DateTime('2018-01-01 12:00:00'))
    ->setObjEndDate(new \DateTime('2018-02-01 12:00:00'))
    ->setStrDocClass('hr')
    ->setStrPackageId('BILLS-115hr4033rfs')
    ->setStrTitle('Geologic Mapping Act');

$result = $collection->item($requestor);

After running this code $result will contain the requested package

Array
(
    [count] => 202767
    [message] => 
    [nextPage] => https://api.govinfo.gov/collections/BILLS/2018-01-01T00:00:00Z/?offset=100&pageSize=100
    [previousPage] => 
    [packages] => Array
        [
            [2] => Array
                [
                    [packageId] => BILLS-115hr4033rfs
                    [lastModified] => 2018-11-16T00:33:00Z
                    [packageLink] => https://api.govinfo.gov/packages/BILLS-115hr4033rfs/summary
                    [docClass] => hr
                    [title] => National Geologic Mapping Act Reauthorization Act
                ]
        ]
)

Retrieve a summary of a package.

use GovInfo\Package;
use GovInfo\Requestor\PackageAbstractRequestor;

$package = new Package($api);
$requestor = new PackageAbstractRequestor();

$result = $package->summary($requestor->setStrPackageId('BILLS-115hr4033rfs'));

After running this code $result will contain a non-truncated version of the package summary

Array
(
    [title] => An Act To reauthorize the National Geologic Mapping Act of 1992.
    [shortTitle] => Array
        [
            [0] => Array
                [
                    [type] => measure
                    [title] => National Geologic Mapping Act Reauthorization Act
                ]

        ]
    ...

Retrieve a package as a specified content-type

use GovInfo\Package;
use GovInfo\Requestor\PackageAbstractRequestor;

$package = new Package($api);
$requestor = new PackageAbstractRequestor();
$requestor
    ->setStrPackageId('BILLS-115hr4033rfs')
    ->setStrContentType('xml');

$result = $package->contentType($requestor);

After running this code $result will be an instance of GuzzleHttp\Psr7\Response

For additional usage examples, please look at src/Console and tests.

Console

There is a minimalist console application that can be used, but its not designed for production use. I built this so I could easily regression test the library against the production API. Each command will prompt you for an API key. To avoid this prompt you can create a local apiKey.txt file with your API key in there.

Example

# displays collections
php console.php collection:index

# display packages for a given collection 
php console.php collection:packages

# writes to csv file
php console.php collection:packages --file

# writes to csv file in a specific folder
php console.php collection:packages --file --path=/home/username/Desktop

# retrieves a package summary as a GuzzleHttp\Psr7\Response instance
php console.php package:summary

# retrieves a package summary and writes to file
php console.php package:summary --file

# writes to file in a specific folder
php console.php collection:packages --file --path=/home/username/Desktop

Testing

vendor/bin/phpunit

License

The MIT License (MIT). Please see License File for more information.

gov-info's People

Contributors

cnizzardini avatar

Stargazers

 avatar  avatar

Watchers

 avatar

gov-info's Issues

PHP Fatal error: require(): Failed opening required '/var/www/html/govinfo/vendor/autoload.php' (include_path='.:/usr/share/php') in /var/www/html/govinfo/console.php on line 4

I like your PHP Script for GovInfo API; I just wish I could get it working. The image demo looks awesome! I appreciate it.

I am needing to download everything from GovInfo; dating back to the furthest 1940 entries before I have to tear into PDF files to get the rest.

What can I do to resolve the below error? I am running latest PHP 7.4.x on Debian 11 Bullseye. Thank you in advance!

Best Regards,

Brandon

root@VMJail:/var/www/html/govinfo# php console.php collection:packages --file
PHP Warning:  require(/var/www/html/govinfo/vendor/autoload.php): failed to open stream: No such file or directory in /var/www/html/govinfo/console.php on line 4
PHP Fatal error:  require(): Failed opening required '/var/www/html/govinfo/vendor/autoload.php' (include_path='.:/usr/share/php') in /var/www/html/govinfo/console.php on line 4
root@VMJail:/var/www/html/govinfo#

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.