Skip to content

Commit

Permalink
+ curl http client
Browse files Browse the repository at this point in the history
  • Loading branch information
vantoozz committed Aug 3, 2017
1 parent 6a7e9c4 commit 9c5a847
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 7 deletions.
10 changes: 3 additions & 7 deletions examples/05-proxy_checker.php
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
<?php declare(strict_types = 1);

use GuzzleHttp\Client as GuzzleClient;
use Vantoozz\ProxyScraper\Appraisers\ProxyAppraiser;
use Vantoozz\ProxyScraper\HttpClient\GuzzleHttpClient;
use Vantoozz\ProxyScraper\HttpClient\CurlHttpClient;
use Vantoozz\ProxyScraper\Scrapers;

require_once __DIR__ . '/../vendor/autoload.php';

const WHOAMI_URL = 'http://whoami.indielife.ru/';

$httpClient = new GuzzleHttpClient(new GuzzleClient([
'connect_timeout' => 2,
'timeout' => 3,
]));
$httpClient = new CurlHttpClient;

$scraper = new Scrapers\UsProxyScraper($httpClient);
$scraper = new Scrapers\MultiproxyScraper($httpClient);

$proxyAppraiser = new ProxyAppraiser($httpClient, WHOAMI_URL);

Expand Down
36 changes: 36 additions & 0 deletions src/HttpClient/CurlHttpClient.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php declare(strict_types = 1);

namespace Vantoozz\ProxyScraper\HttpClient;

use Vantoozz\ProxyScraper\Exceptions\HttpClientException;

/**
* Class CurlHttpClient
* @package Vantoozz\ProxyScraper\HttpClient
*/
final class CurlHttpClient implements HttpClientInterface
{
/**
* @param string $uri
* @param string|null $proxy
* @return string
* @throws \Vantoozz\ProxyScraper\Exceptions\HttpClientException
*/
public function get(string $uri, string $proxy = null): string
{
$handler = curl_init();
curl_setopt($handler, CURLOPT_URL, $uri);
curl_setopt($handler, CURLOPT_PROXY, $proxy);
curl_setopt($handler, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($handler, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($handler, CURLOPT_HEADER, 0);
curl_setopt($handler, CURLOPT_CONNECTTIMEOUT, 2);
$response = curl_exec($handler);
curl_close($handler);

if (!is_string($response)) {
throw new HttpClientException('Bad response');
}
return $response;
}
}

0 comments on commit 9c5a847

Please sign in to comment.