From 4daeede73fd29c179acf500bf301a027218997cf Mon Sep 17 00:00:00 2001 From: Digitaltim | Burhan Date: Mon, 30 Jan 2023 14:41:13 +0100 Subject: [PATCH] url based proxy support Add: Url based Proxy support --- src/Engine/Request.php | 74 ++++++++++++++++++++++-------------------- 1 file changed, 38 insertions(+), 36 deletions(-) diff --git a/src/Engine/Request.php b/src/Engine/Request.php index b6f45ae..ca03f6d 100644 --- a/src/Engine/Request.php +++ b/src/Engine/Request.php @@ -9,7 +9,7 @@ abstract class Request { private $curl; - + private $defaultCurlOpts = [ CURLOPT_RETURNTRANSFER => true, CURLOPT_FOLLOWLOCATION => true, @@ -17,7 +17,7 @@ abstract class Request CURLOPT_SSL_VERIFYPEER => false, CURLINFO_HEADER_OUT => true, ]; - + /** * @param array $proxy * @param string $cookies @@ -28,14 +28,14 @@ abstract class Request * @throws InvalidClassException */ public function makeRequest(array $proxy = [], string $cookies = '', bool $detailed = false, array $curlOpts = [], - bool $multiRequest = false) + bool $multiRequest = false) { // if ($multiRequest) // return self::makeMultiRequest($proxy, $detailed); - + return self::makeSingleRequest($proxy, $cookies, $detailed, $curlOpts); } - + /** * @param array $proxy * @param string $cookies @@ -48,41 +48,43 @@ private function makeSingleRequest(array $proxy = [], string $cookies = '', bool { if (!isset($this->curl)) $this->curl = curl_init(); - + $requestMethod = $this->getRequestMethod(); - + $postOpts = []; - + if ($requestMethod === 'POST') $postOpts = [CURLOPT_POSTFIELDS => http_build_query($this->getFormData())]; - + curl_setopt_array($this->curl, $this->defaultCurlOpts + EngineService::setProxyForSingle($proxy) + $curlOpts + $postOpts +[ CURLOPT_CUSTOMREQUEST => $requestMethod, CURLOPT_HTTPHEADER => self::mergeHeaders($this->getHeaders()), CURLOPT_URL => $this->getUrl(), + CURLOPT_URL => (array_key_exists('url', $proxy)) ? + $proxy['url'].$this->getUrl() : $this->getUrl(), CURLOPT_HEADER => $detailed, CURLOPT_COOKIE => $cookies, ] ); - + return $this->response($detailed ? self::exec() : curl_exec($this->curl), $detailed); } - + /** * @return array */ public function exec(): array { $response = curl_exec($this->curl); - + $requestHeaders = curl_getinfo($this->curl,CURLINFO_HEADER_OUT); $headerSize = curl_getinfo($this->curl,CURLINFO_HEADER_SIZE); $responseHeader = substr($response, 0, $headerSize); - + $code = curl_getinfo($this->curl,CURLINFO_HTTP_CODE) ?: ''; $messageCode = array_key_exists($code, Engine::HTTP_CODES) ? Engine::HTTP_CODES[$code] : ''; - + return [ 'request_headers' => self::headersToArray($requestHeaders), 'response_headers' => self::headersToArray($responseHeader), @@ -97,7 +99,7 @@ public function exec(): array 'response' => substr($response, $headerSize) ]; } - + /** * @param $response * @return array @@ -106,17 +108,17 @@ private function getCookie($response): array { preg_match_all('/^Set-Cookie:\s*([^;]*)/mi', $response, $match_found); - + $cookies = []; - + foreach ($match_found[1] as $item) { parse_str($item, $cookie); $cookies = array_merge($cookies, $cookie); } - + return $cookies; } - + /** * @param string $header * @return array @@ -125,11 +127,11 @@ private function headersToArray(string $header): array { $headers = []; $headersTmpArray = explode("\r\n", $header); - + for ($i = 0 ; $i < count( $headersTmpArray ) ; ++$i) { // we don't care about the two \r\n lines at the end of the headers if (strlen( $headersTmpArray[$i] ) > 0) { - + // the headers start with HTTP status codes, which do not contain a colon, so we can filter them out too if (strpos( $headersTmpArray[$i] , ":")) { $headerName = substr( $headersTmpArray[$i] , 0 , strpos( $headersTmpArray[$i] , ":" ) ); @@ -138,7 +140,7 @@ private function headersToArray(string $header): array } } } - + return $headers; } @@ -146,7 +148,7 @@ private function headersToArray(string $header): array // { // // } - + /** * @param $headers * @return array @@ -154,49 +156,49 @@ private function headersToArray(string $header): array private function mergeHeaders($headers): array { $mergedHeaders = []; - + foreach ($headers as $key => $value) { $mergedHeaders[] = $key . ': ' . $value; } - + return $mergedHeaders; } - + /** * @throws InvalidClassException */ public function response($data, bool $detailed = false, bool $multiRequest = false) { $class = self::getResponseClass(); - + if (!class_exists($class)) throw new InvalidClassException('Call to undefined Response Class'); - + return new $class($data, $detailed, $multiRequest); } - + /** * @return string */ private function getResponseClass(): string { $reversedArr = explode('\\', strrev(get_called_class())); - + $method = strrev($reversedArr[0]); $folders = ''; - + unset($reversedArr[0]); - + foreach ($reversedArr as $folder) { if (strrev($folder) === "Requests") break; - + $folders = strrev($folder) . '\\' . $folders; } - + return Engine::RESPONSE_PREFIX . $folders . $method; } - + /** * Destructor of Request Engine */ @@ -204,4 +206,4 @@ function __destruct() { if ($this->curl) curl_close($this->curl); } -} \ No newline at end of file +}