Passed
Push — master ( 7c7d88...5359ca )
by Bob
03:13 queued 30s
created

cURL.php ➔ downloadData()   A

Complexity

Conditions 2
Paths 13

Size

Total Lines 23
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 2
eloc 20
c 2
b 1
f 0
nc 13
nop 1
dl 0
loc 23
rs 9.0856
1
<?php
2
/**
3
 * The MIT License (MIT)
4
 *
5
 * Copyright (c) 2016 Robert Sardinia
6
 *
7
 * Permission is hereby granted, free of charge, to any person obtaining a copy
8
 * of this software and associated documentation files (the "Software"), to deal
9
 * in the Software without restriction, including without limitation the rights
10
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
 * copies of the Software, and to permit persons to whom the Software is
12
 * furnished to do so, subject to the following conditions:
13
 *
14
 * The above copyright notice and this permission notice shall be included in all
15
 * copies or substantial portions of the Software.
16
 *
17
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
 * SOFTWARE.
24
 */
25
26
/**
27
 * @param $url
28
 * @return string
29
 */
30
function downloadData($url)
31
{
32
    try {
33
        $userAgent = "Mozilla/5.0 (Windows NT 6.2) AppleWebKit/536.6 (KHTML, like Gecko) Chrome/20.0.1090.0 Safari/536.6";
34
        $curl = curl_init();
35
        curl_setopt($curl, CURLOPT_USERAGENT, $userAgent);
36
        curl_setopt($curl, CURLOPT_TIMEOUT, 12);
37
        curl_setopt($curl, CURLOPT_POST, false);
38
        curl_setopt($curl, CURLOPT_FORBID_REUSE, false);
39
        curl_setopt($curl, CURLOPT_ENCODING, "");
40
        $headers = array();
41
        $headers[] = "Connection: keep-alive";
42
        $headers[] = "Keep-Alive: timeout=12, max=1000";
43
        curl_setopt($curl, CURLOPT_URL, $url);
44
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
45
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
46
        $result = curl_exec($curl);
47
        return $result;
48
    } catch (Exception $e) {
49
        var_dump("cURL Error: " . $e->getMessage());
0 ignored issues
show
Security Debugging Code introduced by
var_dump('cURL Error: ' . $e->getMessage()); looks like debug code. Are you sure you do not want to remove it? This might expose sensitive data.
Loading history...
50
        return null;
51
    }
52
}
53
/**
54
 * @param string $url
55
 * @param $downloadPath
56
 * @return bool
57
 */
58
function downloadLargeData($url, $downloadPath)
59
{
60
    try {
61
        $readHandle = fopen($url, "rb");
62
        $writeHandle = fopen($downloadPath, "w+b");
63
        if (!$readHandle || !$writeHandle) {
64
                    return false;
65
        }
66
        while (!feof($readHandle)) {
67
            if (fwrite($writeHandle, fread($readHandle, 4096)) == false) {
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing fwrite($writeHandle, fread($readHandle, 4096)) of type integer to the boolean false. If you are specifically checking for 0, consider using something more explicit like === 0 instead.
Loading history...
68
                            return false;
69
            }
70
        }
71
        fclose($readHandle);
72
        fclose($writeHandle);
73
        return true;
74
    } catch (Exception $e) {
75
        var_dump("Download Error: " . $e->getMessage());
0 ignored issues
show
Security Debugging Code introduced by
var_dump('Download Error: ' . $e->getMessage()); looks like debug code. Are you sure you do not want to remove it? This might expose sensitive data.
Loading history...
76
        return false;
77
    }
78
}
79