1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* php-guard/curl <https://github.com/php-guard/curl> |
4
|
|
|
* Copyright (C) ${YEAR} by Alexandre Le Borgne <[email protected]>. |
5
|
|
|
* |
6
|
|
|
* This program is free software: you can redistribute it and/or modify |
7
|
|
|
* it under the terms of the GNU General Public License as published by |
8
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
9
|
|
|
* (at your option) any later version. |
10
|
|
|
* |
11
|
|
|
* This program is distributed in the hope that it will be useful, |
12
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14
|
|
|
* GNU General Public License for more details. |
15
|
|
|
* |
16
|
|
|
* You should have received a copy of the GNU General Public License |
17
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
namespace PhpGuard\Curl; |
21
|
|
|
|
22
|
|
|
class CurlResponseFactory |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @param $ch |
26
|
|
|
* @param $content |
27
|
|
|
* |
28
|
|
|
* @return CurlResponse |
29
|
|
|
* |
30
|
|
|
* @throws CurlError |
31
|
|
|
*/ |
32
|
45 |
|
public function create($ch, $content) |
33
|
|
|
{ |
34
|
45 |
|
if (false === $content) { |
35
|
3 |
|
$message = curl_error($ch); |
36
|
3 |
|
$code = curl_errno($ch); |
37
|
|
|
|
38
|
3 |
|
throw new CurlError($message, $code); |
39
|
|
|
} |
40
|
|
|
|
41
|
45 |
|
$info = curl_getinfo($ch); |
42
|
|
|
|
43
|
45 |
|
$statusCode = $info['http_code']; |
44
|
45 |
|
$headerSize = $info['header_size']; |
45
|
45 |
|
$rawHeaders = substr($content, 0, $headerSize); |
46
|
45 |
|
$raw = substr($content, $headerSize); |
47
|
|
|
|
48
|
|
|
$headers = array_reduce(explode("\n", $rawHeaders), function ($headers, $header) { |
49
|
45 |
|
$parts = explode(':', $header); |
50
|
45 |
|
if (2 == count($parts)) { |
51
|
45 |
|
$headers[trim($parts[0])] = trim($parts[1]); |
52
|
|
|
} |
53
|
|
|
|
54
|
45 |
|
return $headers; |
55
|
45 |
|
}, []); |
56
|
|
|
|
57
|
45 |
|
return new CurlResponse($statusCode, $raw, $headers, $info); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|