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