27 #require_once 'Zend/Loader.php'; 33 #require_once 'Zend/Uri.php'; 39 #require_once 'Zend/Http/Client/Adapter/Interface.php'; 45 #require_once 'Zend/Http/Header/HeaderValue.php'; 51 #require_once 'Zend/Http/Response.php'; 56 #require_once 'Zend/Http/Response/Stream.php'; 125 'strictredirects' =>
false,
126 'useragent' =>
'Zend_Http_Client',
128 'adapter' =>
'Zend_Http_Client_Adapter_Socket',
129 'httpversion' => self::HTTP_1,
130 'keepalive' =>
false,
131 'storeresponse' =>
true,
133 'output_stream' =>
false,
134 'encodecookies' =>
true,
135 'rfc3986_strict' =>
false 296 $this->_queryBracketsEscaped = version_compare(phpversion(),
'5.1.3',
'>=');
308 if (
$uri instanceof Zend_Uri_Http) {
315 if (!
$uri instanceof Zend_Uri_Http) {
317 #require_once 'Zend/Http/Client/Exception.php'; 322 if (
$uri->getUsername() &&
$uri->getPassword()) {
327 if (!
$uri->getPort()) {
328 $uri->setPort((
$uri->getScheme() ==
'https' ? 443 : 80));
342 public function getUri($as_string =
false)
344 if ($as_string && $this->uri instanceof Zend_Uri_Http) {
345 return $this->uri->__toString();
365 #require_once 'Zend/Http/Client/Exception.php'; 369 foreach (
$config as $k => $v) {
370 $this->config[strtolower($k)] = $v;
375 $this->adapter->setConfig(
$config);
394 if (! preg_match(
'/^[^\x00-\x1f\x7f-\xff\(\)<>@,;:\\\\"\/\[\]\?={}\s]+$/',
$method)) {
395 #require_once 'Zend/Http/Client/Exception.php'; 404 && $this->enctype ===
null 436 if (is_array(
$name)) {
437 foreach (
$name as $k => $v) {
453 if ($this->config[
'strict'] && (! preg_match(
'/^[a-zA-Z0-9-]+$/',
$name))) {
454 #require_once 'Zend/Http/Client/Exception.php'; 458 $normalized_name = strtolower(
$name);
462 unset($this->headers[$normalized_name]);
473 $this->headers[$normalized_name] = array(
$name,
$value);
489 $key = strtolower($key);
490 if (isset($this->headers[$key])) {
491 return $this->headers[$key][1];
506 if (is_array(
$name)) {
507 foreach (
$name as $k => $v)
525 if (is_array(
$name)) {
526 foreach (
$name as $k => $v)
554 if (isset($this->body_field_order[
$name]))
555 unset($this->body_field_order[
$name]);
563 if (isset($parray[
$name])) unset($parray[
$name]);
609 if ($this->uri instanceof Zend_Uri_Http) {
610 $this->
getUri()->setUsername(
'');
611 $this->
getUri()->setPassword(
'');
616 if (! defined(
'self::AUTH_' . strtoupper(
$type))) {
618 #require_once 'Zend/Http/Client/Exception.php'; 623 'user' => (
string)
$user,
624 'password' => (
string) $password,
651 $this->cookiejar =
null;
654 #require_once 'Zend/Http/Client/Exception.php'; 684 if (is_array($cookie)) {
685 foreach ($cookie as $c => $v) {
696 if (
$value !==
null && $this->config[
'encodecookies']) {
700 if (isset($this->cookiejar)) {
702 $this->cookiejar->addCookie($cookie);
706 $this->config[
'encodecookies']);
707 $this->cookiejar->addCookie($cookie);
711 $name = $cookie->getName();
712 $value = $cookie->getValue();
716 if (preg_match(
"/[=,; \t\r\n\013\014]/", $cookie)) {
718 #require_once 'Zend/Http/Client/Exception.php'; 724 if (! isset($this->headers[
'cookie'])) {
725 $this->headers[
'cookie'] = array(
'Cookie',
'');
727 $this->headers[
'cookie'][1] .= $cookie .
'=' .
$value .
'; ';
755 if (
$data ===
null) {
758 #require_once 'Zend/Http/Client/Exception.php'; 770 $this->files[] = array(
771 'formname' => $formname,
772 'filename' => basename($filename),
811 $this->raw_post_data =
$data;
815 $stat = @fstat(
$data);
817 $this->
setHeaders(self::CONTENT_LENGTH, $stat[
'size']);
838 $this->_unmaskStatus = (BOOL)
$status;
867 $this->paramsGet = array();
868 $this->paramsPost = array();
869 $this->files = array();
870 $this->raw_post_data =
null;
871 $this->enctype =
null;
874 $this->headers = array();
875 $this->last_request =
null;
876 $this->last_response =
null;
879 if (isset($this->headers[strtolower(self::CONTENT_TYPE)])) {
880 unset($this->headers[strtolower(self::CONTENT_TYPE)]);
882 if (isset($this->headers[strtolower(self::CONTENT_LENGTH)])) {
883 unset($this->headers[strtolower(self::CONTENT_LENGTH)]);
930 #require_once 'Zend/Http/Client/Exception.php'; 939 #require_once 'Zend/Http/Client/Exception.php'; 946 $this->adapter->setConfig(
$config);
956 if (
null === $this->adapter) {
971 $this->
setConfig(array(
"output_stream" => $streamfile));
981 return $this->config[
"output_stream"];
991 $this->_stream_name = $this->config[
'output_stream'];
992 if(!is_string($this->_stream_name)) {
994 $this->_stream_name = tempnam(isset($this->config[
'stream_tmp_dir'])?$this->config[
'stream_tmp_dir']:sys_get_temp_dir(),
998 if (
false === ($fp = @
fopen($this->_stream_name,
"w+b"))) {
1000 $this->adapter->close();
1002 #require_once 'Zend/Http/Client/Exception.php'; 1018 if (! $this->uri instanceof Zend_Uri_Http) {
1020 #require_once 'Zend/Http/Client/Exception.php'; 1027 $this->redirectCounter = 0;
1031 if ($this->adapter ==
null) {
1039 if (! empty($this->paramsGet)) {
1044 $query .= http_build_query($this->paramsGet,
null,
'&');
1045 if ($this->config[
'rfc3986_strict']) {
1051 if ($this->_queryBracketsEscaped) {
1052 $query = preg_replace(
'/%5B(?:[0-9]|[1-9][0-9]+)%5D=/',
'=',
$query);
1054 $query = preg_replace(
'/\\[(?:[0-9]|[1-9][0-9]+)\\]=/',
'=',
$query);
1067 #require_once 'Zend/Http/Client/Exception.php'; 1072 $this->adapter->connect(
$uri->getHost(),
$uri->getPort(),
1073 (
$uri->getScheme() ==
'https' ? true :
false));
1075 if($this->config[
'output_stream']) {
1078 $this->adapter->setOutputStream($stream);
1081 #require_once 'Zend/Http/Client/Exception.php'; 1086 $this->last_request = $this->adapter->write($this->method,
1092 #require_once 'Zend/Http/Client/Exception.php'; 1096 if($this->config[
'output_stream']) {
1097 $streamMetaData = stream_get_meta_data($stream);
1098 if ($streamMetaData[
'seekable']) {
1102 $this->adapter->setOutputStream(
null);
1104 $response->setStreamName($this->_stream_name);
1105 if(!is_string($this->config[
'output_stream'])) {
1113 if ($this->config[
'storeresponse']) {
1118 if (isset($this->cookiejar)) {
1119 $this->cookiejar->addCookiesFromResponse(
$response,
$uri, $this->config[
'encodecookies']);
1127 $location = trim($location);
1132 ((! $this->config[
'strictredirects']) && (
$response->getStatus() == 302 ||
1140 if (($scheme = substr($location, 0, 6)) && ($scheme ==
'http:/' || $scheme ==
'https:')) {
1142 $this->
setUri($location);
1147 if (strpos($location,
'?') !==
false) {
1148 list($location,
$query) = explode(
'?', $location, 2);
1152 $this->uri->setQuery(
$query);
1155 if(strpos($location,
'/') === 0) {
1156 $this->uri->setPath($location);
1161 $path = $this->uri->getPath();
1163 $this->uri->setPath(
$path .
'/' . $location);
1173 }
while ($this->redirectCounter < $this->config[
'maxredirects']);
1188 if (! isset($this->headers[
'host'])) {
1189 $host = $this->uri->getHost();
1192 if (! (($this->uri->getScheme() ==
'http' && $this->uri->getPort() == 80) ||
1193 ($this->uri->getScheme() ==
'https' && $this->uri->getPort() == 443))) {
1194 $host .=
':' . $this->uri->getPort();
1201 if (! isset($this->headers[
'connection'])) {
1202 if (! $this->config[
'keepalive']) {
1209 if (! isset($this->headers[
'accept-encoding'])) {
1211 $headers[] =
'Accept-encoding: gzip, deflate';
1213 $headers[] =
'Accept-encoding: identity';
1218 if (($this->method == self::POST || $this->method == self::PUT) &&
1219 (! isset($this->headers[strtolower(self::CONTENT_TYPE)]) && isset($this->enctype))) {
1225 if (! isset($this->headers[
'user-agent']) && isset($this->config[
'useragent'])) {
1226 $headers[] =
"User-Agent: {$this->config['useragent']}";
1230 if (is_array($this->auth)) {
1232 $headers[] =
"Authorization: {$auth}";
1236 if (isset($this->cookiejar)) {
1237 $cookstr = $this->cookiejar->getMatchingCookies($this->uri,
1246 foreach ($this->headers as $header) {
1267 if ($this->method == self::TRACE) {
1271 if (isset($this->raw_post_data) &&
is_resource($this->raw_post_data)) {
1277 ((
int)
ini_get(
'mbstring.func_overload')) & 2) {
1279 $mbIntEnc = mb_internal_encoding();
1280 mb_internal_encoding(
'ASCII');
1284 if (isset($this->raw_post_data)) {
1285 $this->
setHeaders(self::CONTENT_LENGTH, strlen($this->raw_post_data));
1286 if (isset($mbIntEnc)) {
1287 mb_internal_encoding($mbIntEnc);
1296 if (count ($this->files) > 0) {
1301 if (count($this->paramsPost) > 0 || count($this->files) > 0) {
1302 switch($this->enctype) {
1305 $boundary =
'---ZENDHTTPCLIENT-' . md5(microtime());
1306 $this->
setHeaders(self::CONTENT_TYPE, self::ENC_FORMDATA .
"; boundary={$boundary}");
1309 foreach ($this->body_field_order as $fieldName=>$fieldType) {
1310 switch ($fieldType) {
1312 foreach ($this->files as $file) {
1313 if ($file[
'formname']===$fieldName) {
1314 $fhead = array(self::CONTENT_TYPE => $file[
'ctype']);
1315 $body .=
self::encodeFormData($boundary, $file[
'formname'], $file[
'data'], $file[
'filename'], $fhead);
1320 if (isset($this->paramsPost[$fieldName])) {
1321 if (is_array($this->paramsPost[$fieldName])) {
1323 foreach ($flattened as $pp) {
1334 $body .=
"--{$boundary}--\r\n";
1339 $this->
setHeaders(self::CONTENT_TYPE, self::ENC_URLENCODED);
1340 $body = http_build_query($this->paramsPost,
'',
'&');
1344 if (isset($mbIntEnc)) {
1345 mb_internal_encoding($mbIntEnc);
1349 #require_once 'Zend/Http/Client/Exception.php'; 1351 " Please use Zend_Http_Client::setRawData to send this kind of content.");
1357 if ($body || $this->method == self::POST || $this->method == self::PUT) {
1358 $this->
setHeaders(self::CONTENT_LENGTH, strlen($body));
1361 if (isset($mbIntEnc)) {
1362 mb_internal_encoding($mbIntEnc);
1388 trigger_error(
"The " . __METHOD__ .
" method is deprecated and will be dropped in 2.0.",
1391 if (! is_array($parray)) {
1394 $parameters = array();
1403 $name .= ($urlencode ?
'%5B%5D' :
'[]');
1404 foreach (
$value as $subval) {
1406 $subval = urlencode($subval);
1408 $parameters[] = array(
$name, $subval);
1441 if (self::$_fileInfoDb ===
null) {
1442 self::$_fileInfoDb = @finfo_open(FILEINFO_MIME);
1445 if (self::$_fileInfoDb) {
1446 $type = finfo_file(self::$_fileInfoDb, $file);
1455 $type =
'application/octet-stream';
1473 $ret =
"--{$boundary}\r\n" .
1474 'Content-Disposition: form-data; name="' .
$name .
'"';
1477 $ret .=
'; filename="' . $filename .
'"';
1481 foreach (
$headers as $hname => $hvalue) {
1482 $ret .=
"{$hname}: {$hvalue}\r\n";
1486 $ret .=
"{$value}\r\n";
1509 if (strpos(
$user,
':') !==
false) {
1511 #require_once 'Zend/Http/Client/Exception.php'; 1515 $authHeader =
'Basic ' . base64_encode(
$user .
':' . $password);
1526 #require_once 'Zend/Http/Client/Exception.php'; 1549 if (! is_array($parray)) {
1553 $parameters = array();
1559 if (is_int(
$name)) {
1569 $parameters = array_merge($parameters, self::_flattenParametersArray(
$value, $key));
1572 $parameters[] = array($key,
$value);
1588 if (is_array(
$value) && $recurse) {
1600 if (! is_string(
$value) && (! is_object(
$value) || ! method_exists(
$value,
'__toString'))) {
1601 #require_once 'Zend/Http/Exception.php';
static fromStream($response_str, $stream)
elseif(isset( $params[ 'redirect_parent']))
setFileUpload($filename, $formname, $data=null, $ctype=null)
static loadClass($class, $dirs=null)
setHeaders($name, $value=null)
_detectFileMimeType($file)
setCookie($cookie, $value=null)
static factory($uri='http', $className=null)
setParameterPost($name, $value=null)
_setParameter($type, $name, $value)
setRawData($data, $enctype=null)
setConfig($config=array())
static encodeFormData($boundary, $name, $value, $filename=null, $headers=array())
static fromString($response_str)
setEncType($enctype=self::ENC_URLENCODED)
setStream($streamfile=true)
static _flattenParametersArray($parray, $prefix=null)
static fromString($cookieStr, $refUri=null, $encodeValue=true)
const COOKIE_STRING_CONCAT
setUnmaskStatus($status=true)
setMethod($method=self::GET)
_getParametersRecursive($parray, $urlencode=false)
setCookieJar($cookiejar=true)
static encodeAuthHeader($user, $password, $type=self::AUTH_BASIC)
__construct($uri=null, $config=null)
setParameterGet($name, $value=null)
_validateHeaderValue($value, $recurse=true)
resetParameters($clearAll=false)
setAuth($user, $password='', $type=self::AUTH_BASIC)
if(!isset($_GET['name'])) $name