Library for scraping free proxies lists written in PHP
Proxy-scraper library is built on top of HTTPlug and requires a compatible HTTP client. Available clients are listed on Packagist: https://packagist.org/providers/php-http/client-implementation. To use the library you have to install any of them, e.g.:
composer require php-http/guzzle6-adapter
Then install proxy-scraper library itself:
composer require vantoozz/proxy-scraper
<?php declare(strict_types = 1);
use GuzzleHttp\Client as GuzzleClient;
use Vantoozz\ProxyScraper\HttpClient\GuzzleHttpClient;
use Vantoozz\ProxyScraper\Scrapers;
require_once __DIR__ . '/vendor/autoload.php';
$httpClient = new GuzzleHttpClient(new GuzzleClient([
'connect_timeout' => 2,
'timeout' => 3,
]));
$scraper = new Scrapers\SpysMeScraper($httpClient);
foreach ($scraper->get() as $proxy) {
echo (string)$proxy . "\n";
}
You can easily get data from many scrapers at once:
<?php declare(strict_types = 1);
use GuzzleHttp\Client as GuzzleClient;
use Vantoozz\ProxyScraper\HttpClient\GuzzleHttpClient;
use Vantoozz\ProxyScraper\Scrapers;
require_once __DIR__ . '/vendor/autoload.php';
$httpClient = new GuzzleHttpClient(new GuzzleClient([
'connect_timeout' => 3,
'timeout' => 4,
]));
$compositeScraper = new Scrapers\CompositeScraper;
$compositeScraper->addScraper(new Scrapers\FreeProxyListScraper($httpClient));
$compositeScraper->addScraper(new Scrapers\MultiproxyScraper($httpClient));
$compositeScraper->addScraper(new Scrapers\SocksProxyScraper($httpClient));
$compositeScraper->addScraper(new Scrapers\SpysMeScraper($httpClient));
foreach ($compositeScraper->get() as $proxy) {
echo (string)$proxy . "\n";
}
Sometimes things go wrong. This example shows how to handle errors while getting data from many scrapers:
<?php declare(strict_types = 1);
use Vantoozz\ProxyScraper\Exceptions\ScraperException;
use Vantoozz\ProxyScraper\Ipv4;
use Vantoozz\ProxyScraper\Port;
use Vantoozz\ProxyScraper\Proxy;
use Vantoozz\ProxyScraper\Scrapers;
require_once __DIR__ . '/vendor/autoload.php';
$compositeScraper = new Scrapers\CompositeScraper;
// Set exception handler
$compositeScraper->handleScraperExceptionWith(function (ScraperException $e) {
echo 'An error occurs: ' . $e->getMessage() . "\n";
});
// Throws an exception
$compositeScraper->addScraper(new class implements Scrapers\ScraperInterface
{
public function get(): \Generator
{
throw new ScraperException('some error');
}
});
// No exceptions
$compositeScraper->addScraper(new class implements Scrapers\ScraperInterface
{
public function get(): \Generator
{
yield new Proxy(new Ipv4('192.168.0.1'), new Port(8888));
}
});
//Run scraper
foreach ($compositeScraper->get() as $proxy) {
echo (string)$proxy . "\n";
}
Will output
An error occurs: some error
192.168.0.1:8888
Validation steps may be added:
<?php declare(strict_types = 1);
use Vantoozz\ProxyScraper\Exceptions\ValidationException;
use Vantoozz\ProxyScraper\Ipv4;
use Vantoozz\ProxyScraper\Port;
use Vantoozz\ProxyScraper\Proxy;
use Vantoozz\ProxyScraper\Scrapers;
use Vantoozz\ProxyScraper\Validators;
require_once __DIR__ . '/vendor/autoload.php';
$scraper = new class implements Scrapers\ScraperInterface
{
public function get(): \Generator
{
yield new Proxy(new Ipv4('104.202.117.106'), new Port(1234));
yield new Proxy(new Ipv4('192.168.0.1'), new Port(8888));
}
};
$validator = new Validators\ValidatorPipeline;
$validator->addStep(new Validators\Ipv4RangeValidator);
foreach ($scraper->get() as $proxy) {
try {
$validator->validate($proxy);
echo '[OK] ' . (string)$proxy . "\n";
} catch (ValidationException $e) {
echo '[Error] ' . $e->getMessage() . ': ' . (string)$proxy . "\n";
}
}
Will output
[OK] 104.202.117.106:1234
[Error] IPv4 is in private range: 192.168.0.1:8888
A Proxy object may have metrics (metadata) associated with.
By default Proxy object has source metric:
<?php declare(strict_types = 1);
use GuzzleHttp\Client as GuzzleClient;
use Vantoozz\ProxyScraper\HttpClient\GuzzleHttpClient;
use Vantoozz\ProxyScraper\Scrapers;
require_once __DIR__ . '/vendor/autoload.php';
$httpClient = new GuzzleHttpClient(new GuzzleClient([
'connect_timeout' => 2,
'timeout' => 3,
]));
$scraper = new Scrapers\SpysMeScraper($httpClient);
/** @var \Vantoozz\ProxyScraper\Proxy $proxy */
$proxy = $scraper->get()->current();
foreach ($proxy->getMetrics() as $metric) {
echo $metric->getName() . ': ' . $metric->getValue() . "\n";
}
Will output
source: Vantoozz\ProxyScraper\Scrapers\SpysMeScraper
Note. Examples use Guzzle as HTTP client.
./vendor/bin/phpunit --testsuite=unit
./vendor/bin/phpunit --testsuite=integration
php ./tests/systemTests.php