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

HttpRequest::sendRequest()   B

Complexity

Conditions 6
Paths 24

Size

Total Lines 39
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 28
CRAP Score 6.0702

Importance

Changes 3
Bugs 2 Features 0
Metric Value
c 3
b 2
f 0
dl 0
loc 39
ccs 28
cts 32
cp 0.875
rs 8.439
cc 6
eloc 28
nc 24
nop 3
crap 6.0702
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 = sprintf('SEOstats %s https://github.com/eyecatchup/SEOstats',
30 7
                \SEOstats\SEOstats::BUILD_NO);
31
32 7
        $ch = curl_init($url);
33 7
        curl_setopt_array($ch, array(
34 7
            CURLOPT_USERAGENT       => $ua,
35 7
            CURLOPT_RETURNTRANSFER  => 1,
36 7
            CURLOPT_CONNECTTIMEOUT  => 30,
37 7
            CURLOPT_FOLLOWLOCATION  => 1,
38 7
            CURLOPT_MAXREDIRS       => 2,
39 7
            CURLOPT_SSL_VERIFYPEER  => 0,
40 7
        ));
41 7
        if(DefaultSettings::CURLOPT_PROXY !== '') {
42
            curl_setopt($ch, CURLOPT_PROXY, DefaultSettings::CURLOPT_PROXY);
43
        }
44 7
        if(DefaultSettings::CURLOPT_PROXYUSERPWD !== '') {
45
            curl_setopt($ch, CURLOPT_PROXYUSERPWD, DefaultSettings::CURLOPT_PROXYUSERPWD);
46
        }
47
48 7
        if (false !== $postData) {
49 4
            if (false !== $postJson) {
50 2
                curl_setopt($ch, CURLOPT_HTTPHEADER,
51 2
                    array('Content-type: application/json'));
52 2
                $data = json_encode($postData);
53 2
            } else {
54 2
                $data = http_build_query($postData);
55
            }
56 4
            curl_setopt($ch, CURLOPT_POST, 1);
57 4
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
58 4
        }
59
60 7
        $response = curl_exec($ch);
61 7
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
62 7
        curl_close($ch);
63
64 7
        return (200 == (int)$httpCode) ? $response : false;
65
    }
66
67
    /**
68
     * HTTP HEAD request with curl.
69
     *
70
     * @access        private
71
     * @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...
72
     * @return        Integer                 Returns the HTTP status code received in
73
     *                                        response to a GET request of the input URL.
74
     */
75 1
    public static function getHttpCode($url)
76
    {
77 1
        $ua = sprintf('SEOstats %s https://github.com/eyecatchup/SEOstats',
78 1
                \SEOstats\SEOstats::BUILD_NO);
79
80 1
        $ch = curl_init($url);
81 1
        curl_setopt_array($ch, array(
82 1
            CURLOPT_USERAGENT       => $ua,
83 1
            CURLOPT_RETURNTRANSFER  => 1,
84 1
            CURLOPT_CONNECTTIMEOUT  => 10,
85 1
            CURLOPT_FOLLOWLOCATION  => 1,
86 1
            CURLOPT_MAXREDIRS       => 2,
87 1
            CURLOPT_SSL_VERIFYPEER  => 0,
88 1
            CURLOPT_NOBODY          => 1,
89 1
        ));
90 1
        if(DefaultSettings::CURLOPT_PROXY !== '') {
91
            curl_setopt($ch, CURLOPT_PROXY, DefaultSettings::CURLOPT_PROXY);
92
        }
93 1
        if(DefaultSettings::CURLOPT_PROXYUSERPWD !== '') {
94
            curl_setopt($ch, CURLOPT_PROXYUSERPWD, DefaultSettings::CURLOPT_PROXYUSERPWD);
95
        }
96
97 1
        curl_exec($ch);
98 1
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
99 1
        curl_close($ch);
100
101 1
        return (int)$httpCode;
102
    }
103
104 2
    public function getFile($url, $file)
105
    {
106 2
        $ua = sprintf('SEOstats %s https://github.com/eyecatchup/SEOstats',
107 2
                \SEOstats\SEOstats::BUILD_NO);
108
109 2
        $fp = fopen("$file", 'w');
110
111 2
        $ch = curl_init($url);
112 2
        curl_setopt_array($ch, array(
113 2
            CURLOPT_USERAGENT       => $ua,
114 2
            CURLOPT_RETURNTRANSFER  => 1,
115 2
            CURLOPT_CONNECTTIMEOUT  => 30,
116 2
            CURLOPT_FOLLOWLOCATION  => 1,
117 2
            CURLOPT_MAXREDIRS       => 2,
118 2
            CURLOPT_SSL_VERIFYPEER  => 0,
119 2
            CURLOPT_FILE            => $fp,
120 2
        ));
121 2
        if(DefaultSettings::CURLOPT_PROXY !== '') {
122
            curl_setopt($ch, CURLOPT_PROXY, DefaultSettings::CURLOPT_PROXY);
123
        }
124 2
        if(DefaultSettings::CURLOPT_PROXYUSERPWD !== '') {
125
            curl_setopt($ch, CURLOPT_PROXYUSERPWD, DefaultSettings::CURLOPT_PROXYUSERPWD);
126
        }
127
128 2
        curl_exec($ch);
129 2
        curl_close($ch);
130 2
        fclose($fp);
131
132 2
        clearstatcache();
133 2
        return (bool)(false !== stat($file));
134
    }
135
}
136