Completed
Pull Request — master (#2)
by Tharanga
02:11
created

CurlRequest::request()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 2
dl 0
loc 11
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @author Tharanga Kothalawala <[email protected]>
4
 * @date 30-12-2018
5
 */
6
7
namespace TSK\SSO\Http;
8
9
/**
10
 * @codeCoverageIgnore
11
 * @internal
12
 * @package TSK\SSO\Storage
13
 *
14
 * This can send GET & POST requests. @todo: Add Guzzle
15
 */
16
class CurlRequest
17
{
18
    /**
19
     * @var resource
20
     */
21
    private $curl;
22
23
    public function __construct()
24
    {
25
        $this->curl = curl_init();
26
        curl_setopt($this->curl, CURLOPT_SSL_VERIFYPEER, false);
27
        curl_setopt($this->curl, CURLOPT_SSL_VERIFYHOST, false);
28
        curl_setopt($this->curl, CURLOPT_FOLLOWLOCATION, true);
29
        curl_setopt($this->curl, CURLOPT_CONNECTTIMEOUT, 120);
30
        curl_setopt($this->curl, CURLOPT_TIMEOUT, 120);
31
    }
32
33
    public function __destruct()
34
    {
35
        if (!empty($this->curl)) {
36
            curl_close($this->curl);
37
        }
38
    }
39
40
    /**
41
     * makes GET request to a given endpoint
42
     *
43
     * @param string $url external url
44
     * @param array $headers http headers if any [optional]
45
     * @return string
46
     */
47
    public function get($url, array $headers = array())
48
    {
49
        return $this->request($url, $headers);
50
    }
51
52
    /**
53
     * makes POST request to a given endpoint
54
     *
55
     * @param string $url external url
56
     * @param array $data [optional] data to post
57
     * @param array $headers [optional] http headers if any
58
     * @return string
59
     */
60
    public function post($url, array $data = array(), array $headers = array())
61
    {
62
        if (!empty($data)) {
63
            curl_setopt($this->curl, CURLOPT_POST, count($data));
64
            curl_setopt($this->curl, CURLOPT_POSTFIELDS, $data);
65
        }
66
67
        return $this->request($url, $headers);
68
    }
69
70
    /**
71
     * makes POST request to a given endpoint using raw data
72
     *
73
     * @param string $url external url
74
     * @param string $rawData [optional] data to post
75
     * @param array $headers [optional] http headers if any
76
     * @return string
77
     */
78
    public function postRaw($url, $rawData = null, array $headers = array())
79
    {
80
        if (!empty($rawData)) {
81
            curl_setopt($this->curl, CURLOPT_POST, strlen($rawData));
82
            curl_setopt($this->curl, CURLOPT_POSTFIELDS, $rawData);
83
        }
84
85
        return $this->request($url, $headers);
86
    }
87
88
    /**
89
     * @todo: this is ugly, find a solution
90
     */
91
    public function yahooGet($url, array $headers)
92
    {
93
        $this->curl = curl_init();
94
95
        return $this->get($url, $headers);
96
    }
97
98
    private function request($url, array $headers)
99
    {
100
        curl_setopt($this->curl, CURLOPT_URL, $url);
101
        curl_setopt($this->curl, CURLOPT_REFERER, $url);
102
        curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, true);
103
104
        if (!empty($headers)) {
105
            curl_setopt($this->curl, CURLOPT_HTTPHEADER, $headers);
106
        }
107
108
        return curl_exec($this->curl);
109
    }
110
}
111