|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @author stev leibelt <[email protected]> |
|
4
|
|
|
* @since 2015-12-09 |
|
5
|
|
|
*/ |
|
6
|
|
|
|
|
7
|
|
|
namespace Net\Bazzline\Component\Curl\Dispatcher; |
|
8
|
|
|
|
|
9
|
|
|
use Net\Bazzline\Component\Curl\Response\Response; |
|
10
|
|
|
|
|
11
|
|
|
class Dispatcher implements DispatcherInterface |
|
12
|
|
|
{ |
|
13
|
|
|
/** @var array */ |
|
14
|
|
|
private $headerLines; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @param string $url |
|
18
|
|
|
* @param array $options |
|
19
|
|
|
* @return Response |
|
20
|
|
|
*/ |
|
21
|
|
|
public function dispatch($url, array $options = array()) |
|
22
|
|
|
{ |
|
23
|
|
|
$this->reset(); |
|
24
|
|
|
$handler = $this->getHandler($url); |
|
25
|
|
|
$handler = $this->setOptions($handler, $options); |
|
26
|
|
|
$response = $this->execute($handler); |
|
27
|
|
|
|
|
28
|
|
|
return $response; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @param resource $handler |
|
33
|
|
|
* @return Response |
|
34
|
|
|
*/ |
|
35
|
|
|
protected function execute($handler) |
|
36
|
|
|
{ |
|
37
|
|
|
$content = curl_exec($handler); |
|
38
|
|
|
$contentType = curl_getinfo($handler, CURLINFO_CONTENT_TYPE); |
|
39
|
|
|
//@see http://stackoverflow.com/a/10667879 |
|
40
|
|
|
$error = curl_error($handler); |
|
41
|
|
|
$errorCode = curl_errno($handler); |
|
42
|
|
|
$statusCode = curl_getinfo($handler, CURLINFO_HTTP_CODE); |
|
43
|
|
|
//@todo investigate if needed http://www.ivangabriele.com/php-how-to-use-4-methods-delete-get-post-put-in-a-restful-api-client-using-curl/ |
|
44
|
|
|
//@todo how to handle response code 100 - other header? - http://stackoverflow.com/a/23939785 |
|
45
|
|
|
|
|
46
|
|
|
return new Response($content, $contentType, $error, $errorCode, $this->headerLines, $statusCode); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @param string $url |
|
51
|
|
|
* @return resource |
|
52
|
|
|
*/ |
|
53
|
|
|
protected function getHandler($url) |
|
54
|
|
|
{ |
|
55
|
|
|
$handler = curl_init($url); |
|
56
|
|
|
|
|
57
|
|
|
return $handler; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @param resource $handler |
|
62
|
|
|
* @param array $options |
|
63
|
|
|
* @return resource |
|
64
|
|
|
*/ |
|
65
|
|
|
protected function setOptions($handler, array $options) |
|
66
|
|
|
{ |
|
67
|
|
|
$options[CURLINFO_HEADER_OUT] = 1; |
|
68
|
|
|
$options[CURLOPT_HEADERFUNCTION] = array($this, 'processHeadLine'); |
|
69
|
|
|
$options[CURLOPT_RETURNTRANSFER] = true; |
|
70
|
|
|
|
|
71
|
|
|
curl_setopt_array($handler, $options); |
|
72
|
|
|
|
|
73
|
|
|
return $handler; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
|
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @param resource $handler |
|
80
|
|
|
* @param string $string |
|
81
|
|
|
* @return int |
|
82
|
|
|
*/ |
|
83
|
|
|
private function processHeadLine($handler, $string) |
|
84
|
|
|
{ |
|
85
|
|
|
$delimiter = ':'; |
|
86
|
|
|
$exploded = explode($delimiter, trim($string)); |
|
87
|
|
|
$isValid = (count($exploded) === 2); |
|
88
|
|
|
|
|
89
|
|
|
if ($isValid) { |
|
90
|
|
|
$prefix = array_shift($exploded); |
|
91
|
|
|
$this->headerLines[$prefix] = implode($delimiter, $exploded); //needed because of lines like "Date: Thu, 17 Dec 2015 16:47:42 GMT" |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
return strlen($string); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
private function reset() |
|
98
|
|
|
{ |
|
99
|
|
|
$this->headerLines = array(); |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|