PHP code example of offdev / gpp

1. Go to this page and download the library: Download offdev/gpp 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/ */

    

offdev / gpp example snippets




use GuzzleHttp\Client as GuzzleClient;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response;
use Offdev\Gpp\Client;
use Offdev\Gpp\Http\MiddlewareInterface;
use Offdev\Gpp\Http\ResponseHandlerInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;

class DirectoryLister implements MiddlewareInterface
{
    public function process(
        RequestInterface $originalRequest,
        ResponseInterface $response,
        ResponseHandlerInterface $responseHandler
    ): ResponseInterface {
        $content = (string)$response->getBody();
        if (preg_match_all('/href="\/articles\/\d+\/([^"]+)"/m', $content, $matches, PREG_SET_ORDER)) {
            $result = [];
            foreach ($matches as $match) {
                $result[] = $match[1];
            }
            return new Response(200, [], json_encode($result, JSON_PRETTY_PRINT));
        }

        return $responseHandler->handle($response);
    }
}

$client = new Client(new GuzzleClient(), [DirectoryLister::class]);
$result = $client->send(new Request('GET', 'https://www.worldhunger.org/articles/12/'));
var_dump((string)$result->getBody());



use GuzzleHttp\Psr7\Request;
use Offdev\Gpp\Utils\IntegerEnumerator;

$enumerator = new IntegerEnumerator();
$nextRequest = $enumerator->getNextRequest(
    new Request('GET', 'https://www.worldhunger.org/articles/12/')
);

var_dump((string)$nextRequest->getUri());



use GuzzleHttp\Client as GuzzleClient;
use GuzzleHttp\Psr7\Request;
use Offdev\Gpp\Client;
use Offdev\Gpp\Crawler;
use Offdev\Gpp\Utils\IntegerEnumerator;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;

$client = new Client(new GuzzleClient(['exceptions' => false]));
$crawler = new Crawler($client, new IntegerEnumerator());
$crawler->crawl(
    new Request('GET', 'https://www.worldhunger.org/articles/15/'),
    5, // time between each request, in seconds
    function ( // callback function, to control the crawler workflow
        RequestInterface $originalRequest,
        ResponseInterface $response
    ) {
        echo $response->getStatusCode().' : '.(string)$originalRequest->getUri().PHP_EOL;
        if ($response->getStatusCode() !== 200) {
            return true; // cancel crawling
        }
        // go ahead, wait for the interval, and crawl the next result
        return false;
    }
);

$ php middleware.php
/tmp/gpp-examples/middleware.php:34:
string(300) "[
    "editorials\/",
    "global\/",
    "images\/",
    "us\/",
    "2012_archive.htm",
    "asia.htm",
    "books.htm",
    "davidson.htm---",
    "editorials.htm",
    "editorials2.htm",
    "global.htm",
    "newtemplate.htm",
    "phn.htm",
    "us.htm",
    "vanderslice_hungry_children.htm"
]"

$ php enumerator.php
/tmp/gpp-examples/enumerator.php:11:
string(40) "https://www.worldhunger.org/articles/13/"

$ ./vendor/bin/phpcs --colors --standard=PSR2 -v src/ tests/
Creating file list... DONE (13 files in queue)
Changing into directory /Users/pascal/devel/gpp/src
Processing Crawler.php [PHP => 419 tokens in 71 lines]... DONE in 25ms (0 errors, 0 warnings)
Changing into directory /Users/pascal/devel/gpp/src/Utils
Processing RequestEnumeratorInterface.php [PHP => 201 tokens in 40 lines]... DONE in 16ms (0 errors, 0 warnings)
Processing IntegerEnumerator.php [PHP => 360 tokens in 51 lines]... DONE in 25ms (0 errors, 0 warnings)
Changing into directory /Users/pascal/devel/gpp/src/Http
Processing MiddlewareInterface.php [PHP => 225 tokens in 47 lines]... DONE in 16ms (0 errors, 0 warnings)
Changing into directory /Users/pascal/devel/gpp/src/Http/Exceptions
Processing PatternException.php [PHP => 91 tokens in 21 lines]... DONE in 6ms (0 errors, 0 warnings)
Processing InvalidArgumentException.php [PHP => 91 tokens in 21 lines]... DONE in 6ms (0 errors, 0 warnings)
Changing into directory /Users/pascal/devel/gpp/src/Http
Processing ResponseHandlerInterface.php [PHP => 169 tokens in 37 lines]... DONE in 13ms (0 errors, 0 warnings)
Changing into directory /Users/pascal/devel/gpp/src
Processing Client.php [PHP => 803 tokens in 129 lines]... DONE in 55ms (0 errors, 0 warnings)
Changing into directory /Users/pascal/devel/gpp/tests
Processing PassthroughModifierMiddleware.php [PHP => 395 tokens in 63 lines]... DONE in 26ms (0 errors, 0 warnings)
Changing into directory /Users/pascal/devel/gpp/tests/Utils
Processing IntegerEnumeratorTest.php [PHP => 320 tokens in 50 lines]... DONE in 14ms (0 errors, 0 warnings)
Changing into directory /Users/pascal/devel/gpp/tests
Processing CrawlerTest.php [PHP => 408 tokens in 52 lines]... DONE in 26ms (0 errors, 0 warnings)
Processing DirectResponseMiddleware.php [PHP => 385 tokens in 62 lines]... DONE in 28ms (0 errors, 0 warnings)
Processing ClientTest.php [PHP => 1159 tokens in 121 lines]... DONE in 92ms (0 errors, 0 warnings)