Skip to content

Instantly share code, notes, and snippets.

@teimur8
Last active March 13, 2018 12:02
Show Gist options
  • Select an option

  • Save teimur8/d576db1553275321f4b0994f9703a2bc to your computer and use it in GitHub Desktop.

Select an option

Save teimur8/d576db1553275321f4b0994f9703a2bc to your computer and use it in GitHub Desktop.
<?php
$LOGINURL = BASE_URL . "ru-RU/Account/Login";
$agent = "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36";
// begin script
$ch = curl_init();
$cookieString = trim($_REQUEST['cookie']);
// extra headers
$headers[] = "Accept: */*";
$headers[] = "Connection: Keep-Alive";
$headers[] = "Cookie: $cookieString";
var_dump($headers);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
// curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path);
// curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);
// curl_setopt($ch, CURLOPT_HTTPHEADER, array("Cookie: "));
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_REFERER, null);
curl_setopt($ch, CURLOPT_TIMEOUT, 100);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //return the transfer as a string
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// curl_setopt($ch, CURLOPT_URL, BASE_URL);
$proxies = Proxy::all();
foreach ($proxies as $proxy) {
curl_setopt($ch, CURLOPT_PROXY, $proxy);
$content = curl_exec($ch);
$reponseHTTPCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($reponseHTTPCode === 200) {
echo 'Using proxy ' . $proxy . PHP_EOL;
break;
} else {
Proxy::removeFromCache($proxy);
}
}
curl_setopt($ch, CURLOPT_URL, BASE_URL);
$result = curl_exec($ch);
if(!preg_match("/КОРЗИНА/", $result)){
echo 'Устаревшие Cookie';
die;
};
<?php
class Proxy
{
/**
* Ссылка на файл с обновляемым списком бесплатных прокси
*/
const PROXY_FILE_DOWNLOAD_URL = 'https://raw.githubusercontent.com/clarketm/proxy-list/master/proxy-list.txt';
/**
* Папка для загрузки файла прокси
*/
const PROXY_FILE_DOWNLOAD_FOLDER = BASEPATH . 'upload/proxies/';
/**
* Время хранения прокси в файловом кэше
*/
const CACHE_TTL = 60 * 60 * 12;
/**
* Ключ файлового кеша
*/
const CACHE_KEY = 'proxy';
/**
* Страны прокси которых будут использоваться
*/
const PROXY_COUNTRIES = ['KZ', 'RU'];
/**
* Сообщенеие о неудачной попытке скачать файл
*/
const DOWNLOAD_FAILURE_MESSAGE = 'Не удалось скачать прокси файл';
/**
* Кому отправлять email об ошибках
*/
const FAILURE_REPORT_EMAILS = [ '[email protected]' ];
/**
* Выдает список прокси из кеша.
* Если кеш пуст, то скачивает файл с списом прокси и выдает их
*
* @return bool|mixed
*/
public static function all()
{
$proxy = new static;
return FileCache::init()->getOrSet(static::CACHE_KEY, function () use ($proxy) {
return $proxy->getFilteredProxyAfterDownloading();
}, static::CACHE_TTL);
}
/**
* Скачивает и возвращает список прокси
*
* @return array прокси
* @throws Exception
*/
protected function getFilteredProxyAfterDownloading()
{
$path = $this->download();
return $this->getFilteredArrayFromFile($path);
}
/**
* Скачивает файл со списом прокси
*
* @return bool|string
* @throws Exception
*/
protected function download()
{
$downloadedFilePath = static::PROXY_FILE_DOWNLOAD_FOLDER . 'proxies' . uniqid(true) . '.txt';
$fp = fopen($downloadedFilePath, 'w+');
if($fp === false) {
throw new Exception('Could not open: ' . $downloadedFilePath);
}
$ch = curl_init(static::PROXY_FILE_DOWNLOAD_URL);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
curl_exec($ch);
if (curl_errno($ch)) {
$this->reportFailure(static::DOWNLOAD_FAILURE_MESSAGE, curl_error($ch));
}
$statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if($statusCode == 200) {
return $downloadedFilePath;
} else {
$this->reportFailure(static::DOWNLOAD_FAILURE_MESSAGE, $statusCode);
return false;
}
}
/**
* Получаем и фильтруем прокси из файла
*
* @param String $path путь к файлу прокси
* @return array
*/
protected function getFilteredArrayFromFile($path)
{
$lines = file($path);
$lineCount = count($lines);
$proxies = [];
foreach ($lines as $lineNumber => $line) {
if ($lineNumber > 3 && $lineNumber < $lineCount - 2) {
$proxies[] = $line;
}
}
//Filter out proxies by country
$proxies = array_filter($proxies, function($proxy) {
$description = explode(' ', $proxy)[1];
$countryCode = explode('-', $description)[0];
return in_array($countryCode, static::PROXY_COUNTRIES);
});
// Get proxies only, without other data
return array_map(function($proxy) {
return explode(' ', $proxy)[0];
}, $proxies);
}
/**
* Удалем прокси из кэша
*
* @param $proxy
* @return bool
*/
public static function removeFromCache($proxy)
{
$proxies = FileCache::init()->get(static::CACHE_KEY);
if (($key = array_search($proxy, $proxies)) !== false) {
unset($proxies[$key]);
return FileCache::init()->set(static::CACHE_KEY, $proxies, null, static::CACHE_TTL);
}
return false;
}
/**
* Отправляет сообщение об ошибке
*
* @param string $theme тема email
* @param mixed $data данные по ошибке
*/
protected function reportFailure($theme, $data)
{
$body = json_encode($data, true);
sender::sendEmail(static::FAILURE_REPORT_EMAILS, $body, $theme);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment