Completed
Pull Request — master (#129)
by
unknown
03:19
created

HttpRequest::getProxy()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
ccs 0
cts 0
cp 0
rs 9.4285
cc 3
eloc 7
nc 4
nop 0
crap 12
1
<?php
2
namespace SEOstats\Helper;
3
4
use SEOstats\Config\DefaultSettings;
5
6
/**
7
 * HTTP Request Helper Class
8
 *
9
 * @package    SEOstats
10
 * @author     Stephan Schmitz <[email protected]>
11
 * @copyright  Copyright (c) 2010 - present Stephan Schmitz
12
 * @license    http://eyecatchup.mit-license.org/  MIT License
13
 * @updated    2016/03/17
14
 */
15
16
class HttpRequest
17
{
18
    /**
19
     *  HTTP GET/POST request with curl.
20
     *  @access    public
21
     *  @param     String      $url        The Request URL
22
     *  @param     Array       $postData   Optional: POST data array to be send.
23
     *  @return    Mixed                   On success, returns the response string.
24
     *                                     Else, the the HTTP status code received
25 7
     *                                     in reponse to the request.
26
     */
27 7
    public static function sendRequest($url, $postData = false, $postJson = false)
28 7
    {
29
        $ua = self::getUserAgent();
30 7
        $curlopt_proxy = self::getProxy();
31 7
        $curlopt_proxyuserpwd = self::getProxyUserPwd();
32 7
33 7
        $ch = curl_init($url);
34 7
        curl_setopt_array($ch, array(
35 7
            CURLOPT_USERAGENT       => $ua,
36 7
            CURLOPT_RETURNTRANSFER  => 1,
37 7
            CURLOPT_CONNECTTIMEOUT  => 30,
38 7
            CURLOPT_FOLLOWLOCATION  => 1,
39
            CURLOPT_MAXREDIRS       => 2,
40 7
            CURLOPT_SSL_VERIFYPEER  => 0,
41 4
        ));
42 2
        if($curlopt_proxy) {
43 2
            curl_setopt($ch, CURLOPT_PROXY, $curlopt_proxy);
44 2
        }
45 2
        if($curlopt_proxyuserpwd) {
46 2
            curl_setopt($ch, CURLOPT_PROXYUSERPWD, $curlopt_proxyuserpwd);
47
        }
48 4
49 4
        if (false !== $postData) {
50 4
            if (false !== $postJson) {
51
                curl_setopt($ch, CURLOPT_HTTPHEADER,
52 7
                    array('Content-type: application/json'));
53 7
                $data = json_encode($postData);
54 7
            } else {
55
                $data = http_build_query($postData);
56 7
            }
57
            curl_setopt($ch, CURLOPT_POST, 1);
58
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
59
        }
60
61
        $response = curl_exec($ch);
62
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
63
        curl_close($ch);
64
65
        return (200 == (int)$httpCode) ? $response : false;
66
    }
67 1
68
    /**
69 1
     * HTTP HEAD request with curl.
70 1
     *
71
     * @access        private
72 1
     * @param         String        $a        The request URL
0 ignored issues
show
Bug introduced by
There is no parameter named $a. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
73 1
     * @return        Integer                 Returns the HTTP status code received in
74 1
     *                                        response to a GET request of the input URL.
75 1
     */
76 1
    public static function getHttpCode($url)
77 1
    {
78 1
        $ua = self::getUserAgent();
79 1
        $curlopt_proxy = self::getProxy();
80 1
        $curlopt_proxyuserpwd = self::getProxyUserPwd();
81 1
82
        $ch = curl_init($url);
83 1
        curl_setopt_array($ch, array(
84 1
            CURLOPT_USERAGENT       => $ua,
85 1
            CURLOPT_RETURNTRANSFER  => 1,
86
            CURLOPT_CONNECTTIMEOUT  => 10,
87 1
            CURLOPT_FOLLOWLOCATION  => 1,
88
            CURLOPT_MAXREDIRS       => 2,
89
            CURLOPT_SSL_VERIFYPEER  => 0,
90 2
            CURLOPT_NOBODY          => 1,
91
        ));
92 2
        if($curlopt_proxy) {
93 2
            curl_setopt($ch, CURLOPT_PROXY, $curlopt_proxy);
94
        }
95 2
        if($curlopt_proxyuserpwd) {
96
            curl_setopt($ch, CURLOPT_PROXYUSERPWD, $curlopt_proxyuserpwd);
97 2
        }
98 2
99 2
        curl_exec($ch);
100 2
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
101 2
        curl_close($ch);
102 2
103 2
        return (int)$httpCode;
104 2
    }
105 2
106 2
    public function getFile($url, $file)
107
    {
108 2
        $ua = self::getUserAgent();
109 2
        $curlopt_proxy = self::getProxy();
110 2
        $curlopt_proxyuserpwd = self::getProxyUserPwd();
111
112 2
        $fp = fopen("$file", 'w');
113 2
114
        $ch = curl_init($url);
115
        curl_setopt_array($ch, array(
116 1
            CURLOPT_USERAGENT       => $ua,
117
            CURLOPT_RETURNTRANSFER  => 1,
118
            CURLOPT_CONNECTTIMEOUT  => 30,
119
            CURLOPT_FOLLOWLOCATION  => 1,
120
            CURLOPT_MAXREDIRS       => 2,
121
            CURLOPT_SSL_VERIFYPEER  => 0,
122
            CURLOPT_FILE            => $fp,
123
        ));
124
        if($curlopt_proxy) {
125
            curl_setopt($ch, CURLOPT_PROXY, $curlopt_proxy);
126
        }
127
        if($curlopt_proxyuserpwd) {
128
            curl_setopt($ch, CURLOPT_PROXYUSERPWD, $curlopt_proxyuserpwd);
129
        }
130
131
        curl_exec($ch);
132
        curl_close($ch);
133
        fclose($fp);
134
135
        clearstatcache();
136
        return (bool)(false !== stat($file));
137
    }
138
139
    public static function getUserAgent() {
140
        $ua = sprintf('SEOstats %s https://github.com/eyecatchup/SEOstats', \SEOstats\SEOstats::BUILD_NO);
141
        if(\SEOstats\Config\DefaultSettings::UA !== '') {
142
            $ua = \SEOstats\Config\DefaultSettings::UA;
143
        }
144
        if(\SEOstats\SEOstats::getUserAgent()) {
145
            $ua = \SEOstats\SEOstats::getUserAgent();
146
        }
147
        return $ua;
148
    }
149
150
    public static function getProxy() {
151
        $curlopt_proxy = false;
152
        if(\SEOstats\Config\DefaultSettings::CURLOPT_PROXY !== '') {
153
            $curlopt_proxy = \SEOstats\Config\DefaultSettings::CURLOPT_PROXY;
154
        }
155
        if(\SEOstats\SEOstats::getCurloptProxy()) {
156
            $curlopt_proxy = \SEOstats\SEOstats::getCurloptProxy();
157
        }
158
        return $curlopt_proxy;
159
    }
160
161
    public static function getProxyUserPwd() {
162
        $curlopt_proxyuserpwd = false;
163
        if(\SEOstats\Config\DefaultSettings::CURLOPT_PROXYUSERPWD !== '') {
164
            $curlopt_proxyuserpwd = \SEOstats\Config\DefaultSettings::CURLOPT_PROXYUSERPWD;
165
        }
166
        if(\SEOstats\SEOstats::getCurloptProxyuserpwd()) {
167
            $curlopt_proxyuserpwd = \SEOstats\SEOstats::getCurloptProxyuserpwd();
168
        }
169
        return $curlopt_proxyuserpwd;
170
    }
171
}
172