Issues (7)

src/Request.php (7 issues)

1
<?php
2
namespace quideroli\httpRequest;
3
4
use GuzzleHttp\Client;
5
use GuzzleHttp\Exception\ClientException;
6
7
class Request
8
{
9
    public function __construct($url, $query = NULL, $header = [], $body = "")
10
    {
11
        $this->url = $url;
0 ignored issues
show
Bug Best Practice introduced by
The property url does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
12
13
        $this->config = [
0 ignored issues
show
Bug Best Practice introduced by
The property config does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
14
            "headers" => $header,
15
            "query" => $query,
16
            "body" => !empty($body) ? json_encode($body) : "{}"
17
        ];
18
    }
19
20
    public function get()
21
    {
22
        try {
23
            $send = (new Client())->get($this->url, $this->config);
24
            return $send->getBody()->getContents();
25
        }catch(ClientException $e){
26
            $this->error = $e;
0 ignored issues
show
Bug Best Practice introduced by
The property error does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
27
            return false;
28
        }
29
30
    }
31
32
    public function post()
33
    {
34
        try {
35
            $send = (new Client())->post($this->url, $this->config);
36
            return $send->getBody()->getContents();
37
        }catch(ClientException $e){
38
            $this->error = $e;
0 ignored issues
show
Bug Best Practice introduced by
The property error does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
39
            return false;
40
        }
41
    }
42
43
    public function put()
44
    {
45
        try {
46
            $send = (new Client())->put($this->url, $this->config);
47
            return $send->getBody()->getContents();
48
        }catch(ClientException $e){
49
            $this->error = $e;
0 ignored issues
show
Bug Best Practice introduced by
The property error does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
50
            return false;
51
        }
52
    }
53
54
    public function delete()
55
    {
56
        try {
57
            $send = (new Client())->delete($this->url, $this->config);
58
            return $send->getBody()->getContents();
59
        }catch(ClientException $e){
60
            $this->error = $e;
0 ignored issues
show
Bug Best Practice introduced by
The property error does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
61
            return false;
62
        }
63
    }
64
65
    public function fail($show=true)
66
    {
67
        if($show==true){
68
            header('Cache-Control: no-cache, must-revalidate');
69
            header('Content-Type: application/json');
70
71
            http_response_code($this->error->getCode());
72
            echo $this->error->getResponse()->getBody();
73
            exit();
0 ignored issues
show
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
74
        }else{
75
            return $this->error->getResponse()->getBody();
76
        }
77
    }
78
}