public
Description: Libraries for Solar PHP Framework
Homepage: http://code.google.com/p/lux/
Clone URL: git://github.com/anttih/lux.git
lux / Lux / Http / Request / Adapter / Http.php
100644 79 lines (67 sloc) 2.342 kb
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
76
77
78
79
<?php
/**
*
* Uses pecl_http for a standalone HTTP request.
*
* @package Lux
*
* @subpackage Lux_Http
*
* @author Antti Holvikari <anttih@gmail.com>
*
* @license http://opensource.org/licenses/bsd-license.php BSD
*
*/
class Lux_Http_Request_Adapter_Http extends Solar_Http_Request_Adapter {
 
    /**
*
* Support method to make the request, then return headers and content.
*
* @param string $uri The URI get a response from.
*
* @param array $headers A sequential array of header lines for the request.
*
* @param string $content A string of content for the request.
*
* @return array A sequential array where element 0 is a sequential array of
* header lines, and element 1 is the body content.
*
* @todo Implement an exception for timeouts.
*
*/
    protected function _fetch($uri, $headers, $content)
    {
        $http = new HttpRequest;
        
        // set HTTP method
        $http->setMethod(constant("HTTP_METH_{$this->_method}"));
        
        // set specialized headers and retain all others
        $http_header = array();
        foreach ($headers as $header) {
            $pos = strpos($header, ':');
            $label = substr($header, 0, $pos);
            $value = substr($header, $pos + 2);
            
            $http_header[$label] = $value;
        }
        $http->setOptions(array('headers' => $http_header));
        
        $http->setUrl($uri);
        
        // decide what content to set
        if (! empty($content)) {
            if ($this->_method == 'POST') {
                $http->addRawPostData($content);
            } elseif ($this->_method == 'PUT') {
                $http->addPutData($content);
            }
        }
        
        // make the request
        $response = $http->send();
        
        $version = $response->getHttpVersion();
        $code = $response->getResponseCode();
        $status = $response->getResponseStatus();
        
        $headers = $response->getHeaders();
        
        // build status line. i.e. HTTP/1.1 200 OK
        $status_line = "HTTP/$version $code $status";
        
        // add status line as the first header
        array_unshift($headers, $status_line);
        
        return array($headers, $response->getBody());
    }
}