Completed
Pull Request — master (#112)
by
unknown
03:22
created

HttpRequest::sendRequest()   B

Complexity

Conditions 5
Paths 9

Size

Total Lines 35
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 27
CRAP Score 5.0011

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 35
ccs 27
cts 28
cp 0.9643
rs 8.439
cc 5
eloc 26
nc 9
nop 3
crap 5.0011
1
<?php
2
namespace SEOstats\Helper;
3
4
/**
5
 * HTTP Request Helper Class
6
 *
7
 * @package    SEOstats
8
 * @author     Stephan Schmitz <[email protected]>
9
 * @copyright  Copyright (c) 2010 - present Stephan Schmitz
10
 * @license    http://eyecatchup.mit-license.org/  MIT License
11
 * @updated    2013/05/12
12
 */
13
14
class HttpRequest
15
{
16
    /**
17
     *  HTTP GET/POST request with curl.
18
     * @access    public
19
     * @param     String $url The Request URL
20
     * @param     Array $postData Optional: POST data array to be send.
21
     * @return    Mixed                   On success, returns the response string.
22
     *                                     Else, the the HTTP status code received
23
     *                                     in reponse to the request.
24
     */
25 7
    public static function sendRequest($url, $postData = false, $postJson = false)
26
    {
27 7
        $ua = sprintf('SEOstats %s https://github.com/eyecatchup/SEOstats',
28 7
            \SEOstats\SEOstats::BUILD_NO);
29
30 7
        $ch = curl_init($url);
31 7
        curl_setopt_array($ch, array(
32 7
            CURLOPT_USERAGENT => $ua,
33 7
            CURLOPT_RETURNTRANSFER => 1,
34 7
            CURLOPT_CONNECTTIMEOUT => 30,
35 7
            CURLOPT_FOLLOWLOCATION => 1,
36 7
            CURLOPT_MAXREDIRS => 2,
37 7
            CURLOPT_SSL_VERIFYPEER => 0,
38 7
        ));
39
40 7
        if (false !== $postData) {
41 4
            if (false !== $postJson) {
42 2
                curl_setopt($ch, CURLOPT_HTTPHEADER,
43 2
                    array('Content-type: application/json'));
44 2
                $data = json_encode($postData);
45 2
            } else {
46 2
                $data = http_build_query($postData);
47
            }
48 4
            curl_setopt($ch, CURLOPT_POST, 1);
49 4
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
50 4
        }
51
52 7
        $response = curl_exec($ch);
53 7
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
54 7
        curl_close($ch);
55 7
        if (429 == (int)$httpCode) {
56
            throw new TooManyRequestsException("To many requests");
57
        }
58 7
        return (200 == (int)$httpCode) ? $response : false;
59
    }
60
61
    /**
62
     * HTTP HEAD request with curl.
63
     *
64
     * @access        private
65
     * @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...
66
     * @return        Integer                 Returns the HTTP status code received in
67
     *                                        response to a GET request of the input URL.
68
     */
69 1
    public static function getHttpCode($url)
70
    {
71 1
        $ua = sprintf('SEOstats %s https://github.com/eyecatchup/SEOstats',
72 1
            \SEOstats\SEOstats::BUILD_NO);
73
74 1
        $ch = curl_init($url);
75 1
        curl_setopt_array($ch, array(
76 1
            CURLOPT_USERAGENT => $ua,
77 1
            CURLOPT_RETURNTRANSFER => 1,
78 1
            CURLOPT_CONNECTTIMEOUT => 10,
79 1
            CURLOPT_FOLLOWLOCATION => 1,
80 1
            CURLOPT_MAXREDIRS => 2,
81 1
            CURLOPT_SSL_VERIFYPEER => 0,
82 1
            CURLOPT_NOBODY => 1,
83 1
        ));
84
85 1
        curl_exec($ch);
86 1
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
87 1
        curl_close($ch);
88
89 1
        return (int)$httpCode;
90
    }
91
92 2
    public function getFile($url, $file)
93
    {
94 2
        $ua = sprintf('SEOstats %s https://github.com/eyecatchup/SEOstats',
95 2
            \SEOstats\SEOstats::BUILD_NO);
96
97 2
        $fp = fopen("$file", 'w');
98
99 2
        $ch = curl_init($url);
100 2
        curl_setopt_array($ch, array(
101 2
            CURLOPT_USERAGENT => $ua,
102 2
            CURLOPT_RETURNTRANSFER => 1,
103 2
            CURLOPT_CONNECTTIMEOUT => 30,
104 2
            CURLOPT_FOLLOWLOCATION => 1,
105 2
            CURLOPT_MAXREDIRS => 2,
106 2
            CURLOPT_SSL_VERIFYPEER => 0,
107 2
            CURLOPT_FILE => $fp,
108 2
        ));
109
110 2
        curl_exec($ch);
111 2
        curl_close($ch);
112 2
        fclose($fp);
113
114 2
        clearstatcache();
115 2
        return (bool)(false !== stat($file));
116
    }
117
}
118