modern handling of http request
	
		
			
	
		
	
	
		
	
		
			Some checks failed
		
		
	
	
		
			
				
	
				soko-web/pipeline/head There was a failure building this commit
				
			
		
		
	
	
				
					
				
			
		
			Some checks failed
		
		
	
	soko-web/pipeline/head There was a failure building this commit
				
			This commit is contained in:
		
							parent
							
								
									d78a82c14c
								
							
						
					
					
						commit
						4b089b4e84
					
				| @ -7,13 +7,15 @@ class Request implements IRequest | ||||
| { | ||||
|     private string $url; | ||||
| 
 | ||||
|     private int $method; | ||||
|     private ?string $method = null; | ||||
| 
 | ||||
|     private string $query = ''; | ||||
| 
 | ||||
|     private ?string $body = null; | ||||
| 
 | ||||
|     private array $headers = []; | ||||
| 
 | ||||
|     public function __construct(string $url = '', int $method = self::HTTP_GET) | ||||
|     public function __construct(string $url = '', ?string $method = null) | ||||
|     { | ||||
|         $this->url = $url; | ||||
|         $this->method = $method; | ||||
| @ -24,7 +26,7 @@ class Request implements IRequest | ||||
|         $this->url = $url; | ||||
|     } | ||||
| 
 | ||||
|     public function setMethod(int $method): void | ||||
|     public function setMethod(string $method): void | ||||
|     { | ||||
|         $this->method = $method; | ||||
|     } | ||||
| @ -38,6 +40,11 @@ class Request implements IRequest | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     public function setBody(string $body): void | ||||
|     { | ||||
|         $this->body = $body; | ||||
|     } | ||||
| 
 | ||||
|     public function setHeaders(array $headers): void | ||||
|     { | ||||
|         $this->headers = array_merge($this->headers, $headers); | ||||
| @ -47,13 +54,20 @@ class Request implements IRequest | ||||
|     { | ||||
|         $ch = curl_init(); | ||||
| 
 | ||||
|         if ($this->method === self::HTTP_POST) { | ||||
|             $url = $this->url; | ||||
|         $url = $this->url . '?' . $this->query; | ||||
| 
 | ||||
|             curl_setopt($ch, CURLOPT_POST, 1); | ||||
|             curl_setopt($ch, CURLOPT_POSTFIELDS, $this->query); | ||||
|         } else { | ||||
|             $url = $this->url . '?' . $this->query; | ||||
|         if ($this->body !== null) { | ||||
|             if ($this->method === null) { | ||||
|                 $this->method = self::HTTP_POST; | ||||
|             } | ||||
| 
 | ||||
|             if ($this->method === self::HTTP_POST) { | ||||
|                 curl_setopt($ch, CURLOPT_POST, 1); | ||||
|             } else { | ||||
|                 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $this->method); | ||||
|             } | ||||
| 
 | ||||
|             curl_setopt($ch, CURLOPT_POSTFIELDS, $this->body); | ||||
|         } | ||||
| 
 | ||||
|         curl_setopt($ch, CURLOPT_URL, $url); | ||||
|  | ||||
| @ -2,16 +2,24 @@ | ||||
| 
 | ||||
| interface IRequest | ||||
| { | ||||
|     const HTTP_GET = 0; | ||||
|     const HTTP_GET = 'GET'; | ||||
| 
 | ||||
|     const HTTP_POST = 1; | ||||
|     const HTTP_POST = 'POST'; | ||||
| 
 | ||||
|     const HTTP_PUT = 'PUT'; | ||||
| 
 | ||||
|     const HTTP_PATCH = 'PATCH'; | ||||
| 
 | ||||
|     const HTTP_DELETE = 'DELETE'; | ||||
| 
 | ||||
|     public function setUrl(string $url): void; | ||||
| 
 | ||||
|     public function setMethod(int $method): void; | ||||
|     public function setMethod(string $method): void; | ||||
| 
 | ||||
|     public function setQuery($query): void; | ||||
| 
 | ||||
|     public function setBody(string $body): void; | ||||
| 
 | ||||
|     public function setHeaders(array $headers): void; | ||||
| 
 | ||||
|     public function send(): IResponse; | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user