Issues (186)

includes/Helpers/HttpHelper.php (2 issues)

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