HttpRequest   A
last analyzed

Complexity

Total Complexity 21

Size/Duplication

Total Lines 156
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 78.18%

Importance

Changes 4
Bugs 2 Features 0
Metric Value
wmc 21
c 4
b 2
f 0
lcom 1
cbo 1
dl 0
loc 156
ccs 86
cts 110
cp 0.7818
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
B sendRequest() 0 40 6
B getHttpCode() 0 29 3
B getFile() 0 32 3
A getUserAgent() 0 10 3
A getProxy() 0 10 3
A getProxyUserPwd() 0 10 3
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
     *                                     in reponse to the request.
26
     */
27 7
    public static function sendRequest($url, $postData = false, $postJson = false)
28
    {
29 7
        $ua = self::getUserAgent();
30 7
        $curlopt_proxy = self::getProxy();
31 7
        $curlopt_proxyuserpwd = self::getProxyUserPwd();
32
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 7
            CURLOPT_MAXREDIRS       => 2,
40 7
            CURLOPT_SSL_VERIFYPEER  => 0,
41 7
        ));
42 7
        if($curlopt_proxy) {
43
            curl_setopt($ch, CURLOPT_PROXY, $curlopt_proxy);
44
        }
45 7
        if($curlopt_proxyuserpwd) {
46
            curl_setopt($ch, CURLOPT_PROXYUSERPWD, $curlopt_proxyuserpwd);
47
        }
48
49 7
        if (false !== $postData) {
50 4
            if (false !== $postJson) {
51 2
                curl_setopt($ch, CURLOPT_HTTPHEADER,
52 2
                    array('Content-type: application/json'));
53 2
                $data = json_encode($postData);
54 2
            } else {
55 2
                $data = http_build_query($postData);
56
            }
57 4
            curl_setopt($ch, CURLOPT_POST, 1);
58 4
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
59 4
        }
60
61 7
        $response = curl_exec($ch);
62 7
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
63 7
        curl_close($ch);
64
65 7
        return (200 == (int)$httpCode) ? $response : false;
66
    }
67
68
    /**
69
     * HTTP HEAD request with curl.
70
     *
71
     * @access        private
72
     * @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
     * @return        Integer                 Returns the HTTP status code received in
74
     *                                        response to a GET request of the input URL.
75
     */
76 1
    public static function getHttpCode($url)
77
    {
78 1
        $ua = self::getUserAgent();
79 1
        $curlopt_proxy = self::getProxy();
80 1
        $curlopt_proxyuserpwd = self::getProxyUserPwd();
81
82 1
        $ch = curl_init($url);
83 1
        curl_setopt_array($ch, array(
84 1
            CURLOPT_USERAGENT       => $ua,
85 1
            CURLOPT_RETURNTRANSFER  => 1,
86 1
            CURLOPT_CONNECTTIMEOUT  => 10,
87 1
            CURLOPT_FOLLOWLOCATION  => 1,
88 1
            CURLOPT_MAXREDIRS       => 2,
89 1
            CURLOPT_SSL_VERIFYPEER  => 0,
90 1
            CURLOPT_NOBODY          => 1,
91 1
        ));
92 1
        if($curlopt_proxy) {
93
            curl_setopt($ch, CURLOPT_PROXY, $curlopt_proxy);
94
        }
95 1
        if($curlopt_proxyuserpwd) {
96
            curl_setopt($ch, CURLOPT_PROXYUSERPWD, $curlopt_proxyuserpwd);
97
        }
98
99 1
        curl_exec($ch);
100 1
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
101 1
        curl_close($ch);
102
103 1
        return (int)$httpCode;
104
    }
105
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
114 2
        $ch = curl_init($url);
115 2
        curl_setopt_array($ch, array(
116 2
            CURLOPT_USERAGENT       => $ua,
117 2
            CURLOPT_RETURNTRANSFER  => 1,
118 2
            CURLOPT_CONNECTTIMEOUT  => 30,
119 2
            CURLOPT_FOLLOWLOCATION  => 1,
120 2
            CURLOPT_MAXREDIRS       => 2,
121 2
            CURLOPT_SSL_VERIFYPEER  => 0,
122 2
            CURLOPT_FILE            => $fp,
123 2
        ));
124 2
        if($curlopt_proxy) {
125
            curl_setopt($ch, CURLOPT_PROXY, $curlopt_proxy);
126
        }
127 2
        if($curlopt_proxyuserpwd) {
128
            curl_setopt($ch, CURLOPT_PROXYUSERPWD, $curlopt_proxyuserpwd);
129
        }
130
131 2
        curl_exec($ch);
132 2
        curl_close($ch);
133 2
        fclose($fp);
134
135 2
        clearstatcache();
136 2
        return (bool)(false !== stat($file));
137
    }
138
139 12
    public static function getUserAgent() {
140 12
        $ua = sprintf('SEOstats %s https://github.com/eyecatchup/SEOstats', \SEOstats\SEOstats::BUILD_NO);
141 12
        if(\SEOstats\Config\DefaultSettings::UA !== '') {
142
            $ua = \SEOstats\Config\DefaultSettings::UA;
143
        }
144 12
        if(\SEOstats\SEOstats::getUserAgent()) {
145
            $ua = \SEOstats\SEOstats::getUserAgent();
146
        }
147 12
        return $ua;
148
    }
149
150 12
    public static function getProxy() {
151 12
        $curlopt_proxy = false;
152 12
        if(\SEOstats\Config\DefaultSettings::CURLOPT_PROXY !== '') {
153
            $curlopt_proxy = \SEOstats\Config\DefaultSettings::CURLOPT_PROXY;
154
        }
155 12
        if(\SEOstats\SEOstats::getCurloptProxy()) {
156
            $curlopt_proxy = \SEOstats\SEOstats::getCurloptProxy();
157
        }
158 12
        return $curlopt_proxy;
159
    }
160
161 12
    public static function getProxyUserPwd() {
162 12
        $curlopt_proxyuserpwd = false;
163 12
        if(\SEOstats\Config\DefaultSettings::CURLOPT_PROXYUSERPWD !== '') {
164
            $curlopt_proxyuserpwd = \SEOstats\Config\DefaultSettings::CURLOPT_PROXYUSERPWD;
165
        }
166 12
        if(\SEOstats\SEOstats::getCurloptProxyuserpwd()) {
167
            $curlopt_proxyuserpwd = \SEOstats\SEOstats::getCurloptProxyuserpwd();
168
        }
169 12
        return $curlopt_proxyuserpwd;
170
    }
171
}
172