Failed Conditions
Push — newinternal ( b66232...216d62 )
by Simon
16:33 queued 06:35
created

includes/Helpers/HttpHelper.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
13
class HttpHelper
14
{
15
    private $curlHandle;
16
17
    /**
18
     * HttpHelper constructor.
19
     *
20
     * @param string  $userAgent
21
     * @param boolean $disableVerifyPeer
22
     * @param string  $cookieJar
0 ignored issues
show
Should the type for parameter $cookieJar not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
23
     */
24
    public function __construct($userAgent, $disableVerifyPeer, $cookieJar = null)
25
    {
26
        $this->curlHandle = curl_init();
27
28
        curl_setopt($this->curlHandle, CURLOPT_RETURNTRANSFER, true);
29
        curl_setopt($this->curlHandle, CURLOPT_USERAGENT, $userAgent);
30
        curl_setopt($this->curlHandle, CURLOPT_FAILONERROR, true);
31
32
        if ($disableVerifyPeer) {
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 View Code Duplication
        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 View Code Duplication
        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
}