3 * StatusNet, the distributed open-source microblogging tool
5 * Utility class for wrapping Curl
9 * LICENCE: This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU Affero General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Affero General Public License for more details.
19 * You should have received a copy of the GNU Affero General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 * @author Evan Prodromou <evan@status.net>
25 * @copyright 2009 StatusNet, Inc.
26 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
27 * @link http://status.net/
30 if (!defined('STATUSNET')) {
34 define(CURLCLIENT_VERSION, "0.1");
39 * Makes Curl HTTP client calls within our HTTPClient framework
43 * @author Evan Prodromou <evan@status.net>
44 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
45 * @link http://status.net/
48 class CurlClient extends HTTPClient
50 function __construct()
54 function head($url, $headers=null)
56 $ch = curl_init($url);
60 curl_setopt_array($ch,
61 array(CURLOPT_NOBODY => true));
63 if (!is_null($headers)) {
64 curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
67 $result = curl_exec($ch);
71 return $this->parseResults($result);
74 function get($url, $headers=null)
76 $ch = curl_init($url);
80 if (!is_null($headers)) {
81 curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
84 $result = curl_exec($ch);
88 return $this->parseResults($result);
91 function post($url, $headers=null, $body=null)
93 $ch = curl_init($url);
97 curl_setopt($ch, CURLOPT_POST, true);
99 if (!is_null($body)) {
100 curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
103 if (!is_null($headers)) {
104 curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
107 $result = curl_exec($ch);
111 return $this->parseResults($result);
116 curl_setopt_array($ch,
117 array(CURLOPT_USERAGENT => $this->userAgent(),
118 CURLOPT_HEADER => true,
119 CURLOPT_RETURNTRANSFER => true));
124 $version = curl_version();
125 return parent::userAgent() . " CurlClient/".CURLCLIENT_VERSION . " cURL/" . $version['version'];
128 function parseResults($results)
130 $resp = new HTTPResponse();
132 $lines = explode("\r\n", $results);
134 if (preg_match("#^HTTP/1.[01] (\d\d\d) .+$#", $lines[0], $match)) {
135 $resp->code = $match[1];
137 throw Exception("Bad format: initial line is not HTTP status line");
142 for ($i = 1; $i < count($lines); $i++) {
144 if (mb_strlen($l) == 0) {
145 $resp->body = implode("\r\n", array_slice($lines, $i + 1));
148 if (preg_match("#^(\S+):\s+(.*)$#", $l, $match)) {
152 if (array_key_exists($k, $resp->headers)) {
153 if (is_array($resp->headers[$k])) {
154 $resp->headers[$k][] = $v;
156 $resp->headers[$k] = array($resp->headers[$k], $v);
159 $resp->headers[$k] = $v;
162 } else if (preg_match("#^\s+(.*)$#", $l, $match)) {
164 if (is_null($lastk)) {
165 throw Exception("Bad format: initial whitespace in headers");
167 $h =& $resp->headers[$lastk];
170 $h[$n-1] .= $match[1];