/
FileGetContents.php
75 lines (61 loc) · 1.89 KB
/
FileGetContents.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<?php
namespace Buzz\Client;
use Buzz\Message\MessageInterface;
use Buzz\Message\RequestInterface;
use Buzz\Util\CookieJar;
use Buzz\Exception\ClientException;
class FileGetContents extends AbstractStream implements ClientInterface
{
protected $cookieJar;
public function __construct(CookieJar $cookieJar = null)
{
if ($cookieJar) {
$this->setCookieJar($cookieJar);
}
}
public function setCookieJar(CookieJar $cookieJar)
{
$this->cookieJar = $cookieJar;
}
public function getCookieJar()
{
return $this->cookieJar;
}
/**
* @see ClientInterface
*
* @throws ClientException If file_get_contents() fires an error
*/
public function send(RequestInterface $request, MessageInterface $response)
{
if ($cookieJar = $this->getCookieJar()) {
$cookieJar->clearExpiredCookies();
$cookieJar->addCookieHeaders($request);
}
$context = stream_context_create($this->getStreamContextArray($request));
$url = $request->getHost().$request->getResource();
$level = error_reporting(0);
$content = file_get_contents($url, 0, $context);
error_reporting($level);
if (false === $content) {
$error = error_get_last();
throw new ClientException($error['message']);
}
$response->setHeaders($this->filterHeaders((array) $http_response_header));
$response->setContent($content);
if ($cookieJar) {
$cookieJar->processSetCookieHeaders($request, $response);
}
}
private function filterHeaders(array $headers)
{
$filtered = array();
foreach ($headers as $header) {
if (0 === stripos($header, 'http/')) {
$filtered = array();
}
$filtered[] = $header;
}
return $filtered;
}
}