-
Notifications
You must be signed in to change notification settings - Fork 5
Quick Start
There are two ways to instantiate the communication object.
- Create a new HTTP client. For a list of available HTTP clients, visit the Http Adapters page.
$adapter = new FurryBear\Http\Adapter\Curl();
- Create a new API provider instance and pass the adapter instance to it. For a list of available providers, visit the Providers page.
$provider = new FurryBear\Provider\Source\SunlightCongress($adapter, $apiKey);
- Create an instance for the output strategy. Sunlight Congress API results are in json format. FurryBear provides two strategies to work with json. One returns a result as an array and the other as an object. For more information on output strategies, visit the Output Strategies page.
$output = new FurryBear\Output\Strategy\JsonToArray();
- Create the entry point class instance and register the provider and the output strategy with it.
$fb = new FurryBear\FurryBear();
$fb->registerProvider($provider)
->registerOutput($output);
- Now we are ready to access some of the resources. The various Sunlight Foundation Congress API methods can be accessed via properties of the FurryBear\FurryBear object. For a list of all available resources, visit the Providers page.
The following example demonstrates a request with full text search (query), partial response (fields), pagination (per_page, page), and highlighting (highlight). The parameters are passed using the setParams() method.
$params = array('query' => '"health care" medicine',
'history.enacted' => true,
'fields' => 'official_title,chamber,introduced_on,search',
'highlight' => true,
'per_page' => 25,
'page' => 2);
try {
$fb->bills_search->setParams($params);
var_dump($fb->bills_search->get());
} catch (\Exception $e) {
echo $e->getMessage();
}
Here is another example this time using a chainable interface and more detailed exceptions. For a list of available chainable methods for each provider, visit the Providers page.
try {
var_dump(
$fb->bills_search->fields('official_title', 'chamber', 'introduced_on', 'search')
->search('"health care" medicine')
->filter('history.enacted', true)
->order('introduced_on')
->order('bill_id', 'asc')
->highlight('<strong>', '</strong>', 300)
->page(2, 25)
->explain()
->get()
);
} catch (FurryBear\Exception\NoProviderException $e) {
echo $e->getMessage();
} catch (FurryBear\Exception\NoOutputException $e) {
echo $e->getMessage();
} catch (FurryBear\Exception\NoResultException $e) {
echo $e->getMessage();
} catch (FurryBear\Exception\FileDoesNotExistException $e) {
echo $e->getMessage();
}
The second way to create the communication object via the FurryBearContainer. The latter has set as default a cURL connection to the Sunlight Congress API that outputs the result as an array. Thus, using this way minimizes the initial code. For example:
use FurryBear\FurryBearContainer;
$container = new FurryBearContainer();
$container['apikey'] = 'Your Sunlight Foundation API Key';
$searchCriteria = array('query' => '"health care" medicine');
var_dump($container['furrybear']->bills_search->get($searchCriteria));
To change any of the services, just redefine them using anonymous functions. Let's continue from the previous example:
use FurryBear\Provider\Source\SunlightOpenStates;
$container['provider'] = function() use ($container) {
return new SunlightOpenStates($container['adapter'], $container['apikey']);
};
var_dump($container['furrybear']->legislator_detail->id('DCL000012')->get());
For a sample implementation, see the FurryBearContainer class. To get a list of all registered services with the container:
print_r($container->getServices());
The same rules apply to use the resources for each provider as in the previous section.