Completed
Push — master ( 25c087...a46420 )
by Lawrence
01:31
created

Curl   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 90
ccs 0
cts 40
cp 0
rs 10
c 0
b 0
f 0
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setOptions() 0 12 1
A __construct() 0 4 1
B post() 0 41 4
1
<?php
2
/*
3
 +------------------------------------------------------------------------+
4
 | Plinker-RPC PHP                                                        |
5
 +------------------------------------------------------------------------+
6
 | Copyright (c)2017-2018 (https://github.com/plinker-rpc/core)           |
7
 +------------------------------------------------------------------------+
8
 | This source file is subject to MIT License                             |
9
 | that is bundled with this package in the file LICENSE.                 |
10
 |                                                                        |
11
 | If you did not receive a copy of the license and are unable to         |
12
 | obtain it through the world-wide-web, please send an email             |
13
 | to [email protected] so we can send you a copy immediately.        |
14
 +------------------------------------------------------------------------+
15
 | Authors: Lawrence Cherone <[email protected]>                     |
16
 +------------------------------------------------------------------------+
17
 */
18
19
namespace Plinker\Core\Lib;
20
21
/**
22
 * Plinker\Core\Lib\Curl
23
 */
24
final class Curl
25
{
26
    /**
27
     * @var
28
     */
29
    private $config;
30
31
    /**
32
     * @var
33
     */
34
    private $options;
35
36
    /**
37
     * Class construct
38
     *
39
     * @param  array  $config  - config array which holds object configuration
40
     * @return void
41
     */
42
    public function __construct(array $config = [])
43
    {
44
        //
45
        $this->config = $config;
46
    }
47
48
    /**
49
     * @return void
50
     */
51
    private function setOptions()
52
    {
53
        //
54
        $this->options = [
55
            CURLOPT_FAILONERROR    => true,
56
            CURLOPT_FOLLOWLOCATION => true,
57
            CURLOPT_RETURNTRANSFER => true,
58
            CURLOPT_SSL_VERIFYPEER => false,
59
            CURLOPT_SSL_VERIFYHOST => false,
60
            CURLOPT_ENCODING       => "gzip",
61
            CURLOPT_HTTPHEADER     => [
62
                "Content-Type: application/json"
63
            ]
64
        ];
65
    }
66
67
    /**
68
     *  POST
69
     *
70
     * @param string $url        - url of the plinker server
71
     * @param array  $parameters - post parameters
72
     */
73
    public function post($url, $parameters = [], $headers = [])
74
    {
75
        //
76
        $this->setOptions();
77
78
        //
79
        $parameters = json_encode($parameters);
80
        $parameters = gzdeflate($parameters, 9);
81
82
        //
83
        $curl = curl_init($url);
84
85
        //
86
        $this->options[CURLOPT_POST] = true;
87
        $this->options[CURLOPT_POSTFIELDS] = $parameters;
88
89
        // set request headers
90
        if (!empty($headers)) {
91
            foreach ($headers as $header) {
92
                $this->options[CURLOPT_HTTPHEADER][] = $header;
93
            }
94
        }
95
96
        //
97
        curl_setopt_array($curl, $this->options);
98
99
        //
100
        $body = curl_exec($curl);
101
    
102
        //
103
        $return = [
104
            'body'    => $body,
105
            'code'    => curl_getinfo($curl, CURLINFO_HTTP_CODE),
106
            'error'   => (curl_error($curl) ? curl_error($curl) : null)
107
        ];
108
109
        //
110
        curl_close($curl);
111
112
        //
113
        return $return;
114
    }
115
}
116