HttpHelper   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 11
eloc 35
dl 0
loc 109
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __destruct() 0 3 1
A post() 0 18 2
A __construct() 0 15 3
A getError() 0 3 1
A get() 0 25 4
1
<?php
2
/******************************************************************************
3
 * Wikipedia Account Creation Assistance tool                                 *
4
 *                                                                            *
5
 * All code in this file is released into the public domain by the ACC        *
6
 * Development Team. Please see team.json for a list of contributors.         *
7
 ******************************************************************************/
8
9
namespace Waca\Helpers;
10
11
use Waca\Exceptions\CurlException;
12
use Waca\SiteConfiguration;
13
14
class HttpHelper
15
{
16
    private $curlHandle;
17
18
    /**
19
     * HttpHelper constructor.
20
     *
21
     * @param SiteConfiguration $siteConfiguration
22
     * @param string            $cookieJar
23
     */
24
    public function __construct($siteConfiguration, $cookieJar = null)
25
    {
26
        $this->curlHandle = curl_init();
27
28
        curl_setopt($this->curlHandle, CURLOPT_RETURNTRANSFER, true);
29
        curl_setopt($this->curlHandle, CURLOPT_USERAGENT, $siteConfiguration->getUserAgent());
30
        curl_setopt($this->curlHandle, CURLOPT_FAILONERROR, true);
31
32
        if ($siteConfiguration->getCurlDisableVerifyPeer()) {
33
            curl_setopt($this->curlHandle, CURLOPT_SSL_VERIFYPEER, false);
34
        }
35
36
        if ($cookieJar !== null) {
37
            curl_setopt($this->curlHandle, CURLOPT_COOKIEFILE, $cookieJar);
38
            curl_setopt($this->curlHandle, CURLOPT_COOKIEJAR, $cookieJar);
39
        }
40
    }
41
42
    public function __destruct()
43
    {
44
        curl_close($this->curlHandle);
45
    }
46
47
    /**
48
     * Fetches the content of a URL, with an optional parameter set.
49
     *
50
     * @param string     $url        The URL to fetch.
51
     * @param null|array $parameters Key/value pair of GET parameters to add to the request.
52
     *                               Null lets you handle it yourself.
53
     *
54
     * @param array      $headers
55
     * @param int        $timeout Timeout in ms
56
     *
57
     * @return string
58
     * @throws CurlException
59
     */
60
    public function get($url, $parameters = null, $headers = array(), $timeout = 300000)
61
    {
62
        if ($parameters !== null && is_array($parameters)) {
63
            $getString = '?' . http_build_query($parameters);
64
            $url .= $getString;
65
        }
66
67
        curl_setopt($this->curlHandle, CURLOPT_URL, $url);
68
69
        // Make sure we're doing a GET
70
        curl_setopt($this->curlHandle, CURLOPT_POST, false);
71
72
        curl_setopt($this->curlHandle, CURLOPT_HTTPHEADER, $headers);
73
74
        curl_setopt($this->curlHandle, CURLOPT_CONNECTTIMEOUT_MS, $timeout);
75
        curl_setopt($this->curlHandle, CURLOPT_TIMEOUT_MS, $timeout);
76
77
        $result = curl_exec($this->curlHandle);
78
79
        if ($result === false) {
80
            $error = curl_error($this->curlHandle);
81
            throw new CurlException('Remote request failed with error ' . $error);
82
        }
83
84
        return $result;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $result also could return the type true which is incompatible with the documented return type string.
Loading history...
85
    }
86
87
    /**
88
     * Posts data to a URL
89
     *
90
     * @param string $url        The URL to fetch.
91
     * @param array  $parameters Key/value pair of POST parameters to add to the request.
92
     * @param array  $headers
93
     *
94
     * @return string
95
     * @throws CurlException
96
     */
97
    public function post($url, $parameters, $headers = array())
98
    {
99
        curl_setopt($this->curlHandle, CURLOPT_URL, $url);
100
101
        // Make sure we're doing a POST
102
        curl_setopt($this->curlHandle, CURLOPT_POST, true);
103
        curl_setopt($this->curlHandle, CURLOPT_POSTFIELDS, http_build_query($parameters));
104
105
        curl_setopt($this->curlHandle, CURLOPT_HTTPHEADER, $headers);
106
107
        $result = curl_exec($this->curlHandle);
108
109
        if ($result === false) {
110
            $error = curl_error($this->curlHandle);
111
            throw new CurlException('Remote request failed with error ' . $error);
112
        }
113
114
        return $result;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $result also could return the type true which is incompatible with the documented return type string.
Loading history...
115
    }
116
117
    /**
118
     * @return string
119
     */
120
    public function getError()
121
    {
122
        return curl_error($this->curlHandle);
123
    }
124
}
125