<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
paslandau / guzzle-rotating-proxy-subscriber example snippets
// define proxies
$proxy1 = new RotatingProxy("username:[email protected]:4711");
$proxy2 = new RotatingProxy("username:[email protected]:4711");
// setup and attach subscriber
$rotator = new ProxyRotator([$proxy1,$proxy2]);
$sub = new RotatingProxySubscriber($rotator);
$client = new Client();
$client->getEmitter()->attach($sub);
// perform the requests
$num = 10;
$url = "http://www.myseosolution.de/scripts/myip.php";
for ($i = 0; $i < $num; $i++) {
$request = $client->createRequest("GET",$url);
try {
$response = $client->send($request);
echo "Success with " . $request->getConfig()->get("proxy") . " on $i. request\n";
} catch (Exception $e) {
echo "Failed with " . $request->getConfig()->get("proxy") . " on $i. request: " . $e->getMessage() . "\n";
}
}
$evaluation = function(RotatingProxyInterface $proxy, AbstractTransferEvent $event){
if($event instanceof CompleteEvent){
$content = $event->getResponse()->getBody();
// example of a custom message returned by a target system
// for a blocked IP
$pattern = "#Sorry! You made too many requests, your IP is blocked#";
if(preg_match($pattern,$content)){
// The current proxy seems to be blocked
// so let's mark it as blocked
$proxy->block();
return false;
}else{
// nothing went wrong, the request was successful
return true;
}
}else{
// We didn't get a CompleteEvent maybe
// due to some connection issues at the proxy
// so let's mark the request as failed
return false;
}
};
$proxy = new RotatingProxy("username:[email protected]:4711", $evaluation);
// or
$proxy->setEvaluationFunction($evaluation);
// function specific to your domain model that performs the evaluation
function domain_specific_evaluation(AbstractTransferEvent $event){
if($event instanceof CompleteEvent){
$content = $event->getResponse()->getBody();
// example of a custom message returned by a target system
// for a blocked IP
$pattern = "#Sorry! You made too many requests, your IP is blocked#";
if(preg_match($pattern,$content)){
// The current proxy seems to be blocked
// so let's mark it as blocked
$event->getRequest()->getConfig()->set(RotatingProxyInterface::GUZZLE_CONFIG_KEY_REQUEST_RESULT, RotatingProxyInterface::GUZZLE_CONFIG_VALUE_REQUEST_RESULT_BLOCKED);
return false;
}else{
// nothing went wrong, the request was successful
$event->getRequest()->getConfig()->set(RotatingProxyInterface::GUZZLE_CONFIG_KEY_REQUEST_RESULT, RotatingProxyInterface::GUZZLE_CONFIG_VALUE_REQUEST_RESULT_SUCCESS);
return true;
}
}else{
// We didn't get a CompleteEvent maybe
// due to some connection issues at the proxy
// so let's mark the request as failed
$event->getRequest()->getConfig()->set(RotatingProxyInterface::GUZZLE_CONFIG_KEY_REQUEST_RESULT, RotatingProxyInterface::GUZZLE_CONFIG_VALUE_REQUEST_RESULT_FAILURE);
return false;
}
}
$evaluation = function(RotatingProxyInterface $proxy, AbstractTransferEvent $event){
$result = $event->getRequest()->getConfig()->get(RotatingProxyInterface::GUZZLE_CONFIG_KEY_REQUEST_RESULT);
switch($result){
case RotatingProxyInterface::GUZZLE_CONFIG_VALUE_REQUEST_RESULT_SUCCESS:{
return true;
}
case RotatingProxyInterface::GUZZLE_CONFIG_VALUE_REQUEST_RESULT_FAILURE:{
return false;
}
case RotatingProxyInterface::GUZZLE_CONFIG_VALUE_REQUEST_RESULT_BLOCKED:{
$proxy->block();
return false;
}
default: throw new RuntimeException("Unknown value '{$result}' for config key ".RotatingProxyInterface::GUZZLE_CONFIG_KEY_REQUEST_RESULT);
}
};
$proxy = new RotatingProxy("username:[email protected]:4711", $evaluation);
// or
$proxy->setEvaluationFunction($evaluation);
$maximumFails = 100;
$consecutiveFails = 5;
$proxy = new RotatingProxy("username:[email protected]:4711", null,$consecutiveFails,$maximumFails);
// or
$proxy->setMaxTotalFails($maximumFails);
$proxy->setMaxConsecutiveFails($consecutiveFails);
$from = 1;
$to = 5;
$wait = new RandomTimeInterval($from,$to);
$proxy = new RotatingProxy("username:[email protected]:4711", null,null,null,$wait);
// or
$proxy->setWaitInterval($wait);
$rotator = new ProxyRotator($proxies);
$waitFn = function (WaitingEvent $event){
$proxy = $event->getProxy();
echo "All proxies have a timeout restriction, the lowest is {$proxy->getWaitingTime()}s!\n";
// nah, we don't wanna wait
$event->skipWaiting();
};
$rotator->getEmitter()->on(ProxyRotator::EVENT_ON_WAIT, $waitFn);
$proxies = [/* ... */];
$useOwnIp = true;
$rotator = new ProxyRotator($proxies,$useOwnIp);
// or
$rotator->setUseOwnIp($useOwnIp);
$infoFn = function (UseOwnIpEvent $event){
echo "No proxies are left, making a direct request!\n";
};
$rotator->getEmitter()->on(ProxyRotator::EVENT_ON_USE_OWN_IP,$infoFn);
$s = "
username:[email protected]:4711
username:[email protected]:4711
username:[email protected]:4711
";
$rotator = Build::rotator()
->failsIfNoProxiesAreLeft() // throw exception if no proxies are left
->withProxiesFromString($s, "\n") // build proxies from a string of proxies
// where each proxy is seperated by a new line
->evaluatesProxyResultsByDefault() // use the default evaluation function
->eachProxyMayFailInfinitlyInTotal() // don't care about total number of fails for a proxy
->eachProxyMayFailConsecutively(5) // but block a proxy if it fails 5 times in a row
->eachProxyNeedsToWaitSecondsBetweenRequests(1, 3) // and let it wait between 1 and 3 seconds before making another request
->build();
$s = "
username:[email protected]:4711
username:[email protected]:4711
username:[email protected]:4711
";
$lines = explode("\n",$s);
$proxies = [];
foreach($lines as $line){
$trimmed = trim($line);
if($trimmed != ""){
$wait = new RandomTimeInterval(1,3);
$proxies[$trimmed] = new RotatingProxy($trimmed,null,5,-1,$wait);
}
}
$rotator = new ProxyRotator($proxies,false);
$userAgent = "Mozilla/5.0 (Windows NT 6.3; WOW64; rv:37.0) Gecko/20100101 Firefox/37.0"; // common user agent string for firefox
$defaultRequestHeaders = ["Accept-Language" => "de,en"]; // add a preferred language to each of our requests
$cookieSession = new CookieJar(); // enable cookies for this identity
$identity = new Identity($userAgent,$defaultRequestHeaders,$cookieSession);
$identities = [$identity];
$proxy1 = new RotatingIdentityProxy($identities, "[PROXY 1]");
$userAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36"; // common user agent string for chrome
$defaultRequestHeaders = ["Accept-Language" => "de"]; // add a preferred language to each of our requests
$cookieSession = null; // disable cookies for this identity
$identity1 = new Identity($userAgent,$defaultRequestHeaders,$cookieSession);
$userAgent = "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)"; // common user agent string for Internet Explorer
$defaultRequestHeaders = ["Pragma" => "no-cache"]; // add a no-cache directive to each request
$cookieSession = new CookieJar(); // enable cookies for this identity
$identity2 = new Identity($userAgent,$defaultRequestHeaders,$cookieSession);
$identities = [$identity1,$identity2];
$systemRandomizer = new SystemRandomizer();
// switch identities randomly after 2 to 5 requests
$minRequests = 2;
$maxRequests = 5;
$counter = new RandomCounterInterval($minRequests,$maxRequests);
$proxy2 = new RotatingIdentityProxy($identities, "[PROXY 2]",$systemRandomizer,$counter);
$s = "
username:[email protected]:4711
username:[email protected]:4711
username:[email protected]:4711
";
$identities = [
new Identity(/*...*/),
new Identity(/*...*/),
new Identity(/*...*/),
new Identity(/*...*/),
new Identity(/*...*/),
/*..*/
];
$rotator = Build::rotator()
->failsIfNoProxiesAreLeft() // throw exception if no proxies are left
->withProxiesFromString($s, "\n") // build proxies from a string of proxies
// where each proxy is seperated by a new line
->evaluatesProxyResultsByDefault() // use the default evaluation function
->eachProxyMayFailInfinitlyInTotal() // don't care about total number of fails for a proxy
->eachProxyMayFailConsecutively(5) // but block a proxy if it fails 5 times in a row
->eachProxyNeedsToWaitSecondsBetweenRequests(1, 3) // and let it wait between 1 and 3 seconds before making another request
// identity options
->distributeIdentitiesAmongProxies($identities) // setup each proxy with a subset of $identities - no identity is assigne twice!
->eachProxySwitchesIdentityAfterRequests(3,7) // switch to another identity after between 3 and 7 requests
->build();
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.