20 require_once
"Auth/Yadis/HTTPFetcher.php";
34 return function_exists(
'openssl_open');
37 function get($url, $extra_headers = null)
48 while ($redir && ($off > 0)) {
50 $parts = parse_url($url);
55 if (!array_key_exists(
'port', $parts)) {
56 $specify_port =
false;
57 if ($parts[
'scheme'] ==
'http') {
59 } elseif ($parts[
'scheme'] ==
'https') {
66 if (!array_key_exists(
'path', $parts)) {
70 $host = $parts[
'host'];
72 if ($parts[
'scheme'] ==
'https') {
73 $host =
'ssl://' . $host;
79 "GET ".$parts[
'path'].
80 (array_key_exists(
'query', $parts) ?
81 "?".$parts[
'query'] :
"").
83 "User-Agent: $user_agent",
84 "Host: ".$parts[
'host'].
85 ($specify_port ?
":".$parts[
'port'] :
""),
86 "Port: ".$parts[
'port']);
92 foreach ($extra_headers as $h) {
97 @$sock = fsockopen($host, $parts[
'port'], $errno, $errstr,
99 if ($sock ===
false) {
103 stream_set_timeout($sock, $this->timeout);
105 fputs($sock, implode(
"\r\n", $headers) .
"\r\n\r\n");
109 while (!feof($sock) &&
111 $data .= fgets($sock, 1024);
118 list($headers, $body) = explode(
"\r\n\r\n",
$data, 2);
119 $headers = explode(
"\r\n", $headers);
121 $http_code = explode(
" ", $headers[0]);
122 $code = $http_code[1];
124 if (in_array($code, array(
'301',
'302'))) {
131 $off = $stop - time();
134 $new_headers = array();
136 foreach ($headers as $header) {
137 if (preg_match(
"/:/", $header)) {
138 $parts = explode(
": ", $header, 2);
140 if (count($parts) == 2) {
141 list(
$name, $value) = $parts;
142 $new_headers[
$name] = $value;
151 function post($url, $body, $extra_headers = null)
157 $parts = parse_url($url);
161 $post_path = $parts[
'path'];
162 if (isset($parts[
'query'])) {
163 $post_path .=
'?' . $parts[
'query'];
166 $headers[] =
"POST ".$post_path.
" HTTP/1.0";
167 $headers[] =
"Host: " . $parts[
'host'];
168 $headers[] =
"Content-type: application/x-www-form-urlencoded";
169 $headers[] =
"Content-length: " . strval(strlen($body));
171 if ($extra_headers &&
172 is_array($extra_headers)) {
173 $headers = array_merge($headers, $extra_headers);
177 $all_headers = implode(
"\r\n", $headers);
180 $request = $all_headers .
"\r\n\r\n" . $body;
183 if (!array_key_exists(
'port', $parts)) {
184 if ($parts[
'scheme'] ==
'http') {
186 } elseif ($parts[
'scheme'] ==
'https') {
187 $parts[
'port'] = 443;
193 if ($parts[
'scheme'] ==
'https') {
194 $parts[
'host'] = sprintf(
"ssl://%s", $parts[
'host']);
201 $sock = fsockopen($parts[
'host'], $parts[
'port'], $errno, $errstr,
204 if ($sock ===
false) {
208 stream_set_timeout($sock, $this->timeout);
211 fputs($sock, $request);
215 while (!feof($sock)) {
216 if (
$data = fgets($sock, 128)) {
224 list($headers, $response_body) = explode(
"\r\n\r\n", $response, 2);
226 $headers = explode(
"\r\n", $headers);
231 $http_code = explode(
" ", $headers[0]);
232 $code = $http_code[1];
234 $new_headers = array();
236 foreach ($headers as $header) {
237 if (preg_match(
"/:/", $header)) {
238 list(
$name, $value) = explode(
": ", $header, 2);
239 $new_headers[
$name] = $value;
245 $new_headers, $response_body);