From 8b3417525926aade7fd4138910a9561ebeb9ee17 Mon Sep 17 00:00:00 2001 From: mhmmdq Date: Mon, 11 Oct 2021 07:11:48 +0330 Subject: [PATCH 01/28] add psr-4 autoloading map --- .gitignore | 1 + composer.json | 25 +++++++ src/Server.php | 189 +++++++++++++++++++++++++++++++++++++++++++++++++ test/test.php | 13 ++++ 4 files changed, 228 insertions(+) create mode 100644 .gitignore create mode 100644 composer.json create mode 100644 src/Server.php create mode 100644 test/test.php diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..57872d0 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/vendor/ diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..75ddacd --- /dev/null +++ b/composer.json @@ -0,0 +1,25 @@ +{ + "name": "arashabedii/server", + "description": "php class to work on different web services", + "license": "MIT", + "autoload": { + "psr-4": { + "ArashAbedii\\Server\\": "src/" + } + }, + "authors": [ + { + "name": "mhmmdq", + "email": "mhmmdq@mhmmdq.ir", + "homepage": "https://github.com/mhmmdq", + "role": "Developer" + }, + { + "name": "ArashAbedii", + "email": "arashabedi998@gmail.com", + "homepage": "https://arashabedi.com", + "role": "Developer" + } + ], + "require": {} +} diff --git a/src/Server.php b/src/Server.php new file mode 100644 index 0000000..7763ce3 --- /dev/null +++ b/src/Server.php @@ -0,0 +1,189 @@ +$value){ + $value=str_replace(" ","%20",$value); + $output.=$key."=".$value."&"; + + } + + return "?".trim($output,"&"); //return GET format (?a=1&b=2&c=3) + + } + + + + private static function changeArrayToHeaderFormat($headers){ + + $output=array(); + + foreach($headers as $key=>$value){ + + $output[]="$key: $value"; + + } + + return $output; //return array of headers format (["Referer: https://www.google.com/","Content-type: audio/mpeg"]) + + } + + + + + + //CHECK VALIDATE INPUTS + + private static function validateUrl($url){ + + if(empty($url)){ + + file_put_contents("ErrHandler.log","\nERR MESSAGE: Url required !\t".date("d M Y H:i:s"),FILE_APPEND); + + self::$err=true; + + } + + if(!filter_var($url,FILTER_VALIDATE_URL)){ + + file_put_contents("ErrHandler.log","\nERR MESSAGE: Invalid url format !\t".date("d M Y H:i:s"),FILE_APPEND); + + self::$err=true; + + } + + } + + + + + private static function validHeaders($headers){ + + if(!is_array($headers)){ + + file_put_contents("ErrHandler.log","\nERR MESSAGE: Invalid headers format! headers format should be an array\t".date("d M Y H:i:s"),FILE_APPEND); + + self::$err=true; + + } + + } + + + + private static function validType($reqtype){ + + if(empty($reqtype)){ + + file_put_contents("ErrHandler.log","\nERR MESSAGE: Request type required! put GET or POST or etc type\t".date("d M Y H:i:s"),FILE_APPEND); + + self::$err=true; + + } + + } + + + + + + + +} diff --git a/test/test.php b/test/test.php new file mode 100644 index 0000000..9666032 --- /dev/null +++ b/test/test.php @@ -0,0 +1,13 @@ +1,'b'=>2]; +$headers=['Accept'=>'application/json']; + +echo '
';
+echo Server::sendRequest($url,$params,$type,$headers);
+echo '
'; From 493d8ec821c6c6f79ed5e5b45cb02ecfa5b7e8af Mon Sep 17 00:00:00 2001 From: mhmmdq Date: Mon, 11 Oct 2021 07:13:56 +0330 Subject: [PATCH 02/28] create licence file --- LICENCE | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 LICENCE diff --git a/LICENCE b/LICENCE new file mode 100644 index 0000000..76ef48d --- /dev/null +++ b/LICENCE @@ -0,0 +1,7 @@ +Copyright 2021 ArashAbedii + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. \ No newline at end of file From f034d56de39851ccd6a3a8ad5e0d51d6329f0159 Mon Sep 17 00:00:00 2001 From: mhmmdq Date: Mon, 11 Oct 2021 07:20:28 +0330 Subject: [PATCH 03/28] create logger class file --- src/Logger.php | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 src/Logger.php diff --git a/src/Logger.php b/src/Logger.php new file mode 100644 index 0000000..dff2d38 --- /dev/null +++ b/src/Logger.php @@ -0,0 +1,16 @@ + + * @copyright Copyright (c), 2021 Mhmmdq + * @license MIT public license + */ + +namespace ArashAbedii\Server; + +class Logger{ + + + + +} \ No newline at end of file From 4ebedbab0eda582214e4161f0c2b1b6bb796b0fe Mon Sep 17 00:00:00 2001 From: mhmmdq Date: Mon, 11 Oct 2021 07:50:27 +0330 Subject: [PATCH 04/28] Build the log file storage process --- src/Logger.php | 53 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 52 insertions(+), 1 deletion(-) diff --git a/src/Logger.php b/src/Logger.php index dff2d38..ac648c7 100644 --- a/src/Logger.php +++ b/src/Logger.php @@ -8,8 +8,59 @@ namespace ArashAbedii\Server; -class Logger{ +class Logger { + /** + * We keep the log file storage location in this variable + * + * @var string + */ + protected static $file_path = 'log.txt'; + + /** + * Define the __construct method privately to prevent the class from being constructed as an object + * + */ + private function __construct() + { + + } + + /** + * Reload log file storage + * + * @param string $newPath + * @return void + */ + public static function changeLogPath($newPath) { + + self::$file_path = $newPath; + + } + + /** + * Save the log in the file + * + * @param string $text + * @return void + */ + protected static function saveLog($text) { + + # Using mode a causes a file to be created in the absence of a file + $file = fopen(self::$file_path , 'a'); + + + $date = date("F j, Y, g:i a"); + + # A simple structure to make the log file more beautiful + $string = "----------------- {$date} -----------------\n"; + $string .= "Error Mwssage: {$text}\n"; + $string .= "Timestamp: ".time()."\n"; + $string .= "----------------- {$date} -----------------\n\n\n"; + # Write to file and exit the file + fwrite($file , $string); + fclose($file); + } From d14f78f0e1252de6e8595021e95868daaf92c074 Mon Sep 17 00:00:00 2001 From: mhmmdq Date: Mon, 11 Oct 2021 07:51:16 +0330 Subject: [PATCH 05/28] Log file storage test by logger class --- test/LogTest.php | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 test/LogTest.php diff --git a/test/LogTest.php b/test/LogTest.php new file mode 100644 index 0000000..3f13df8 --- /dev/null +++ b/test/LogTest.php @@ -0,0 +1,18 @@ + Date: Mon, 11 Oct 2021 07:56:14 +0330 Subject: [PATCH 06/28] Log storage by the log class --- src/Server.php | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/src/Server.php b/src/Server.php index 7763ce3..3ea354e 100644 --- a/src/Server.php +++ b/src/Server.php @@ -2,7 +2,7 @@ namespace ArashAbedii\Server; -class Server{ +class Server extends Logger{ private static $err=false; @@ -40,9 +40,11 @@ private static function createRequest($url,$type='get',$params=[],$headers=[],$r curl_setopt($ch,CURLOPT_URL,$url); curl_setopt($ch,CURLOPT_POSTFIELDS,$params); }else{ - file_put_contents("ErrHandler.log","\nERR MESSAGE: Parameter format is incorrect. You should send array or binary format !\t".date("d M Y H:i:s"),FILE_APPEND); + + self::saveLog('Parameter format is incorrect. You should send array or binary format!'); echo "ERR! Check ErrHandler.log file on your project directory\n"; return false; + } if($return_header){ @@ -70,10 +72,12 @@ public static function sendRequestWithoutResponse($url,$type='get',$params=[],$h }elseif(strtolower($type)=='post'){ curl_setopt($ch,CURLOPT_URL,$url); curl_setopt($ch,CURLOPT_POSTFIELDS,$params); - }else{ - file_put_contents("ErrHandler.log","\nERR MESSAGE: Parameter format is incorrect. You should send array or binary format !\t".date("d M Y H:i:s"),FILE_APPEND); + }else { + + self::saveLog('Parameter format is incorrect. You should send array or binary format!'); echo "ERR! Check ErrHandler.log file on your project directory\n"; return false; + } curl_setopt($ch,CURLOPT_CUSTOMREQUEST,$type); @@ -135,7 +139,7 @@ private static function validateUrl($url){ if(empty($url)){ - file_put_contents("ErrHandler.log","\nERR MESSAGE: Url required !\t".date("d M Y H:i:s"),FILE_APPEND); + self::saveLog('Url required!'); self::$err=true; @@ -143,7 +147,7 @@ private static function validateUrl($url){ if(!filter_var($url,FILTER_VALIDATE_URL)){ - file_put_contents("ErrHandler.log","\nERR MESSAGE: Invalid url format !\t".date("d M Y H:i:s"),FILE_APPEND); + self::saveLog('Invalid url format!'); self::$err=true; @@ -158,7 +162,7 @@ private static function validHeaders($headers){ if(!is_array($headers)){ - file_put_contents("ErrHandler.log","\nERR MESSAGE: Invalid headers format! headers format should be an array\t".date("d M Y H:i:s"),FILE_APPEND); + self::saveLog('Invalid headers format! headers format should be an array'); self::$err=true; @@ -172,7 +176,7 @@ private static function validType($reqtype){ if(empty($reqtype)){ - file_put_contents("ErrHandler.log","\nERR MESSAGE: Request type required! put GET or POST or etc type\t".date("d M Y H:i:s"),FILE_APPEND); + self::saveLog('Request type required! put GET or POST or etc type'); self::$err=true; From 423142908d218938d8666d5a8eb596f4d29a19ca Mon Sep 17 00:00:00 2001 From: mhmmdq Date: Mon, 11 Oct 2021 08:03:55 +0330 Subject: [PATCH 07/28] add header comment blocks --- src/Server.php | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/Server.php b/src/Server.php index 3ea354e..b287768 100644 --- a/src/Server.php +++ b/src/Server.php @@ -1,10 +1,18 @@ + * @author mhmmdq + * @copyright Copyright (c), 2021 ArashAbedii + * @license MIT public license + */ namespace ArashAbedii\Server; class Server extends Logger{ - private static $err=false; + private static $err = false; //USAGE SEND REQUEST FUNCTION From 3886203c117dab31b4d881189d865aebb4d68d09 Mon Sep 17 00:00:00 2001 From: mhmmdq Date: Mon, 11 Oct 2021 09:49:53 +0330 Subject: [PATCH 08/28] Add whoops package to dependencies --- composer.json | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 75ddacd..b5c08bc 100644 --- a/composer.json +++ b/composer.json @@ -21,5 +21,7 @@ "role": "Developer" } ], - "require": {} + "require": { + "filp/whoops": "^2.14" + } } From 73f3aed74ebf7b174565fa02b53e7d4d73f1dacc Mon Sep 17 00:00:00 2001 From: mhmmdq Date: Mon, 11 Oct 2021 10:41:09 +0330 Subject: [PATCH 09/28] Slightly change the previous method to improve scalability --- src/Server.php | 263 +++++++++++++++++++++++++------------------------ 1 file changed, 132 insertions(+), 131 deletions(-) diff --git a/src/Server.php b/src/Server.php index b287768..3ffe3e8 100644 --- a/src/Server.php +++ b/src/Server.php @@ -12,184 +12,185 @@ class Server extends Logger{ - private static $err = false; - - //USAGE SEND REQUEST FUNCTION - - public static function sendRequest($url,array $params=[],$type='get',array $headers=[]){ - - //check valid data - - self::validateUrl($url); + /** + * If a problem occurs, the value changes to true + * + * @var boolean + */ + protected $haveError = false; - self::validHeaders($headers); + /** + * The target url for sending data and receiving output is in this variable + * + * @var string + */ + protected $url; - self::validType($type); + /** + * The values ​​to be sent to the url are included in this array + * + * @var array + */ + protected $params = []; + /** + * The method of sending values ​​to the url is included in this presentation + * + * @var string + */ + protected $method; + /** + * Headers that need to be sent to the url are included in this presentation + * + * @var array + */ + protected $headers = []; - if(self::$err==true){ + /** + * curl is kept in this variable + * + * @var curl + */ + public $curlInit; - echo "ERR! Check ErrHandler.log file on your project directory\n"; + /** + * Proxies are maintained for connection in this presentation + * + * @var array + */ + protected $proxy = []; - return FALSE; + /** + * All errors are stored in this variable + * + * @var array + */ + protected $errors = []; + public function __construct() + { + if (!extension_loaded('curl')) { + self::saveLog('cURL library is not loaded'); + throw new \Exception('cURL library is not loaded'); } - return self::createRequest($url,strtoupper($type),$params,$headers); - + $this->curlInit = curl_init(); } - private static function createRequest($url,$type='get',$params=[],$headers=[],$return_header=false){ - $ch=curl_init(); - if(is_array($params)){ - curl_setopt($ch,CURLOPT_URL,$url.Server::changeArrayToGetFormat($params)); - }elseif(strtolower($type)=='post'){ - curl_setopt($ch,CURLOPT_URL,$url); - curl_setopt($ch,CURLOPT_POSTFIELDS,$params); - }else{ - - self::saveLog('Parameter format is incorrect. You should send array or binary format!'); - echo "ERR! Check ErrHandler.log file on your project directory\n"; - return false; - - } - - if($return_header){ - curl_setopt($ch,CURLOPT_HEADER,1); - } - - curl_setopt($ch,CURLOPT_CUSTOMREQUEST,$type); - curl_setopt($ch,CURLOPT_HTTPHEADER,$headers); - curl_setopt($ch,CURLOPT_RETURNTRANSFER,true); - curl_setopt($ch,CURLOPT_FOLLOWLOCATION,true); - - $result=curl_exec($ch); - curl_close($ch); - - return $result; - - } + /** + * Send a request to the desired url + * + * @param string $url + * @param array $params + * @param string $type + * @param array $headers + * @return void + */ + public function sendRequest(string $url, array $params = [], string $method = 'get', array $headers = []) { - //SEND REQUEST WITHOUT RESPONSE + # Check inputs + $this->checkInputs(['url' => $url]); - public static function sendRequestWithoutResponse($url,$type='get',$params=[],$headers=[]){ - $ch=curl_init(); - if(is_array($params)){ - curl_setopt($ch,CURLOPT_URL,$url.Server::changeArrayToGetFormat($params)); - }elseif(strtolower($type)=='post'){ - curl_setopt($ch,CURLOPT_URL,$url); - curl_setopt($ch,CURLOPT_POSTFIELDS,$params); - }else { + if(!$this->haveError) { - self::saveLog('Parameter format is incorrect. You should send array or binary format!'); - echo "ERR! Check ErrHandler.log file on your project directory\n"; - return false; + $this->url = $url; + $this->params = $params; + $this->method = strtoupper($method); + $this->headers = $headers; + return $this->createRequest()->exec(); + } - - curl_setopt($ch,CURLOPT_CUSTOMREQUEST,$type); - curl_setopt($ch,CURLOPT_HTTPHEADER,$headers); - curl_setopt($ch,CURLOPT_RETURNTRANSFER,false); - curl_setopt($ch,CURLOPT_TIMEOUT,1); - curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,1); - - $result=curl_exec($ch); - curl_close($ch); - - return $result; } - - - - - //MAKING VALID FORMAT - - - - private static function changeArrayToGetFormat($params){ - - $output=''; - - foreach($params as $key=>$value){ - $value=str_replace(" ","%20",$value); - $output.=$key."=".$value."&"; - + /** + * Make a request ready to send + * + * @return void + */ + protected function createRequest() { + + + + if($this->method == "GET") { + $this->url .= "?" . http_build_query($this->params); } - return "?".trim($output,"&"); //return GET format (?a=1&b=2&c=3) - - } - - - - private static function changeArrayToHeaderFormat($headers){ - - $output=array(); - - foreach($headers as $key=>$value){ - - $output[]="$key: $value"; + curl_setopt($this->curlInit , CURLOPT_URL , $this->url); + if($this->method == "POST") { + $this->sendPostRequest(); } - return $output; //return array of headers format (["Referer: https://www.google.com/","Content-type: audio/mpeg"]) - - } - - - - - - //CHECK VALIDATE INPUTS - - private static function validateUrl($url){ - - if(empty($url)){ - - self::saveLog('Url required!'); - - self::$err=true; - + if(!empty($this->headers)) { + $this->useCustomHeader(); } - if(!filter_var($url,FILTER_VALIDATE_URL)){ - self::saveLog('Invalid url format!'); - self::$err=true; + + curl_setopt($this->curlInit, CURLOPT_RETURNTRANSFER, true); - } + return $this; } + /** + * If the method is post, the requirements apply + * + * @return void + */ + protected function sendPostRequest() { + curl_setopt($this->curlInit , CURLOPT_POST , 1); + curl_setopt($this->curlInit , CURLOPT_POSTFIELDS , $this->params); + } + /** + * Headers apply if the user has a specific header to send + * + * @return void + */ + protected function useCustomHeader() { - private static function validHeaders($headers){ + curl_setopt($this->curlInit , CURLOPT_HTTPHEADER , $this->headers); - if(!is_array($headers)){ + } - self::saveLog('Invalid headers format! headers format should be an array'); + /** + * Executes the output and returns the result + * + * @return string + */ - self::$err=true; + public function exec() { - } + return curl_exec($this->curlInit); + curl_close($this->curlInit); } + /** + * Examines the inputs + * + * @param array $inputs + * @return void + */ + protected function checkInputs(array $inputs) + { + + extract($inputs); - private static function validType($reqtype){ - - if(empty($reqtype)){ - - self::saveLog('Request type required! put GET or POST or etc type'); + if(!isset($url) && !filter_var($url , FILTER_VALIDATE_URL)) { - self::$err=true; + $this->haveError = true; + $this->errors[] = 'A valid url is required'; + self::saveLog('A valid url is required'); } + } From cd0289f459674b645faaf3fb2736f4a6784aff60 Mon Sep 17 00:00:00 2001 From: mhmmdq Date: Mon, 11 Oct 2021 10:43:43 +0330 Subject: [PATCH 10/28] A method for sending a direct request to Post --- src/Server.php | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/src/Server.php b/src/Server.php index 3ffe3e8..1b39e1b 100644 --- a/src/Server.php +++ b/src/Server.php @@ -85,7 +85,7 @@ public function __construct() * @param array $params * @param string $type * @param array $headers - * @return void + * @return string */ public function sendRequest(string $url, array $params = [], string $method = 'get', array $headers = []) { @@ -192,7 +192,32 @@ protected function checkInputs(array $inputs) } + + /** + * Undocumented function + * + * @param string $url + * @param array $params + * @param array $headers + * @return string + */ + public function post(string $url , array $params = [] , array $headers = []) { + + # Check inputs + $this->checkInputs(['url' => $url]); + if(!$this->haveError) { + + $this->url = $url; + $this->params = $params; + $this->method = "POST"; + $this->headers = $headers; + + return $this->createRequest()->exec(); + + } + + } From 8e0b6f545372bcfb519081a942209799c6972670 Mon Sep 17 00:00:00 2001 From: mhmmdq Date: Mon, 11 Oct 2021 10:45:02 +0330 Subject: [PATCH 11/28] A method for sending a direct request to Post --- src/Server.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Server.php b/src/Server.php index 1b39e1b..0a3493d 100644 --- a/src/Server.php +++ b/src/Server.php @@ -192,9 +192,9 @@ protected function checkInputs(array $inputs) } - + /** - * Undocumented function + * A method for sending a direct request to Post * * @param string $url * @param array $params From 7ebfe37a626c98a558a4fc945c6279c791dea1ec Mon Sep 17 00:00:00 2001 From: mhmmdq Date: Mon, 11 Oct 2021 10:46:04 +0330 Subject: [PATCH 12/28] create get --- src/Server.php | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/Server.php b/src/Server.php index 0a3493d..07a4b84 100644 --- a/src/Server.php +++ b/src/Server.php @@ -219,6 +219,32 @@ public function post(string $url , array $params = [] , array $headers = []) { } + /** + * A method for sending a direct request to get + * + * @param string $url + * @param array $params + * @param array $headers + * @return string + */ + public function get(string $url , array $params = [] , array $headers = []) { + + # Check inputs + $this->checkInputs(['url' => $url]); + + if(!$this->haveError) { + + $this->url = $url; + $this->params = $params; + $this->method = "GET"; + $this->headers = $headers; + + return $this->createRequest()->exec(); + + } + + } + From e5fab938ab142a1db5cc4199980f2355819f8173 Mon Sep 17 00:00:00 2001 From: mhmmdq Date: Mon, 11 Oct 2021 10:50:03 +0330 Subject: [PATCH 13/28] Correct a small mistake --- src/Server.php | 37 +++++++------------------------------ 1 file changed, 7 insertions(+), 30 deletions(-) diff --git a/src/Server.php b/src/Server.php index 07a4b84..9fd728f 100644 --- a/src/Server.php +++ b/src/Server.php @@ -87,10 +87,10 @@ public function __construct() * @param array $headers * @return string */ - public function sendRequest(string $url, array $params = [], string $method = 'get', array $headers = []) { + public function sendRequest(string $url, array $params = [], string $method = 'get', array $headers = [] , array $proxy) { # Check inputs - $this->checkInputs(['url' => $url]); + $this->checkInputs(['url' => $url , 'proxy' => $proxy]); if(!$this->haveError) { @@ -98,6 +98,7 @@ public function sendRequest(string $url, array $params = [], string $method = 'g $this->params = $params; $this->method = strtoupper($method); $this->headers = $headers; + $this->proxy = $proxy; return $this->createRequest()->exec(); @@ -201,21 +202,9 @@ protected function checkInputs(array $inputs) * @param array $headers * @return string */ - public function post(string $url , array $params = [] , array $headers = []) { + public function post(string $url , array $params = [] , array $headers = [] , array $proxy = []) { - # Check inputs - $this->checkInputs(['url' => $url]); - - if(!$this->haveError) { - - $this->url = $url; - $this->params = $params; - $this->method = "POST"; - $this->headers = $headers; - - return $this->createRequest()->exec(); - - } + return $this->sendRequest($url , $params , "POST" , $headers , $proxy); } @@ -227,21 +216,9 @@ public function post(string $url , array $params = [] , array $headers = []) { * @param array $headers * @return string */ - public function get(string $url , array $params = [] , array $headers = []) { + public function get(string $url , array $params = [] , array $headers = [] , array $proxy = []) { - # Check inputs - $this->checkInputs(['url' => $url]); - - if(!$this->haveError) { - - $this->url = $url; - $this->params = $params; - $this->method = "GET"; - $this->headers = $headers; - - return $this->createRequest()->exec(); - - } + return $this->sendRequest($url , $params , "GET" , $headers , $proxy); } From 5487fd3f0f6c96fc7fd32292790433bb29d2de2d Mon Sep 17 00:00:00 2001 From: mhmmdq Date: Mon, 11 Oct 2021 11:00:10 +0330 Subject: [PATCH 14/28] Connect to a proxy --- src/Server.php | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/src/Server.php b/src/Server.php index 9fd728f..d408029 100644 --- a/src/Server.php +++ b/src/Server.php @@ -85,6 +85,7 @@ public function __construct() * @param array $params * @param string $type * @param array $headers + * @param array $proxy * @return string */ public function sendRequest(string $url, array $params = [], string $method = 'get', array $headers = [] , array $proxy) { @@ -127,6 +128,10 @@ protected function createRequest() { $this->useCustomHeader(); } + if(!empty($this->proxy)) { + $this->connectProxyServer(); + } + @@ -191,6 +196,24 @@ protected function checkInputs(array $inputs) } + if(isset($proxy) && !empty($proxy)) { + + if(!filter_var($proxy['ip'] , FILTER_VALIDATE_IP)) { + $this->haveError = true; + $this->errors[] = 'You need a valid IP to connect to the proxy'; + self::saveLog('You need a valid IP to connect to the proxy'); + } + + if(is_numeric($proxy['port'])) { + + $this->haveError = true; + $this->errors[] = 'The proxy port must be a number'; + self::saveLog('The proxy port must be a number'); + + } + + } + } @@ -200,6 +223,7 @@ protected function checkInputs(array $inputs) * @param string $url * @param array $params * @param array $headers + * @param array $proxy * @return string */ public function post(string $url , array $params = [] , array $headers = [] , array $proxy = []) { @@ -214,6 +238,7 @@ public function post(string $url , array $params = [] , array $headers = [] , ar * @param string $url * @param array $params * @param array $headers + * @param array $proxy * @return string */ public function get(string $url , array $params = [] , array $headers = [] , array $proxy = []) { @@ -221,6 +246,24 @@ public function get(string $url , array $params = [] , array $headers = [] , arr return $this->sendRequest($url , $params , "GET" , $headers , $proxy); } + /** + * Connect to the http proxy + * + * @return void + */ + public function connectProxyServer() { + + curl_setopt($this->curlInit, CURLOPT_PROXY, $this->proxy['ip']); + curl_setopt($this->curlInit, CURLOPT_PROXYPORT, $this->proxy['port']); + + if(isset($this->proxy['auth'])) { + + curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); + curl_setopt($ch, CURLOPT_PROXYUSERPWD, $this->proxy['auth']); + + } + + } From ec39d252c1fba1626bc22d92cf371626c8165fef Mon Sep 17 00:00:00 2001 From: mhmmdq Date: Mon, 11 Oct 2021 11:02:41 +0330 Subject: [PATCH 15/28] Correct a small mistake --- src/Server.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Server.php b/src/Server.php index d408029..30b8702 100644 --- a/src/Server.php +++ b/src/Server.php @@ -88,7 +88,7 @@ public function __construct() * @param array $proxy * @return string */ - public function sendRequest(string $url, array $params = [], string $method = 'get', array $headers = [] , array $proxy) { + public function sendRequest(string $url, array $params = [], string $method = 'get', array $headers = [] , array $proxy = []) { # Check inputs $this->checkInputs(['url' => $url , 'proxy' => $proxy]); @@ -258,8 +258,8 @@ public function connectProxyServer() { if(isset($this->proxy['auth'])) { - curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); - curl_setopt($ch, CURLOPT_PROXYUSERPWD, $this->proxy['auth']); + curl_setopt($this->curlInit, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); + curl_setopt($this->curlInit, CURLOPT_PROXYUSERPWD, $this->proxy['auth']); } From 3e0499017386327007a87caa183e64d8dd43788b Mon Sep 17 00:00:00 2001 From: mhmmdq Date: Mon, 11 Oct 2021 11:10:53 +0330 Subject: [PATCH 16/28] Correct a small mistake --- src/Server.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Server.php b/src/Server.php index 30b8702..b164640 100644 --- a/src/Server.php +++ b/src/Server.php @@ -204,7 +204,7 @@ protected function checkInputs(array $inputs) self::saveLog('You need a valid IP to connect to the proxy'); } - if(is_numeric($proxy['port'])) { + if(!is_numeric($proxy['port'])) { $this->haveError = true; $this->errors[] = 'The proxy port must be a number'; From ce1516c3b6f7b6296a82fd7f9ec990abd2a7c7d4 Mon Sep 17 00:00:00 2001 From: mhmmdq Date: Mon, 11 Oct 2021 11:20:41 +0330 Subject: [PATCH 17/28] Correct a small mistake --- src/Server.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Server.php b/src/Server.php index b164640..f38c7cb 100644 --- a/src/Server.php +++ b/src/Server.php @@ -50,7 +50,7 @@ class Server extends Logger{ /** * curl is kept in this variable * - * @var curl + * */ public $curlInit; @@ -108,7 +108,7 @@ public function sendRequest(string $url, array $params = [], string $method = 'g /** * Make a request ready to send * - * @return void + * */ protected function createRequest() { From 97d6e09c58ed896d6927b9c6f1fc1c52c137ab4a Mon Sep 17 00:00:00 2001 From: mhmmdq Date: Mon, 11 Oct 2021 11:24:07 +0330 Subject: [PATCH 18/28] make test for send request --- test/RequestTest.php | 28 ++++++++++++++++++++++++++++ test/test.php | 13 ------------- 2 files changed, 28 insertions(+), 13 deletions(-) create mode 100644 test/RequestTest.php delete mode 100644 test/test.php diff --git a/test/RequestTest.php b/test/RequestTest.php new file mode 100644 index 0000000..fe625d8 --- /dev/null +++ b/test/RequestTest.php @@ -0,0 +1,28 @@ + 'Mhmmdq', + 'email' => 'mhmmdq@mhmmdq.ir', + 'github' => 'mhmmdq' + ]; + + $request = new Server; + echo $request->sendRequest($url , $params , 'post'); + + + } +} + +RequestTest::test(); \ No newline at end of file diff --git a/test/test.php b/test/test.php deleted file mode 100644 index 9666032..0000000 --- a/test/test.php +++ /dev/null @@ -1,13 +0,0 @@ -1,'b'=>2]; -$headers=['Accept'=>'application/json']; - -echo '
';
-echo Server::sendRequest($url,$params,$type,$headers);
-echo '
'; From d7052f6e49542db03c16cb5937c89b19411690c2 Mon Sep 17 00:00:00 2001 From: mhmmdq Date: Mon, 11 Oct 2021 11:25:12 +0330 Subject: [PATCH 19/28] update request test --- test/RequestTest.php | 3 --- 1 file changed, 3 deletions(-) diff --git a/test/RequestTest.php b/test/RequestTest.php index fe625d8..b05a6eb 100644 --- a/test/RequestTest.php +++ b/test/RequestTest.php @@ -4,9 +4,6 @@ use ArashAbedii\Server\Server; - -$request = new Server; - class RequestTest { public static function test() { From 9d739a17e7502d954d703cc8e001d5039368cd77 Mon Sep 17 00:00:00 2001 From: mhmmdq Date: Mon, 11 Oct 2021 12:02:34 +0330 Subject: [PATCH 20/28] add curl error to log --- src/Server.php | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/Server.php b/src/Server.php index f38c7cb..193deca 100644 --- a/src/Server.php +++ b/src/Server.php @@ -170,12 +170,21 @@ protected function useCustomHeader() { */ public function exec() { + $responsve = curl_exec($this->curlInit); - return curl_exec($this->curlInit); + if($responsve !== false) + return $responsve; + else + return $this->curlError(); + curl_close($this->curlInit); } + protected function curlError() { + $this->errors['curl_error'] = curl_error($this->curlInit); + self::saveLog('CURLERROR: ' . $this->errors['curl_error']); + } /** * Examines the inputs From 0959a926479bfa3b0c6dfb5cad3aea6e2ea8ac55 Mon Sep 17 00:00:00 2001 From: mhmmdq Date: Mon, 11 Oct 2021 12:03:50 +0330 Subject: [PATCH 21/28] add curl error to log --- src/Server.php | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/Server.php b/src/Server.php index 193deca..38d7330 100644 --- a/src/Server.php +++ b/src/Server.php @@ -175,15 +175,21 @@ public function exec() { if($responsve !== false) return $responsve; else - return $this->curlError(); + $this->curlError(); curl_close($this->curlInit); } - + /** + * Undocumented function + * + * @return void + */ protected function curlError() { $this->errors['curl_error'] = curl_error($this->curlInit); self::saveLog('CURLERROR: ' . $this->errors['curl_error']); + + } /** From 27cc9080e6c283c94ee2905e42ac2ba6c72bce7d Mon Sep 17 00:00:00 2001 From: mhmmdq Date: Mon, 11 Oct 2021 12:04:44 +0330 Subject: [PATCH 22/28] add curl error to log --- src/Server.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Server.php b/src/Server.php index 38d7330..1f5620b 100644 --- a/src/Server.php +++ b/src/Server.php @@ -181,7 +181,7 @@ public function exec() { } /** - * Undocumented function + * Save curl problems in the list of errors * * @return void */ @@ -189,7 +189,7 @@ protected function curlError() { $this->errors['curl_error'] = curl_error($this->curlInit); self::saveLog('CURLERROR: ' . $this->errors['curl_error']); - + } /** From 2c4b9cbcbb274d4a833a959f15ac358837892999 Mon Sep 17 00:00:00 2001 From: mhmmdq Date: Mon, 11 Oct 2021 12:13:15 +0330 Subject: [PATCH 23/28] update errors to public --- src/Exception.php | 8 ++++++++ src/Server.php | 4 ++-- 2 files changed, 10 insertions(+), 2 deletions(-) create mode 100644 src/Exception.php diff --git a/src/Exception.php b/src/Exception.php new file mode 100644 index 0000000..51a3c4d --- /dev/null +++ b/src/Exception.php @@ -0,0 +1,8 @@ + Date: Mon, 11 Oct 2021 12:15:29 +0330 Subject: [PATCH 24/28] make redirect method --- src/Server.php | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/Server.php b/src/Server.php index 8a3bab5..eba87cb 100644 --- a/src/Server.php +++ b/src/Server.php @@ -279,6 +279,15 @@ public function connectProxyServer() { } } + /** + * Move the user to a new page + * + * @param string $url + * @return void + */ + public function redirect(string $url) { + header('location: ' . $url); + } From ce9211ce4f983473623b902461bc2897109e680b Mon Sep 17 00:00:00 2001 From: mhmmdq Date: Mon, 11 Oct 2021 12:16:19 +0330 Subject: [PATCH 25/28] remove some files --- Server.php | 256 ----------------------------------------------------- test.php | 13 --- 2 files changed, 269 deletions(-) delete mode 100644 Server.php delete mode 100644 test.php diff --git a/Server.php b/Server.php deleted file mode 100644 index 36d3402..0000000 --- a/Server.php +++ /dev/null @@ -1,256 +0,0 @@ -$value){ - $value=str_replace(" ","%20",$value); - $output.=$key."=".$value."&"; - - } - - return "?".trim($output,"&"); //return GET format (?a=1&b=2&c=3) - - } - - - - private static function changeArrayToHeaderFormat($headers){ - - $output=array(); - - foreach($headers as $key=>$value){ - - $output[]="$key: $value"; - - } - - return $output; //return array of headers format (["Referer: https://www.google.com/","Content-type: audio/mpeg"]) - - } - - - - - - //CHECK VALIDATE INPUTS - - private static function validateUrl($url){ - - if(empty($url)){ - - file_put_contents("ErrHandler.log","\nERR MESSAGE: Url required !\t".date("d M Y H:i:s"),FILE_APPEND); - - self::$err=true; - - } - - if(!filter_var($url,FILTER_VALIDATE_URL)){ - - file_put_contents("ErrHandler.log","\nERR MESSAGE: Invalid url format !\t".date("d M Y H:i:s"),FILE_APPEND); - - self::$err=true; - - } - - } - - - - private static function validParams($params){ - - if(!is_array($params)){ - - file_put_contents("ErrHandler.log","\nERR MESSAGE: Invalid params format! params format should be an array\t".date("d M Y H:i:s"),FILE_APPEND); - - self::$err=true; - - } - - } - - - - private static function validHeaders($headers){ - - if(!is_array($headers)){ - - file_put_contents("ErrHandler.log","\nERR MESSAGE: Invalid headers format! headers format should be an array\t".date("d M Y H:i:s"),FILE_APPEND); - - self::$err=true; - - } - - } - - - - private static function validType($reqtype){ - - if(empty($reqtype)){ - - file_put_contents("ErrHandler.log","\nERR MESSAGE: Request type required! put GET or POST or etc type\t".date("d M Y H:i:s"),FILE_APPEND); - - self::$err=true; - - } - - } - - - - - - - -} diff --git a/test.php b/test.php deleted file mode 100644 index 1d81501..0000000 --- a/test.php +++ /dev/null @@ -1,13 +0,0 @@ -'cat','amount'=>5]; -$response=Server::sendRequest($url,$params,'GET'); - -echo '
';
-var_dump(json_decode($response,true));
-echo '
'; From 0552d4bceb64ebf89d0222497922a0ff19de5ade Mon Sep 17 00:00:00 2001 From: mhmmdq Date: Mon, 11 Oct 2021 12:17:32 +0330 Subject: [PATCH 26/28] update .gitignore --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.gitignore b/.gitignore index 57872d0..fd96c37 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,4 @@ /vendor/ +/src/log.txt +/test/myTestLogFile.txt +/test/log.txt \ No newline at end of file From 1e078a48ddde8850079be13182722a43a763f48e Mon Sep 17 00:00:00 2001 From: mhmmdq Date: Mon, 11 Oct 2021 12:54:24 +0330 Subject: [PATCH 27/28] update readme.md --- README.md | 124 ++++++++++++++++++++++++------------------------- src/Server.php | 1 + 2 files changed, 62 insertions(+), 63 deletions(-) diff --git a/README.md b/README.md index 55cbd05..89625c0 100644 --- a/README.md +++ b/README.md @@ -1,82 +1,80 @@ -# SERVER -## php class to work on different web services (API) +## A simple class for sending http requests +Send http requests and receive answers easily with a few lines by this class; This class is just a simple example that can be turned into a more professional software with your help -
+# How to use +In the first step, install the software package with composer (if you do not know what composer is, [click here](https://code.tutsplus.com/tutorials/what-is-composer-for-php-and-how-to-install-it--cms-35160 "click here")) -## usage +### Installation with Composer +Run this command on your command line (make sure composer is installed) +```bash +composer require arashabedii/server ``` -Server::sendRequest(string url, array parameters, string request type= get or post, array headers); +### Add depending on the application +Now by loading the autoload.php file created in the Vendor folder you will have access to the class. Create and use an object from the class +```php + 'Mhmmdq', + 'email' => 'mhmmdq@mhmmdq.ir', + 'github' => 'mhmmdq' +]; + +$request = new Server; +echo $request->sendRequest($url , $params , 'post'); ``` -

+### Submit a request +In general, to send a request to the server, you must use the `sendRequest` method, which accepts the following inputs: -#### at first include Server.php file to your project file
-``` -require 'Server.php'; -``` -#### after you can call Server::sendRequest() to send your requests. - -

- - ## examples: - -
- - **SEND GET REQUEST**
- ```PHP - 'cat','amount'=>5]; - $response=Server::sendRequest($url,$params,'GET'); - - echo '
';
-      var_dump(json_decode($response,true));
-      echo '
'; - - ``` -

+`$url` The first and only mandatory entry of the string in which the destination of the request is placed +`$params` An array in which the parameters sent to the destination are placed as keys and values ​​and can be empty -**SEND POST REQUEST**
-```PHP - '1','parameter2'=>2]; - $response=Server::sendRequest($url,$params,'POST'); - - echo '
';
-      var_dump(json_decode($response,true));
-      echo '
'; +`$headers` If you need to send a special header to the destination, you can enter it as a key and value array +```php +$request->sendRequest('https://mhmmdq.ir/requestTest.php' , [ + 'username'=>'mhmmdq' , + 'password'=>'xxxxxxxx'] , 'post'); ``` +#### Send a request directly with the get method +You can send your request directly using the get command -

- +```php +echo $request->get($url , $params); +``` -**SEND POST REQUEST WITH HEADERS**
+#### Send a request directly with the post method +You can send your request directly using the post command - ```PHP - post($url , $params); +``` - require 'Server.php'; +### Errors And Logs +If there is an error while performing a variable called `haveError` will be converted to `true` and the entire list of errors will be placed in a variable called `errors`. Also, all errors will be recorded in a file whose location and name can be changed. - //SEND POST REQUEST - $url="https://api.example.com/v1/method"; - $headers=['Authorization'=>'Bearer ACCESS_TOKEN']; - $params=[]; - //send request - $response=Server::sendRequest($url,[],$request_type,$headers); +```php +if($request->haveError) { + var_dump($request->errors); +} +``` - echo '
';
-      var_dump(json_decode($response,true));
-      echo '
'; +##### Specify the log file address +You can change the storage location of the file as shown below +```php +Server::changeLogPath(__DIR__ . 'log.txt'); +``` +### Move user to a new location +```php +$request->redirect('https://google.com'); ``` + diff --git a/src/Server.php b/src/Server.php index eba87cb..fe3d7c3 100644 --- a/src/Server.php +++ b/src/Server.php @@ -186,6 +186,7 @@ public function exec() { * @return void */ protected function curlError() { + $this->haveError = true; $this->errors['curl_error'] = curl_error($this->curlInit); self::saveLog('CURLERROR: ' . $this->errors['curl_error']); From d30ef3f2c342feadc2b28708b3d305f7b223b151 Mon Sep 17 00:00:00 2001 From: mhmmdq Date: Mon, 11 Oct 2021 13:03:53 +0330 Subject: [PATCH 28/28] update composer.json --- composer.json | 1 + 1 file changed, 1 insertion(+) diff --git a/composer.json b/composer.json index b5c08bc..1adc64a 100644 --- a/composer.json +++ b/composer.json @@ -1,5 +1,6 @@ { "name": "arashabedii/server", + "type": "library", "description": "php class to work on different web services", "license": "MIT", "autoload": {