24 require_once
'Net/URL2.php';
29 require_once
'HTTP/Request2/Exception.php';
86 private static $_fileinfoDb;
125 'adapter' =>
'HTTP_Request2_Adapter_Socket',
126 'connect_timeout' => 10,
128 'use_brackets' =>
true,
129 'protocol_version' =>
'1.1',
130 'buffer_size' => 16384,
131 'store_body' =>
true,
137 'proxy_password' =>
'',
138 'proxy_auth_scheme' => self::AUTH_BASIC,
139 'proxy_type' =>
'http',
141 'ssl_verify_peer' =>
true,
142 'ssl_verify_host' =>
true,
143 'ssl_cafile' => null,
144 'ssl_capath' => null,
145 'ssl_local_cert' => null,
146 'ssl_passphrase' => null,
148 'digest_compat_ie' =>
false,
150 'follow_redirects' =>
false,
151 'max_redirects' => 5,
152 'strict_redirects' =>
false
216 'user-agent',
'HTTP_Request2/2.2.1 ' .
217 '(http://pear.php.net/package/http_request2) PHP/' . phpversion()
235 if (is_string(
$url)) {
242 'Parameter is not a valid HTTP URL',
247 if (
$url->getUserinfo()) {
248 $username =
$url->getUser();
249 $password =
$url->getPassword();
250 $this->
setAuth(rawurldecode($username), $password? rawurldecode($password):
'');
251 $url->setUserinfo(
'');
253 if (
'' ==
$url->getPath()) {
282 if (preg_match(self::REGEXP_INVALID_TOKEN,
$method)) {
284 "Invalid request method '{$method}'",
359 if (is_array($nameOrConfig)) {
360 foreach ($nameOrConfig as $name => $value) {
364 } elseif (
'proxy' == $nameOrConfig) {
367 'proxy_type' =>
$url->getScheme(),
368 'proxy_host' =>
$url->getHost(),
369 'proxy_port' =>
$url->getPort(),
370 'proxy_user' => rawurldecode(
$url->getUser()),
371 'proxy_password' => rawurldecode(
$url->getPassword())
375 if (!array_key_exists($nameOrConfig, $this->config)) {
377 "Unknown configuration parameter '{$nameOrConfig}'",
381 $this->config[$nameOrConfig] = $value;
398 if (null === $name) {
400 } elseif (!array_key_exists($name, $this->config)) {
402 "Unknown configuration parameter '{$name}'",
406 return $this->config[$name];
418 public function setAuth($user, $password =
'', $scheme = self::AUTH_BASIC)
424 'user' => (
string)$user,
425 'password' => (
string)$password,
474 public function setHeader($name, $value = null, $replace =
true)
476 if (is_array($name)) {
477 foreach ($name as $k => $v) {
485 if (null === $value && strpos($name,
':')) {
486 list($name, $value) = array_map(
'trim', explode(
':', $name, 2));
489 if (preg_match(self::REGEXP_INVALID_TOKEN, $name)) {
491 "Invalid header name '{$name}'",
496 $name = strtolower($name);
497 if (null === $value) {
498 unset($this->headers[$name]);
501 if (is_array($value)) {
502 $value = implode(
', ', array_map(
'trim', $value));
503 } elseif (is_string($value)) {
504 $value = trim($value);
506 if (!isset($this->headers[$name]) || $replace) {
507 $this->headers[$name] = $value;
509 $this->headers[$name] .=
', ' . $value;
550 if (!empty($this->cookieJar)) {
551 $this->cookieJar->store(
552 array(
'name' => $name,
'value' => $value), $this->url
556 $cookie = $name .
'=' . $value;
557 if (preg_match(self::REGEXP_INVALID_COOKIE, $cookie)) {
559 "Invalid cookie: '{$cookie}'",
563 $cookies = empty($this->headers[
'cookie'])?
'': $this->headers[
'cookie'] .
'; ';
564 $this->
setHeader(
'cookie', $cookies . $cookie);
587 if (!$isFilename && !is_resource(
$body)) {
589 $this->body = (string)
$body;
595 $this->body = $fileData[
'fp'];
596 if (empty($this->headers[
'content-type'])) {
597 $this->
setHeader(
'content-type', $fileData[
'type']);
600 $this->postParams = $this->uploads = array();
612 if (self::METHOD_POST == $this->method
613 && (!empty($this->postParams) || !empty($this->uploads))
615 if (0 === strpos($this->headers[
'content-type'],
'application/x-www-form-urlencoded')) {
616 $body = http_build_query($this->postParams,
'',
'&');
618 $body = preg_replace(
'/%5B\d+%5D=/',
'=',
$body);
621 return str_replace(
'%7E',
'~',
$body);
623 } elseif (0 === strpos($this->headers[
'content-type'],
'multipart/form-data')) {
624 require_once
'HTTP/Request2/MultipartBody.php';
626 $this->postParams, $this->uploads, $this->
getConfig(
'use_brackets')
655 $fieldName, $filename, $sendFilename = null, $contentType = null
657 if (!is_array($filename)) {
658 $fileData = $this->
fopenWrapper($filename, empty($contentType));
659 $this->uploads[$fieldName] = array(
660 'fp' => $fileData[
'fp'],
661 'filename' => !empty($sendFilename)? $sendFilename
662 :(is_string($filename)? basename($filename):
'anonymous.blob') ,
663 'size' => $fileData[
'size'],
664 'type' => empty($contentType)? $fileData[
'type']: $contentType
667 $fps = $names = $sizes = $types = array();
668 foreach ($filename as $f) {
673 $fps[] = $fileData[
'fp'];
674 $names[] = !empty($f[1])? $f[1]
675 :(is_string($f[0])? basename($f[0]):
'anonymous.blob');
676 $sizes[] = $fileData[
'size'];
677 $types[] = empty($f[2])? $fileData[
'type']: $f[2];
679 $this->uploads[$fieldName] = array(
680 'fp' => $fps,
'filename' => $names,
'size' => $sizes,
'type' => $types
683 if (empty($this->headers[
'content-type'])
684 ||
'application/x-www-form-urlencoded' == $this->headers[
'content-type']
686 $this->
setHeader(
'content-type',
'multipart/form-data');
702 if (!is_array($name)) {
703 $this->postParams[$name] = $value;
705 foreach ($name as $k => $v) {
709 if (empty($this->headers[
'content-type'])) {
710 $this->
setHeader(
'content-type',
'application/x-www-form-urlencoded');
721 public function attach(SplObserver $observer)
723 foreach ($this->observers as $attached) {
724 if ($attached === $observer) {
728 $this->observers[] = $observer;
736 public function detach(SplObserver $observer)
738 foreach ($this->observers as $key => $attached) {
739 if ($attached === $observer) {
740 unset($this->observers[$key]);
751 foreach ($this->observers as $observer) {
752 $observer->update($this);
767 $this->lastEvent = array(
828 if (!class_exists(
$adapter,
false)) {
829 if (
false === strpos(
$adapter,
'_')) {
833 && preg_match(
'/^HTTP_Request2_Adapter_([a-zA-Z0-9]+)$/',
$adapter)
835 include_once str_replace(
'_', DIRECTORY_SEPARATOR,
$adapter) .
'.php';
837 if (!class_exists(
$adapter,
false)) {
839 "Class {$adapter} not found",
848 'Parameter is not a HTTP request adapter',
872 if (!class_exists(
'HTTP_Request2_CookieJar',
false)) {
873 require_once
'HTTP/Request2/CookieJar.php';
877 $this->cookieJar = $jar;
878 } elseif (
true === $jar) {
879 $this->cookieJar =
new HTTP_Request2_CookieJar();
881 $this->cookieJar = null;
884 'Invalid parameter passed to setCookieJar()',
913 || !in_array(strtolower($this->url->getScheme()), array(
'https',
'http'))
916 'HTTP_Request2 needs an absolute HTTP(S) request URL, '
918 ?
"'" . $this->url->
__toString() .
"'" :
'none')
923 if (empty($this->adapter)) {
928 if ($magicQuotes = get_magic_quotes_runtime()) {
929 set_magic_quotes_runtime(
false);
933 if (extension_loaded(
'mbstring') && (2 & ini_get(
'mbstring.func_overload'))) {
934 $oldEncoding = mb_internal_encoding();
935 mb_internal_encoding(
'8bit');
939 $response = $this->adapter->sendRequest($this);
940 }
catch (Exception $e) {
944 set_magic_quotes_runtime(
true);
946 if (!empty($oldEncoding)) {
947 mb_internal_encoding($oldEncoding);
969 if (!is_string($file) && !is_resource($file)) {
971 "Filename or file pointer resource expected",
976 'fp' => is_string($file)? null: $file,
977 'type' =>
'application/octet-stream',
980 if (is_string($file)) {
981 if (!($fileData[
'fp'] = @fopen($file,
'rb'))) {
982 $error = error_get_last();
988 $fileData[
'type'] = self::detectMimeType($file);
991 if (!($stat = fstat($fileData[
'fp']))) {
996 $fileData[
'size'] = $stat[
'size'];
1015 if (function_exists(
'finfo_open')) {
1016 if (!isset(self::$_fileinfoDb)) {
1017 self::$_fileinfoDb = @finfo_open(FILEINFO_MIME);
1019 if (self::$_fileinfoDb) {
1020 $info = finfo_file(self::$_fileinfoDb, $filename);
1024 if (empty($info) && function_exists(
'mime_content_type')) {
1025 return mime_content_type($filename);
1027 return empty($info)?
'application/octet-stream': $info;
fopenWrapper($file, $detectType=false)
setBody($body, $isFilename=false)
__construct($url=null, $method=self::METHOD_GET, array $config=array())
const REGEXP_INVALID_COOKIE
detach(SplObserver $observer)
attach(SplObserver $observer)
static detectMimeType($filename)
setHeader($name, $value=null, $replace=true)
addPostParameter($name, $value=null)
setConfig($nameOrConfig, $value=null)
setAuth($user, $password= '', $scheme=self::AUTH_BASIC)
const REGEXP_INVALID_TOKEN
setLastEvent($name, $data=null)
addUpload($fieldName, $filename, $sendFilename=null, $contentType=null)
const OPTION_USE_BRACKETS