Completed
Pull Request — master (#129)
by
unknown
05:32
created

HttpRequest::getHttpCode()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 28
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 3

Importance

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