Subversion Repositories instagram

[/] [classes/] [CurlHttpClient.php] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 ffruehstueck
<?php
2
class CurlHttpClient {
3
    /**
4
     * Default User-Agent
5
     * @var string
6
     */
7
    const DEFAULT_USER_AGENT = 'Instagram PHP Implementation http://mauriciocuenca.com/';
8
 
9
    /**
10
     * Used HTTP request methods
11
     */
12
    const GET = 'GET';
13
    const POST = 'POST';
14
    const DELETE = 'DELETE';
15
 
16
    /**
17
     * cURL handler
18
     * @var resource
19
     */
20
    private $handler;
21
 
22
    /**
23
     * Store the POST fields
24
     */
25
    private $postParams = array();
26
 
27
    /**
28
     * Initiate a cURL session
29
     * @param string $url
30
     */
31
    public function __construct($uri) {
32
        $this->handler = curl_init($uri);
33
        $this->_setOptions();
34
    }
35
 
36
    protected function _setOptions() {
37
        curl_setopt($this->handler, CURLOPT_RETURNTRANSFER, true);
38
        curl_setopt($this->handler, CURLOPT_FOLLOWLOCATION, true);
39
        curl_setopt($this->handler, CURLOPT_USERAGENT, self::DEFAULT_USER_AGENT);
40
    }
41
 
42
    /**
43
     * Set the URI
44
     * @param $uri
45
     */
46
    public function setUri($uri) {
47
        $this->handler = curl_init($uri);
48
        $this->_setOptions();
49
    }
50
 
51
    /**
52
     * Receive the response with full headers
53
     * @param boolean $value
54
     */
55
    public function setHeaders($value = true) {
56
        curl_setopt($this->handler, CURLOPT_HEADER, $value);
57
    }
58
 
59
    /**
60
     * Set the HTTP request method
61
     * @param string $method
62
     */
63
    public function setMethod($method = self::GET) {
64
        switch ($method) {
65
            case self::GET :
66
                curl_setopt($this->handler, CURLOPT_HTTPGET, true);
67
                break;
68
            case self::POST :
69
                curl_setopt($this->handler, CURLOPT_POST, true);
70
                break;
71
            case self::DELETE :
72
                curl_setopt($this->handler, CURLOPT_CUSTOMREQUEST, self::DELETE);
73
                break;
74
            default:
75
                throw new CurlHttpClientException('Method not supported');
76
        }
77
    }
78
 
79
    /**
80
     * Add a new post param to the set
81
     * @param string $name
82
     * @param mixed $value
83
     */
84
    public function setPostParam($name, $value) {
85
        $this->postParams[$name] = $value;
86
        curl_setopt($this->handler, CURLOPT_POSTFIELDS, $this->postParams);
87
    }
88
 
89
    /**
90
     * Get the response
91
     * @return string
92
     */
93
    public function getResponse() {
94
        $response = curl_exec($this->handler);
95
        curl_close($this->handler);
96
 
97
        return $response;
98
    }
99
 
100
    /**
101
     * Extract the headers from a response string
102
     *
103
     * @param string $response
104
     * @return mixed[]
105
     */
106
    protected function extractHeaders($response) {
107
        $headers = array();
108
 
109
        // First, split body and headers
110
        $parts = preg_split('|(?:\r?\n){2}|m', $response_str, 2);
111
        if (!$parts[0]) return $headers;
112
 
113
        // Split headers part to lines
114
        $lines = explode("\n", $parts[0]);
115
        unset($parts);
116
        $last_header = null;
117
 
118
        foreach($lines as $line) {
119
            $line = trim($line, "\r\n");
120
            if ($line == "") break;
121
 
122
            // Locate headers like 'Location: ...' and 'Location:...' (note the missing space)
123
            if (preg_match("|^([\w-]+):\s*(.+)|", $line, $m)) {
124
                unset($last_header);
125
                $h_name = strtolower($m[1]);
126
                $h_value = $m[2];
127
 
128
                if (isset($headers[$h_name])) {
129
                    if (! is_array($headers[$h_name])) {
130
                        $headers[$h_name] = array($headers[$h_name]);
131
                    }
132
 
133
                    $headers[$h_name][] = $h_value;
134
                } else {
135
                    $headers[$h_name] = $h_value;
136
                }
137
                $last_header = $h_name;
138
            } else if (preg_match("|^\s+(.+)$|", $line, $m) && $last_header !== null) {
139
                if (is_array($headers[$last_header])) {
140
                    end($headers[$last_header]);
141
                    $last_header_key = key($headers[$last_header]);
142
                    $headers[$last_header][$last_header_key] .= $m[1];
143
                } else {
144
                    $headers[$last_header] .= $m[1];
145
                }
146
            }
147
        }
148
 
149
        return $headers;
150
    }
151
}
152
 
153
class CurlHttpClientException extends Exception {}