Passed
Push — master ( e2eed7...6f6e4e )
by Bob
03:02
created

cURL.php ➔ downloadData()   B

Complexity

Conditions 2
Paths 13

Size

Total Lines 25
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

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