### Install PHPOAIPMH with Composer Source: https://github.com/caseyamcl/phpoaipmh/blob/master/README.md Include the PHPOAIPMH library and Guzzle HTTP client in your composer.json file for installation. ```json { "require": { "caseyamcl/phpoaipmh": "^3.0", "guzzlehttp/guzzle": "^7.0" } } ``` -------------------------------- ### Run Project Tests Source: https://github.com/caseyamcl/phpoaipmh/blob/master/CONTRIBUTING.md Execute the project's test suite using PHPUnit. Ensure you have PHPUnit installed and configured. ```bash $ phpunit ``` -------------------------------- ### Setup OAI-PMH Endpoint Client Source: https://github.com/caseyamcl/phpoaipmh/blob/master/README.md Demonstrates two ways to set up an OAI-PMH endpoint client: using the quick 'build' method or manually creating a Client and Endpoint instance. ```php // Quick and easy 'build' method $myEndpoint = \Phpoaipmh\Endpoint::build('http://some.service.com/oai'); // Or, create your own client instance and pass it to `Endpoint::__construct()` $client = new \Phpoaipmh\Client('http://some.service.com/oai'); $myEndpoint = new \Phpoaipmh\Endpoint($client); ``` -------------------------------- ### Install Guzzle Param Middleware Source: https://github.com/caseyamcl/phpoaipmh/blob/master/README.md Install the emarref/guzzle-param-middleware using Composer. This middleware allows for adding arbitrary query parameters to Guzzle requests. ```bash $composer require emarref/guzzle-param-middleware ``` -------------------------------- ### Install Guzzle Retry Middleware Source: https://github.com/caseyamcl/phpoaipmh/blob/master/README.md Install the Guzzle-Retry-Middleware using Composer. This library helps automatically handle OAI-PMH endpoint rate limiting rules. ```bash composer require caseyamcl/guzzle_retry_middleware ``` -------------------------------- ### Get Basic Endpoint Information Source: https://github.com/caseyamcl/phpoaipmh/blob/master/README.md Retrieve the identify information from an OAI-PMH endpoint, which returns a SimpleXMLElement object. Also shows how to list all available metadata formats, returning an iterator of SimpleXMLElement objects. ```php // Result will be a SimpleXMLElement object $result = $myEndpoint->identify(); var_dump($result); // Results will be iterator of SimpleXMLElement objects $results = $myEndpoint->listMetadataFormats(); foreach($results as $item) { var_dump($item); } ``` -------------------------------- ### Get Total Record Count Source: https://github.com/caseyamcl/phpoaipmh/blob/master/README.md Retrieve the total number of records from an endpoint if available. Returns 'unknown' if the count is not provided. ```php $iterator = $myEndpoint->listRecords('someMetaDataFormat'); echo "Total count is " . ($iterator->getTotalRecordCount() ?: 'unknown'); ``` -------------------------------- ### Limit Record Retrieval by Date Range Source: https://github.com/caseyamcl/phpoaipmh/blob/master/README.md Retrieve records within a specific date range by passing DateTimeInterface objects to the listRecords method. Use null for either the start or end date if only one boundary is needed. ```php // Retrieve records from Jan 1, 2018 through October 1, 2018 $recs = $myEndpoint->listRecords('someMetaDataFormat', new DateTime('2018-01-01'), new DateTime('2018-10-01')); foreach($recs as $rec) { var_dump($rec); } ``` -------------------------------- ### Configure Guzzle v6 Client with Param Middleware Source: https://github.com/caseyamcl/phpoaipmh/blob/master/README.md Set up a Guzzle v6 client with the ParamMiddleware to automatically include specified query parameters in all requests. This involves creating a HandlerStack and pushing the middleware onto it. ```php use Emarref\Guzzle\Middleware\ParamMiddleware use GuzzleHttp\Client as GuzzleClient; use GuzzleHttp\HandlerStack; use GuzzleHttp\Middleware; use Psr\Http\Message\RequestInterface; // Setup the the Guzzle stack $stack = HandlerStack()::create(); $stack->push(new ParamMiddleware(['api_key' => 'xyz123'])); // Setup Guzzle client, adapter, and PHP OAI-PMH client $guzzleClient = new GuzzleClient(['handler' => $stack]) $guzzleAdapter = new \Phpoaipmh\HttpAdapter\GuzzleAdapter($guzzleClient) $client = new \Phpoaipmh\Client('http://some.service.com/oai', $guzzleAdapter); ``` -------------------------------- ### Configure Guzzle v6 Client with Retry Middleware Source: https://github.com/caseyamcl/phpoaipmh/blob/master/README.md Set up a Guzzle v6 client with the retry middleware for automatic rate limiting handling. This involves creating a HandlerStack and pushing the retry middleware factory onto it. ```php use GuzzleRetry\GuzzleRetryMiddleware; use GuzzleHttp\Client as GuzzleClient; use GuzzleHttp\HandlerStack; // Setup the the Guzzle client with the retry middleware $stack = HandlerStack::create(); $stack->push(GuzzleRetryMiddleware::factory()); $guzzleClient = new GuzzleClient(['handler' => $stack]); // Setup the Guzzle adpater and PHP OAI-PMH client $guzzleAdapter = new \Phpoaipmh\HttpAdapter\GuzzleAdapter($guzzleClient); $client = new \Phpoaipmh\Client('http://some.service.com/oai', $guzzleAdapter); ``` -------------------------------- ### Customize Guzzle v6 Request Options Source: https://github.com/caseyamcl/phpoaipmh/blob/master/README.md Set default request options like connection and total timeout for Guzzle v6 client. Requires manual Guzzle client construction. ```php use GuzzleHttp\Client as GuzzleClient; use Phpoaipmh\Client; use Phpoaipmh\Endpoint; use Phpoaipmh\HttpAdapter\GuzzleAdapter; $guzzle = new GuzzleAdapter(new GuzzleClient([ 'connect_timeout' => 2.0, 'timeout' => 10.0 ])); $myEndpoint = new Endpoint(new Client('http://some.service.com/oai', $guzzle)); ``` -------------------------------- ### List Record Sets Source: https://github.com/caseyamcl/phpoaipmh/blob/master/README.md Iterate through all available record sets for an OAI-PMH endpoint by calling the listSets() method. ```php foreach ($myEndpoint->listSets() as $set) { var_dump($set); } ``` -------------------------------- ### Retrieve All Records Source: https://github.com/caseyamcl/phpoaipmh/blob/master/README.md Iterate through all records from an OAI-PMH endpoint for a specified metadata format. The iterator handles pagination and OAI-PMH logic automatically. ```php // Recs will be an iterator of SimpleXMLElement objects $recs = $myEndpoint->listRecords('someMetaDataFormat'); // The iterator will continue retrieving items across multiple HTTP requests. // You can keep running this loop through the *entire* collection you // are harvesting. All OAI-PMH and HTTP pagination logic is hidden neatly // behind the iterator API. foreach($recs as $rec) { var_dump($rec); } ``` -------------------------------- ### Customize Guzzle v5 Request Options Source: https://github.com/caseyamcl/phpoaipmh/blob/master/README.md Set default request options for Guzzle v5 by accessing the Guzzle client via the adapter and using setDefaultOption(). ```php use Phpoaipmh\Client, Phpoaipmh\HttpAdapter\GuzzleAdapter; $adapter = new GuzzleAdapter(); $adapter->getGuzzleClient()->setDefaultOption('timeout', 120); $client = new Client('http://some.service.com/oai', $adapter); $myEndpoint = new Endpoint($client); ``` -------------------------------- ### Customize cURL Request Options Source: https://github.com/caseyamcl/phpoaipmh/blob/master/README.md Set cURL request options, such as timeout, by passing an array of key/value pairs to CurlAdapter::setCurlOpts(). ```php use Phpoaipmh\Client, Phpoaipmh\HttpAdapter\CurlAdapter; $adapter = new CurlAdapter(); $adapter->setCurlOpts([CURLOPT_TIMEOUT => 120]); $client = new Client('http://some.service.com/oai', $adapter); $myEndpoint = new Endpoint($client); ``` -------------------------------- ### Send Arbitrary Query Parameters with Guzzle v5 Events Source: https://github.com/caseyamcl/phpoaipmh/blob/master/README.md Use Guzzle v5's event system to add arbitrary query parameters and headers to requests. This involves defining a listener function that modifies the request before it is sent. ```php // Create a function or class to add parameters to a request $addParamsListener = function(\GuzzleHttp\Event\BeforeEvent $event) { $req = $event->getRequest(); $req->getQuery()->add('api_key', 'xyz123'); // You could do other things to the request here, too, like adding a header.. $req->addHeader('Some-Header', 'some-header-value'); }; // Manually create a Guzzle HTTP adapter $guzzleAdapter = new \Phpoaipmh\HttpAdapter\GuzzleAdapter(); $guzzleAdapter->getGuzzleClient()->getEmitter()->on('before', $addParamsListener); $client = new \Phpoaipmh\Client('http://some.service.com/oai', $guzzleAdapter); ``` -------------------------------- ### Send Arbitrary Query Parameters with Phpoaipmh Client Source: https://github.com/caseyamcl/phpoaipmh/blob/master/README.md Send custom HTTP query parameters directly when making requests using the Phpoaipmh Client. This is useful for passing additional data not covered by standard OAI-PMH parameters. ```php $client = new \Phpoaipmh\Client('http://some.service.com/oai'); $client->request('Identify', ['some' => 'extra-param']); ``` -------------------------------- ### Add Retry Subscriber for Guzzle v5 Source: https://github.com/caseyamcl/phpoaipmh/blob/master/README.md Configure Guzzle v5 to use the Retry-Subscriber for handling rate limiting. This involves creating a RetrySubscriber instance and attaching it to the Guzzle client's emitter. ```php // Create a Retry Guzzle Subscriber $retrySubscriber = new \GuzzleHttp\Subscriber\Retry\RetrySubscriber([ 'delay' => function($numRetries, \GuzzleHttp\Event\AbstractTransferEvent $event) { $waitSecs = $event->getResponse()->getHeader('Retry-After') ?: '5'; return ($waitSecs * 1000) + 1000; // wait one second longer than the server said to }, 'filter' => \GuzzleHttp\Subscriber\Retry\RetrySubscriber::createStatusFilter(), ]); // Manually create a Guzzle HTTP adapter $guzzleAdapter = new \Phpoaipmh\HttpAdapter\GuzzleAdapter(); $guzzleAdapter->getGuzzleClient()->getEmitter()->attach($retrySubscriber); $client = new \Phpoaipmh\Client('http://some.service.com/oai', $guzzleAdapter); ``` -------------------------------- ### Set Date Granularity Manually Source: https://github.com/caseyamcl/phpoaipmh/blob/master/README.md Manually set the date granularity for the OAI-PMH endpoint client by passing a Granularity constant to the Endpoint constructor. This overrides automatic detection. ```php use Phpoaipmh\Client, Phpoaipmh\Endpoint, Phpoaipmh\Granularity; $client = new Client('http://some.service.com/oai'); $myEndpoint = new Endpoint($client, Granularity::DATE_AND_TIME); ``` -------------------------------- ### Retrieve Records from a Specific Set Source: https://github.com/caseyamcl/phpoaipmh/blob/master/README.md Retrieve records from a specific set by providing the set name as the fourth argument to the listRecords method. This filters the results to only include records from the specified set. ```php foreach ($myEndpoint->listRecords('someMetadataFormat', null, null, 'someSetName') as $record) { var_dump($record); } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.