Failed Conditions
Push — newinternal-releasecandidate ( 2e1778...b14046 )
by Simon
15:26 queued 05:35
created

includes/Helpers/HttpHelper.php (1 issue)

Labels
Severity
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;
0 ignored issues
show
The type Waca\SiteConfiguration was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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
     *
56
     * @return string
57
     * @throws CurlException
58
     */
59
    public function get($url, $parameters = null, $headers = array())
60
    {
61
        if ($parameters !== null && is_array($parameters)) {
62
            $getString = '?' . http_build_query($parameters);
63
            $url .= $getString;
64
        }
65
66
        curl_setopt($this->curlHandle, CURLOPT_URL, $url);
67
68
        // Make sure we're doing a GET
69
        curl_setopt($this->curlHandle, CURLOPT_POST, false);
70
71
        curl_setopt($this->curlHandle, CURLOPT_HTTPHEADER, $headers);
72
73
        $result = curl_exec($this->curlHandle);
74
75
        if ($result === false) {
76
            $error = curl_error($this->curlHandle);
77
            throw new CurlException('Remote request failed with error ' . $error);
78
        }
79
80
        return $result;
81
    }
82
83
    /**
84
     * Posts data to a URL
85
     *
86
     * @param string $url        The URL to fetch.
87
     * @param array  $parameters Key/value pair of POST parameters to add to the request.
88
     * @param array  $headers
89
     *
90
     * @return string
91
     * @throws CurlException
92
     */
93
    public function post($url, $parameters, $headers = array())
94
    {
95
        curl_setopt($this->curlHandle, CURLOPT_URL, $url);
96
97
        // Make sure we're doing a POST
98
        curl_setopt($this->curlHandle, CURLOPT_POST, true);
99
        curl_setopt($this->curlHandle, CURLOPT_POSTFIELDS, http_build_query($parameters));
100
101
        curl_setopt($this->curlHandle, CURLOPT_HTTPHEADER, $headers);
102
103
        $result = curl_exec($this->curlHandle);
104
105
        if ($result === false) {
106
            $error = curl_error($this->curlHandle);
107
            throw new CurlException('Remote request failed with error ' . $error);
108
        }
109
110
        return $result;
111
    }
112
113
    /**
114
     * @return string
115
     */
116
    public function getError()
117
    {
118
        return curl_error($this->curlHandle);
119
    }
120
}
121