1234567891011121314151617181920212223242526272829303132333435363738394041424344 |
- <?php
- class Request {
- private $url;
- private $headers;
- private $body;
- public function __construct($url, array $headers = [], string $body = ''){
- if(is_string($url) || is_array($url)){
- $this->url = new Uri($url);
- }elseif($url instanceof Uri){
- $this->url = $url;
- }else{
- throw new Exception("Invalid url {$url}");
- }
- if(is_array($headers)){
- $this->headers = $headers;
- }else{
- $this->headers = [];
- }
- $this->body = $body;
- }
- public function __get($name){
- switch($name){
- case 'headers':
- return $this->headers;
- break;
- case 'url':
- return $this->url;
- break;
- case 'body':
- return $this->body;
- break;
- }
- }
- public function header($name){
- return $this->headers[$name];
- }
- public function json(){
- return json_decode($this->body, true);
- }
- public function text(){
- return $this->body;
- }
- }
- ?>
|