Passed
Pull Request — master (#8)
by Bob
03:01
created

cURL.php ➔ downloadData()   B

Complexity

Conditions 2
Paths 13

Size

Total Lines 25
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 2 Features 0
Metric Value
cc 2
eloc 20
c 3
b 2
f 0
nc 13
nop 1
dl 0
loc 25
rs 8.8571
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
 *
29
 * @return string
30
 */
31
function downloadData($url)
32
{
33
    try {
34
        $userAgent = 'Mozilla/5.0 (Windows NT 6.2) AppleWebKit/536.6 (KHTML, like Gecko) Chrome/20.0.1090.0 Safari/536.6';
35
        $curl = curl_init();
36
        curl_setopt($curl, CURLOPT_USERAGENT, $userAgent);
37
        curl_setopt($curl, CURLOPT_TIMEOUT, 12);
38
        curl_setopt($curl, CURLOPT_POST, false);
39
        curl_setopt($curl, CURLOPT_FORBID_REUSE, false);
40
        curl_setopt($curl, CURLOPT_ENCODING, '');
41
        $headers = [];
42
        $headers[] = 'Connection: keep-alive';
43
        $headers[] = 'Keep-Alive: timeout=12, max=1000';
44
        curl_setopt($curl, CURLOPT_URL, $url);
45
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
46
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
47
        $result = curl_exec($curl);
48
49
        return $result;
50
    } catch (Exception $e) {
51
        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...
52
53
        return;
54
    }
55
}
56
/**
57
 * @param string $url
58
 * @param $downloadPath
59
 *
60
 * @return bool
61
 */
62
function downloadLargeData($url, $downloadPath)
63
{
64
    try {
65
        $readHandle = fopen($url, 'rb');
66
        $writeHandle = fopen($downloadPath, 'w+b');
67
        if (!$readHandle || !$writeHandle) {
68
            return false;
69
        }
70
        while (!feof($readHandle)) {
71
            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...
72
                return false;
73
            }
74
        }
75
        fclose($readHandle);
76
        fclose($writeHandle);
77
78
        return true;
79
    } catch (Exception $e) {
80
        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...
81
82
        return false;
83
    }
84
}
85