2
0

Client.php 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Http
  17. * @subpackage Client
  18. * @version $Id$
  19. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  20. * @license http://framework.zend.com/license/new-bsd New BSD License
  21. */
  22. /**
  23. * @see Zend_Loader
  24. */
  25. require_once 'Zend/Loader.php';
  26. /**
  27. * @see Zend_Uri
  28. */
  29. require_once 'Zend/Uri.php';
  30. /**
  31. * @see Zend_Http_Client_Adapter_Interface
  32. */
  33. require_once 'Zend/Http/Client/Adapter/Interface.php';
  34. /**
  35. * @see Zend_Http_Response
  36. */
  37. require_once 'Zend/Http/Response.php';
  38. /**
  39. * @see Zend_Http_Response_Stream
  40. */
  41. require_once 'Zend/Http/Response/Stream.php';
  42. /**
  43. * Zend_Http_Client is an implementation of an HTTP client in PHP. The client
  44. * supports basic features like sending different HTTP requests and handling
  45. * redirections, as well as more advanced features like proxy settings, HTTP
  46. * authentication and cookie persistence (using a Zend_Http_CookieJar object)
  47. *
  48. * @todo Implement proxy settings
  49. * @category Zend
  50. * @package Zend_Http
  51. * @subpackage Client
  52. * @throws Zend_Http_Client_Exception
  53. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  54. * @license http://framework.zend.com/license/new-bsd New BSD License
  55. */
  56. class Zend_Http_Client
  57. {
  58. /**
  59. * HTTP request methods
  60. */
  61. const GET = 'GET';
  62. const POST = 'POST';
  63. const PUT = 'PUT';
  64. const HEAD = 'HEAD';
  65. const DELETE = 'DELETE';
  66. const TRACE = 'TRACE';
  67. const OPTIONS = 'OPTIONS';
  68. const CONNECT = 'CONNECT';
  69. const MERGE = 'MERGE';
  70. /**
  71. * Supported HTTP Authentication methods
  72. */
  73. const AUTH_BASIC = 'basic';
  74. //const AUTH_DIGEST = 'digest'; <-- not implemented yet
  75. /**
  76. * HTTP protocol versions
  77. */
  78. const HTTP_1 = '1.1';
  79. const HTTP_0 = '1.0';
  80. /**
  81. * Content attributes
  82. */
  83. const CONTENT_TYPE = 'Content-Type';
  84. const CONTENT_LENGTH = 'Content-Length';
  85. /**
  86. * POST data encoding methods
  87. */
  88. const ENC_URLENCODED = 'application/x-www-form-urlencoded';
  89. const ENC_FORMDATA = 'multipart/form-data';
  90. /**
  91. * Configuration array, set using the constructor or using ::setConfig()
  92. *
  93. * @var array
  94. */
  95. protected $config = array(
  96. 'maxredirects' => 5,
  97. 'strictredirects' => false,
  98. 'useragent' => 'Zend_Http_Client',
  99. 'timeout' => 10,
  100. 'adapter' => 'Zend_Http_Client_Adapter_Socket',
  101. 'httpversion' => self::HTTP_1,
  102. 'keepalive' => false,
  103. 'storeresponse' => true,
  104. 'strict' => true,
  105. 'output_stream' => false,
  106. 'encodecookies' => true,
  107. );
  108. /**
  109. * The adapter used to perform the actual connection to the server
  110. *
  111. * @var Zend_Http_Client_Adapter_Interface
  112. */
  113. protected $adapter = null;
  114. /**
  115. * Request URI
  116. *
  117. * @var Zend_Uri_Http
  118. */
  119. protected $uri = null;
  120. /**
  121. * Associative array of request headers
  122. *
  123. * @var array
  124. */
  125. protected $headers = array();
  126. /**
  127. * HTTP request method
  128. *
  129. * @var string
  130. */
  131. protected $method = self::GET;
  132. /**
  133. * Associative array of GET parameters
  134. *
  135. * @var array
  136. */
  137. protected $paramsGet = array();
  138. /**
  139. * Associative array of POST parameters
  140. *
  141. * @var array
  142. */
  143. protected $paramsPost = array();
  144. /**
  145. * Request body content type (for POST requests)
  146. *
  147. * @var string
  148. */
  149. protected $enctype = null;
  150. /**
  151. * The raw post data to send. Could be set by setRawData($data, $enctype).
  152. *
  153. * @var string
  154. */
  155. protected $raw_post_data = null;
  156. /**
  157. * HTTP Authentication settings
  158. *
  159. * Expected to be an associative array with this structure:
  160. * $this->auth = array('user' => 'username', 'password' => 'password', 'type' => 'basic')
  161. * Where 'type' should be one of the supported authentication types (see the AUTH_*
  162. * constants), for example 'basic' or 'digest'.
  163. *
  164. * If null, no authentication will be used.
  165. *
  166. * @var array|null
  167. */
  168. protected $auth;
  169. /**
  170. * File upload arrays (used in POST requests)
  171. *
  172. * An associative array, where each element is of the format:
  173. * 'name' => array('filename.txt', 'text/plain', 'This is the actual file contents')
  174. *
  175. * @var array
  176. */
  177. protected $files = array();
  178. /**
  179. * The client's cookie jar
  180. *
  181. * @var Zend_Http_CookieJar
  182. */
  183. protected $cookiejar = null;
  184. /**
  185. * The last HTTP request sent by the client, as string
  186. *
  187. * @var string
  188. */
  189. protected $last_request = null;
  190. /**
  191. * The last HTTP response received by the client
  192. *
  193. * @var Zend_Http_Response
  194. */
  195. protected $last_response = null;
  196. /**
  197. * Redirection counter
  198. *
  199. * @var int
  200. */
  201. protected $redirectCounter = 0;
  202. /**
  203. * Fileinfo magic database resource
  204. *
  205. * This variable is populated the first time _detectFileMimeType is called
  206. * and is then reused on every call to this method
  207. *
  208. * @var resource
  209. */
  210. static protected $_fileInfoDb = null;
  211. /**
  212. * Constructor method. Will create a new HTTP client. Accepts the target
  213. * URL and optionally configuration array.
  214. *
  215. * @param Zend_Uri_Http|string $uri
  216. * @param array $config Configuration key-value pairs.
  217. */
  218. public function __construct($uri = null, $config = null)
  219. {
  220. if ($uri !== null) {
  221. $this->setUri($uri);
  222. }
  223. if ($config !== null) {
  224. $this->setConfig($config);
  225. }
  226. }
  227. /**
  228. * Set the URI for the next request
  229. *
  230. * @param Zend_Uri_Http|string $uri
  231. * @return Zend_Http_Client
  232. * @throws Zend_Http_Client_Exception
  233. */
  234. public function setUri($uri)
  235. {
  236. if (is_string($uri)) {
  237. $uri = Zend_Uri::factory($uri);
  238. }
  239. if (!$uri instanceof Zend_Uri_Http) {
  240. /** @see Zend_Http_Client_Exception */
  241. require_once 'Zend/Http/Client/Exception.php';
  242. throw new Zend_Http_Client_Exception('Passed parameter is not a valid HTTP URI.');
  243. }
  244. // Set auth if username and password has been specified in the uri
  245. if ($uri->getUsername() && $uri->getPassword()) {
  246. $this->setAuth($uri->getUsername(), $uri->getPassword());
  247. }
  248. // We have no ports, set the defaults
  249. if (! $uri->getPort()) {
  250. $uri->setPort(($uri->getScheme() == 'https' ? 443 : 80));
  251. }
  252. $this->uri = $uri;
  253. return $this;
  254. }
  255. /**
  256. * Get the URI for the next request
  257. *
  258. * @param boolean $as_string If true, will return the URI as a string
  259. * @return Zend_Uri_Http|string
  260. */
  261. public function getUri($as_string = false)
  262. {
  263. if ($as_string && $this->uri instanceof Zend_Uri_Http) {
  264. return $this->uri->__toString();
  265. } else {
  266. return $this->uri;
  267. }
  268. }
  269. /**
  270. * Set configuration parameters for this HTTP client
  271. *
  272. * @param Zend_Config | array $config
  273. * @return Zend_Http_Client
  274. * @throws Zend_Http_Client_Exception
  275. */
  276. public function setConfig($config = array())
  277. {
  278. if ($config instanceof Zend_Config) {
  279. $config = $config->toArray();
  280. } elseif (! is_array($config)) {
  281. /** @see Zend_Http_Client_Exception */
  282. require_once 'Zend/Http/Client/Exception.php';
  283. throw new Zend_Http_Client_Exception('Array or Zend_Config object expected, got ' . gettype($config));
  284. }
  285. foreach ($config as $k => $v) {
  286. $this->config[strtolower($k)] = $v;
  287. }
  288. // Pass configuration options to the adapter if it exists
  289. if ($this->adapter instanceof Zend_Http_Client_Adapter_Interface) {
  290. $this->adapter->setConfig($config);
  291. }
  292. return $this;
  293. }
  294. /**
  295. * Set the next request's method
  296. *
  297. * Validated the passed method and sets it. If we have files set for
  298. * POST requests, and the new method is not POST, the files are silently
  299. * dropped.
  300. *
  301. * @param string $method
  302. * @return Zend_Http_Client
  303. * @throws Zend_Http_Client_Exception
  304. */
  305. public function setMethod($method = self::GET)
  306. {
  307. if (! preg_match('/^[^\x00-\x1f\x7f-\xff\(\)<>@,;:\\\\"\/\[\]\?={}\s]+$/', $method)) {
  308. /** @see Zend_Http_Client_Exception */
  309. require_once 'Zend/Http/Client/Exception.php';
  310. throw new Zend_Http_Client_Exception("'{$method}' is not a valid HTTP request method.");
  311. }
  312. if ($method == self::POST && $this->enctype === null) {
  313. $this->setEncType(self::ENC_URLENCODED);
  314. }
  315. $this->method = $method;
  316. return $this;
  317. }
  318. /**
  319. * Set one or more request headers
  320. *
  321. * This function can be used in several ways to set the client's request
  322. * headers:
  323. * 1. By providing two parameters: $name as the header to set (e.g. 'Host')
  324. * and $value as it's value (e.g. 'www.example.com').
  325. * 2. By providing a single header string as the only parameter
  326. * e.g. 'Host: www.example.com'
  327. * 3. By providing an array of headers as the first parameter
  328. * e.g. array('host' => 'www.example.com', 'x-foo: bar'). In This case
  329. * the function will call itself recursively for each array item.
  330. *
  331. * @param string|array $name Header name, full header string ('Header: value')
  332. * or an array of headers
  333. * @param mixed $value Header value or null
  334. * @return Zend_Http_Client
  335. * @throws Zend_Http_Client_Exception
  336. */
  337. public function setHeaders($name, $value = null)
  338. {
  339. // If we got an array, go recursive!
  340. if (is_array($name)) {
  341. foreach ($name as $k => $v) {
  342. if (is_string($k)) {
  343. $this->setHeaders($k, $v);
  344. } else {
  345. $this->setHeaders($v, null);
  346. }
  347. }
  348. } else {
  349. // Check if $name needs to be split
  350. if ($value === null && (strpos($name, ':') > 0)) {
  351. list($name, $value) = explode(':', $name, 2);
  352. }
  353. // Make sure the name is valid if we are in strict mode
  354. if ($this->config['strict'] && (! preg_match('/^[a-zA-Z0-9-]+$/', $name))) {
  355. /** @see Zend_Http_Client_Exception */
  356. require_once 'Zend/Http/Client/Exception.php';
  357. throw new Zend_Http_Client_Exception("{$name} is not a valid HTTP header name");
  358. }
  359. $normalized_name = strtolower($name);
  360. // If $value is null or false, unset the header
  361. if ($value === null || $value === false) {
  362. unset($this->headers[$normalized_name]);
  363. // Else, set the header
  364. } else {
  365. // Header names are stored lowercase internally.
  366. if (is_string($value)) {
  367. $value = trim($value);
  368. }
  369. $this->headers[$normalized_name] = array($name, $value);
  370. }
  371. }
  372. return $this;
  373. }
  374. /**
  375. * Get the value of a specific header
  376. *
  377. * Note that if the header has more than one value, an array
  378. * will be returned.
  379. *
  380. * @param string $key
  381. * @return string|array|null The header value or null if it is not set
  382. */
  383. public function getHeader($key)
  384. {
  385. $key = strtolower($key);
  386. if (isset($this->headers[$key])) {
  387. return $this->headers[$key][1];
  388. } else {
  389. return null;
  390. }
  391. }
  392. /**
  393. * Set a GET parameter for the request. Wrapper around _setParameter
  394. *
  395. * @param string|array $name
  396. * @param string $value
  397. * @return Zend_Http_Client
  398. */
  399. public function setParameterGet($name, $value = null)
  400. {
  401. if (is_array($name)) {
  402. foreach ($name as $k => $v)
  403. $this->_setParameter('GET', $k, $v);
  404. } else {
  405. $this->_setParameter('GET', $name, $value);
  406. }
  407. return $this;
  408. }
  409. /**
  410. * Set a POST parameter for the request. Wrapper around _setParameter
  411. *
  412. * @param string|array $name
  413. * @param string $value
  414. * @return Zend_Http_Client
  415. */
  416. public function setParameterPost($name, $value = null)
  417. {
  418. if (is_array($name)) {
  419. foreach ($name as $k => $v)
  420. $this->_setParameter('POST', $k, $v);
  421. } else {
  422. $this->_setParameter('POST', $name, $value);
  423. }
  424. return $this;
  425. }
  426. /**
  427. * Set a GET or POST parameter - used by SetParameterGet and SetParameterPost
  428. *
  429. * @param string $type GET or POST
  430. * @param string $name
  431. * @param string $value
  432. * @return null
  433. */
  434. protected function _setParameter($type, $name, $value)
  435. {
  436. $parray = array();
  437. $type = strtolower($type);
  438. switch ($type) {
  439. case 'get':
  440. $parray = &$this->paramsGet;
  441. break;
  442. case 'post':
  443. $parray = &$this->paramsPost;
  444. break;
  445. }
  446. if ($value === null) {
  447. if (isset($parray[$name])) unset($parray[$name]);
  448. } else {
  449. $parray[$name] = $value;
  450. }
  451. }
  452. /**
  453. * Get the number of redirections done on the last request
  454. *
  455. * @return int
  456. */
  457. public function getRedirectionsCount()
  458. {
  459. return $this->redirectCounter;
  460. }
  461. /**
  462. * Set HTTP authentication parameters
  463. *
  464. * $type should be one of the supported types - see the self::AUTH_*
  465. * constants.
  466. *
  467. * To enable authentication:
  468. * <code>
  469. * $this->setAuth('shahar', 'secret', Zend_Http_Client::AUTH_BASIC);
  470. * </code>
  471. *
  472. * To disable authentication:
  473. * <code>
  474. * $this->setAuth(false);
  475. * </code>
  476. *
  477. * @see http://www.faqs.org/rfcs/rfc2617.html
  478. * @param string|false $user User name or false disable authentication
  479. * @param string $password Password
  480. * @param string $type Authentication type
  481. * @return Zend_Http_Client
  482. * @throws Zend_Http_Client_Exception
  483. */
  484. public function setAuth($user, $password = '', $type = self::AUTH_BASIC)
  485. {
  486. // If we got false or null, disable authentication
  487. if ($user === false || $user === null) {
  488. $this->auth = null;
  489. // Clear the auth information in the uri instance as well
  490. if ($this->uri instanceof Zend_Uri_Http) {
  491. $this->getUri()->setUsername('');
  492. $this->getUri()->setPassword('');
  493. }
  494. // Else, set up authentication
  495. } else {
  496. // Check we got a proper authentication type
  497. if (! defined('self::AUTH_' . strtoupper($type))) {
  498. /** @see Zend_Http_Client_Exception */
  499. require_once 'Zend/Http/Client/Exception.php';
  500. throw new Zend_Http_Client_Exception("Invalid or not supported authentication type: '$type'");
  501. }
  502. $this->auth = array(
  503. 'user' => (string) $user,
  504. 'password' => (string) $password,
  505. 'type' => $type
  506. );
  507. }
  508. return $this;
  509. }
  510. /**
  511. * Set the HTTP client's cookie jar.
  512. *
  513. * A cookie jar is an object that holds and maintains cookies across HTTP requests
  514. * and responses.
  515. *
  516. * @param Zend_Http_CookieJar|boolean $cookiejar Existing cookiejar object, true to create a new one, false to disable
  517. * @return Zend_Http_Client
  518. * @throws Zend_Http_Client_Exception
  519. */
  520. public function setCookieJar($cookiejar = true)
  521. {
  522. if (! class_exists('Zend_Http_CookieJar')) {
  523. require_once 'Zend/Http/CookieJar.php';
  524. }
  525. if ($cookiejar instanceof Zend_Http_CookieJar) {
  526. $this->cookiejar = $cookiejar;
  527. } elseif ($cookiejar === true) {
  528. $this->cookiejar = new Zend_Http_CookieJar();
  529. } elseif (! $cookiejar) {
  530. $this->cookiejar = null;
  531. } else {
  532. /** @see Zend_Http_Client_Exception */
  533. require_once 'Zend/Http/Client/Exception.php';
  534. throw new Zend_Http_Client_Exception('Invalid parameter type passed as CookieJar');
  535. }
  536. return $this;
  537. }
  538. /**
  539. * Return the current cookie jar or null if none.
  540. *
  541. * @return Zend_Http_CookieJar|null
  542. */
  543. public function getCookieJar()
  544. {
  545. return $this->cookiejar;
  546. }
  547. /**
  548. * Add a cookie to the request. If the client has no Cookie Jar, the cookies
  549. * will be added directly to the headers array as "Cookie" headers.
  550. *
  551. * @param Zend_Http_Cookie|string $cookie
  552. * @param string|null $value If "cookie" is a string, this is the cookie value.
  553. * @return Zend_Http_Client
  554. * @throws Zend_Http_Client_Exception
  555. */
  556. public function setCookie($cookie, $value = null)
  557. {
  558. if (! class_exists('Zend_Http_Cookie')) {
  559. require_once 'Zend/Http/Cookie.php';
  560. }
  561. if (is_array($cookie)) {
  562. foreach ($cookie as $c => $v) {
  563. if (is_string($c)) {
  564. $this->setCookie($c, $v);
  565. } else {
  566. $this->setCookie($v);
  567. }
  568. }
  569. return $this;
  570. }
  571. if ($value !== null && $this->config['encodecookies']) {
  572. $value = urlencode($value);
  573. }
  574. if (isset($this->cookiejar)) {
  575. if ($cookie instanceof Zend_Http_Cookie) {
  576. $this->cookiejar->addCookie($cookie);
  577. } elseif (is_string($cookie) && $value !== null) {
  578. $cookie = Zend_Http_Cookie::fromString("{$cookie}={$value}",
  579. $this->uri,
  580. $this->config['encodecookies']);
  581. $this->cookiejar->addCookie($cookie);
  582. }
  583. } else {
  584. if ($cookie instanceof Zend_Http_Cookie) {
  585. $name = $cookie->getName();
  586. $value = $cookie->getValue();
  587. $cookie = $name;
  588. }
  589. if (preg_match("/[=,; \t\r\n\013\014]/", $cookie)) {
  590. /** @see Zend_Http_Client_Exception */
  591. require_once 'Zend/Http/Client/Exception.php';
  592. throw new Zend_Http_Client_Exception("Cookie name cannot contain these characters: =,; \t\r\n\013\014 ({$cookie})");
  593. }
  594. $value = addslashes($value);
  595. if (! isset($this->headers['cookie'])) {
  596. $this->headers['cookie'] = array('Cookie', '');
  597. }
  598. $this->headers['cookie'][1] .= $cookie . '=' . $value . '; ';
  599. }
  600. return $this;
  601. }
  602. /**
  603. * Set a file to upload (using a POST request)
  604. *
  605. * Can be used in two ways:
  606. *
  607. * 1. $data is null (default): $filename is treated as the name if a local file which
  608. * will be read and sent. Will try to guess the content type using mime_content_type().
  609. * 2. $data is set - $filename is sent as the file name, but $data is sent as the file
  610. * contents and no file is read from the file system. In this case, you need to
  611. * manually set the Content-Type ($ctype) or it will default to
  612. * application/octet-stream.
  613. *
  614. * @param string $filename Name of file to upload, or name to save as
  615. * @param string $formname Name of form element to send as
  616. * @param string $data Data to send (if null, $filename is read and sent)
  617. * @param string $ctype Content type to use (if $data is set and $ctype is
  618. * null, will be application/octet-stream)
  619. * @return Zend_Http_Client
  620. * @throws Zend_Http_Client_Exception
  621. */
  622. public function setFileUpload($filename, $formname, $data = null, $ctype = null)
  623. {
  624. if ($data === null) {
  625. if (($data = @file_get_contents($filename)) === false) {
  626. /** @see Zend_Http_Client_Exception */
  627. require_once 'Zend/Http/Client/Exception.php';
  628. throw new Zend_Http_Client_Exception("Unable to read file '{$filename}' for upload");
  629. }
  630. if (! $ctype) {
  631. $ctype = $this->_detectFileMimeType($filename);
  632. }
  633. }
  634. // Force enctype to multipart/form-data
  635. $this->setEncType(self::ENC_FORMDATA);
  636. $this->files[] = array(
  637. 'formname' => $formname,
  638. 'filename' => basename($filename),
  639. 'ctype' => $ctype,
  640. 'data' => $data
  641. );
  642. return $this;
  643. }
  644. /**
  645. * Set the encoding type for POST data
  646. *
  647. * @param string $enctype
  648. * @return Zend_Http_Client
  649. */
  650. public function setEncType($enctype = self::ENC_URLENCODED)
  651. {
  652. $this->enctype = $enctype;
  653. return $this;
  654. }
  655. /**
  656. * Set the raw (already encoded) POST data.
  657. *
  658. * This function is here for two reasons:
  659. * 1. For advanced user who would like to set their own data, already encoded
  660. * 2. For backwards compatibilty: If someone uses the old post($data) method.
  661. * this method will be used to set the encoded data.
  662. *
  663. * $data can also be stream (such as file) from which the data will be read.
  664. *
  665. * @param string|resource $data
  666. * @param string $enctype
  667. * @return Zend_Http_Client
  668. */
  669. public function setRawData($data, $enctype = null)
  670. {
  671. $this->raw_post_data = $data;
  672. $this->setEncType($enctype);
  673. if (is_resource($data)) {
  674. // We've got stream data
  675. $stat = @fstat($data);
  676. if($stat) {
  677. $this->setHeaders(self::CONTENT_LENGTH, $stat['size']);
  678. }
  679. }
  680. return $this;
  681. }
  682. /**
  683. * Clear all GET and POST parameters
  684. *
  685. * Should be used to reset the request parameters if the client is
  686. * used for several concurrent requests.
  687. *
  688. * clearAll parameter controls if we clean just parameters or also
  689. * headers and last_*
  690. *
  691. * @param bool $clearAll Should all data be cleared?
  692. * @return Zend_Http_Client
  693. */
  694. public function resetParameters($clearAll = false)
  695. {
  696. // Reset parameter data
  697. $this->paramsGet = array();
  698. $this->paramsPost = array();
  699. $this->files = array();
  700. $this->raw_post_data = null;
  701. if($clearAll) {
  702. $this->headers = array();
  703. $this->last_request = null;
  704. $this->last_response = null;
  705. } else {
  706. // Clear outdated headers
  707. if (isset($this->headers[strtolower(self::CONTENT_TYPE)])) {
  708. unset($this->headers[strtolower(self::CONTENT_TYPE)]);
  709. }
  710. if (isset($this->headers[strtolower(self::CONTENT_LENGTH)])) {
  711. unset($this->headers[strtolower(self::CONTENT_LENGTH)]);
  712. }
  713. }
  714. return $this;
  715. }
  716. /**
  717. * Get the last HTTP request as string
  718. *
  719. * @return string
  720. */
  721. public function getLastRequest()
  722. {
  723. return $this->last_request;
  724. }
  725. /**
  726. * Get the last HTTP response received by this client
  727. *
  728. * If $config['storeresponse'] is set to false, or no response was
  729. * stored yet, will return null
  730. *
  731. * @return Zend_Http_Response or null if none
  732. */
  733. public function getLastResponse()
  734. {
  735. return $this->last_response;
  736. }
  737. /**
  738. * Load the connection adapter
  739. *
  740. * While this method is not called more than one for a client, it is
  741. * seperated from ->request() to preserve logic and readability
  742. *
  743. * @param Zend_Http_Client_Adapter_Interface|string $adapter
  744. * @return null
  745. * @throws Zend_Http_Client_Exception
  746. */
  747. public function setAdapter($adapter)
  748. {
  749. if (is_string($adapter)) {
  750. if (!class_exists($adapter)) {
  751. try {
  752. require_once 'Zend/Loader.php';
  753. Zend_Loader::loadClass($adapter);
  754. } catch (Zend_Exception $e) {
  755. /** @see Zend_Http_Client_Exception */
  756. require_once 'Zend/Http/Client/Exception.php';
  757. throw new Zend_Http_Client_Exception("Unable to load adapter '$adapter': {$e->getMessage()}", 0, $e);
  758. }
  759. }
  760. $adapter = new $adapter;
  761. }
  762. if (! $adapter instanceof Zend_Http_Client_Adapter_Interface) {
  763. /** @see Zend_Http_Client_Exception */
  764. require_once 'Zend/Http/Client/Exception.php';
  765. throw new Zend_Http_Client_Exception('Passed adapter is not a HTTP connection adapter');
  766. }
  767. $this->adapter = $adapter;
  768. $config = $this->config;
  769. unset($config['adapter']);
  770. $this->adapter->setConfig($config);
  771. }
  772. /**
  773. * Load the connection adapter
  774. *
  775. * @return Zend_Http_Client_Adapter_Interface $adapter
  776. */
  777. public function getAdapter()
  778. {
  779. return $this->adapter;
  780. }
  781. /**
  782. * Set streaming for received data
  783. *
  784. * @param string|boolean $streamfile Stream file, true for temp file, false/null for no streaming
  785. * @return Zend_Http_Client
  786. */
  787. public function setStream($streamfile = true)
  788. {
  789. $this->setConfig(array("output_stream" => $streamfile));
  790. return $this;
  791. }
  792. /**
  793. * Get status of streaming for received data
  794. * @return boolean|string
  795. */
  796. public function getStream()
  797. {
  798. return $this->config["output_stream"];
  799. }
  800. /**
  801. * Create temporary stream
  802. *
  803. * @return resource
  804. */
  805. protected function _openTempStream()
  806. {
  807. $this->_stream_name = $this->config['output_stream'];
  808. if(!is_string($this->_stream_name)) {
  809. // If name is not given, create temp name
  810. $this->_stream_name = tempnam(isset($this->config['stream_tmp_dir'])?$this->config['stream_tmp_dir']:sys_get_temp_dir(),
  811. 'Zend_Http_Client');
  812. }
  813. $fp = fopen($this->_stream_name, "w+b");
  814. if(!$fp) {
  815. $this->close();
  816. require_once 'Zend/Http/Client/Exception.php';
  817. throw new Zend_Http_Client_Exception("Could not open temp file $name");
  818. }
  819. return $fp;
  820. }
  821. /**
  822. * Send the HTTP request and return an HTTP response object
  823. *
  824. * @param string $method
  825. * @return Zend_Http_Response
  826. * @throws Zend_Http_Client_Exception
  827. */
  828. public function request($method = null)
  829. {
  830. if (! $this->uri instanceof Zend_Uri_Http) {
  831. /** @see Zend_Http_Client_Exception */
  832. require_once 'Zend/Http/Client/Exception.php';
  833. throw new Zend_Http_Client_Exception('No valid URI has been passed to the client');
  834. }
  835. if ($method) {
  836. $this->setMethod($method);
  837. }
  838. $this->redirectCounter = 0;
  839. $response = null;
  840. // Make sure the adapter is loaded
  841. if ($this->adapter == null) {
  842. $this->setAdapter($this->config['adapter']);
  843. }
  844. // Send the first request. If redirected, continue.
  845. do {
  846. // Clone the URI and add the additional GET parameters to it
  847. $uri = clone $this->uri;
  848. if (! empty($this->paramsGet)) {
  849. $query = $uri->getQuery();
  850. if (! empty($query)) {
  851. $query .= '&';
  852. }
  853. $query .= http_build_query($this->paramsGet, null, '&');
  854. $uri->setQuery($query);
  855. }
  856. $body = $this->_prepareBody();
  857. $headers = $this->_prepareHeaders();
  858. // check that adapter supports streaming before using it
  859. if(is_resource($body) && !($this->adapter instanceof Zend_Http_Client_Adapter_Stream)) {
  860. /** @see Zend_Http_Client_Exception */
  861. require_once 'Zend/Http/Client/Exception.php';
  862. throw new Zend_Http_Client_Exception('Adapter does not support streaming');
  863. }
  864. // Open the connection, send the request and read the response
  865. $this->adapter->connect($uri->getHost(), $uri->getPort(),
  866. ($uri->getScheme() == 'https' ? true : false));
  867. if($this->config['output_stream']) {
  868. if($this->adapter instanceof Zend_Http_Client_Adapter_Stream) {
  869. $stream = $this->_openTempStream();
  870. $this->adapter->setOutputStream($stream);
  871. } else {
  872. /** @see Zend_Http_Client_Exception */
  873. require_once 'Zend/Http/Client/Exception.php';
  874. throw new Zend_Http_Client_Exception('Adapter does not support streaming');
  875. }
  876. }
  877. $this->last_request = $this->adapter->write($this->method,
  878. $uri, $this->config['httpversion'], $headers, $body);
  879. $response = $this->adapter->read();
  880. if (! $response) {
  881. /** @see Zend_Http_Client_Exception */
  882. require_once 'Zend/Http/Client/Exception.php';
  883. throw new Zend_Http_Client_Exception('Unable to read response, or response is empty');
  884. }
  885. if($this->config['output_stream']) {
  886. rewind($stream);
  887. // cleanup the adapter
  888. $this->adapter->setOutputStream(null);
  889. $response = Zend_Http_Response_Stream::fromStream($response, $stream);
  890. $response->setStreamName($this->_stream_name);
  891. if(!is_string($this->config['output_stream'])) {
  892. // we used temp name, will need to clean up
  893. $response->setCleanup(true);
  894. }
  895. } else {
  896. $response = Zend_Http_Response::fromString($response);
  897. }
  898. if ($this->config['storeresponse']) {
  899. $this->last_response = $response;
  900. }
  901. // Load cookies into cookie jar
  902. if (isset($this->cookiejar)) {
  903. $this->cookiejar->addCookiesFromResponse($response, $uri);
  904. }
  905. // If we got redirected, look for the Location header
  906. if ($response->isRedirect() && ($location = $response->getHeader('location'))) {
  907. // Check whether we send the exact same request again, or drop the parameters
  908. // and send a GET request
  909. if ($response->getStatus() == 303 ||
  910. ((! $this->config['strictredirects']) && ($response->getStatus() == 302 ||
  911. $response->getStatus() == 301))) {
  912. $this->resetParameters();
  913. $this->setMethod(self::GET);
  914. }
  915. // If we got a well formed absolute URI
  916. if (Zend_Uri_Http::check($location)) {
  917. $this->setHeaders('host', null);
  918. $this->setUri($location);
  919. } else {
  920. // Split into path and query and set the query
  921. if (strpos($location, '?') !== false) {
  922. list($location, $query) = explode('?', $location, 2);
  923. } else {
  924. $query = '';
  925. }
  926. $this->uri->setQuery($query);
  927. // Else, if we got just an absolute path, set it
  928. if(strpos($location, '/') === 0) {
  929. $this->uri->setPath($location);
  930. // Else, assume we have a relative path
  931. } else {
  932. // Get the current path directory, removing any trailing slashes
  933. $path = $this->uri->getPath();
  934. $path = rtrim(substr($path, 0, strrpos($path, '/')), "/");
  935. $this->uri->setPath($path . '/' . $location);
  936. }
  937. }
  938. ++$this->redirectCounter;
  939. } else {
  940. // If we didn't get any location, stop redirecting
  941. break;
  942. }
  943. } while ($this->redirectCounter < $this->config['maxredirects']);
  944. return $response;
  945. }
  946. /**
  947. * Prepare the request headers
  948. *
  949. * @return array
  950. */
  951. protected function _prepareHeaders()
  952. {
  953. $headers = array();
  954. // Set the host header
  955. if (! isset($this->headers['host'])) {
  956. $host = $this->uri->getHost();
  957. // If the port is not default, add it
  958. if (! (($this->uri->getScheme() == 'http' && $this->uri->getPort() == 80) ||
  959. ($this->uri->getScheme() == 'https' && $this->uri->getPort() == 443))) {
  960. $host .= ':' . $this->uri->getPort();
  961. }
  962. $headers[] = "Host: {$host}";
  963. }
  964. // Set the connection header
  965. if (! isset($this->headers['connection'])) {
  966. if (! $this->config['keepalive']) {
  967. $headers[] = "Connection: close";
  968. }
  969. }
  970. // Set the Accept-encoding header if not set - depending on whether
  971. // zlib is available or not.
  972. if (! isset($this->headers['accept-encoding'])) {
  973. if (function_exists('gzinflate')) {
  974. $headers[] = 'Accept-encoding: gzip, deflate';
  975. } else {
  976. $headers[] = 'Accept-encoding: identity';
  977. }
  978. }
  979. // Set the Content-Type header
  980. if ($this->method == self::POST &&
  981. (! isset($this->headers[strtolower(self::CONTENT_TYPE)]) && isset($this->enctype))) {
  982. $headers[] = self::CONTENT_TYPE . ': ' . $this->enctype;
  983. }
  984. // Set the user agent header
  985. if (! isset($this->headers['user-agent']) && isset($this->config['useragent'])) {
  986. $headers[] = "User-Agent: {$this->config['useragent']}";
  987. }
  988. // Set HTTP authentication if needed
  989. if (is_array($this->auth)) {
  990. $auth = self::encodeAuthHeader($this->auth['user'], $this->auth['password'], $this->auth['type']);
  991. $headers[] = "Authorization: {$auth}";
  992. }
  993. // Load cookies from cookie jar
  994. if (isset($this->cookiejar)) {
  995. $cookstr = $this->cookiejar->getMatchingCookies($this->uri,
  996. true, Zend_Http_CookieJar::COOKIE_STRING_CONCAT);
  997. if ($cookstr) {
  998. $headers[] = "Cookie: {$cookstr}";
  999. }
  1000. }
  1001. // Add all other user defined headers
  1002. foreach ($this->headers as $header) {
  1003. list($name, $value) = $header;
  1004. if (is_array($value)) {
  1005. $value = implode(', ', $value);
  1006. }
  1007. $headers[] = "$name: $value";
  1008. }
  1009. return $headers;
  1010. }
  1011. /**
  1012. * Prepare the request body (for POST and PUT requests)
  1013. *
  1014. * @return string
  1015. * @throws Zend_Http_Client_Exception
  1016. */
  1017. protected function _prepareBody()
  1018. {
  1019. // According to RFC2616, a TRACE request should not have a body.
  1020. if ($this->method == self::TRACE) {
  1021. return '';
  1022. }
  1023. if (isset($this->raw_post_data) && is_resource($this->raw_post_data)) {
  1024. return $this->raw_post_data;
  1025. }
  1026. // If mbstring overloads substr and strlen functions, we have to
  1027. // override it's internal encoding
  1028. if (function_exists('mb_internal_encoding') &&
  1029. ((int) ini_get('mbstring.func_overload')) & 2) {
  1030. $mbIntEnc = mb_internal_encoding();
  1031. mb_internal_encoding('ASCII');
  1032. }
  1033. // If we have raw_post_data set, just use it as the body.
  1034. if (isset($this->raw_post_data)) {
  1035. $this->setHeaders(self::CONTENT_LENGTH, strlen($this->raw_post_data));
  1036. if (isset($mbIntEnc)) {
  1037. mb_internal_encoding($mbIntEnc);
  1038. }
  1039. return $this->raw_post_data;
  1040. }
  1041. $body = '';
  1042. // If we have files to upload, force enctype to multipart/form-data
  1043. if (count ($this->files) > 0) {
  1044. $this->setEncType(self::ENC_FORMDATA);
  1045. }
  1046. // If we have POST parameters or files, encode and add them to the body
  1047. if (count($this->paramsPost) > 0 || count($this->files) > 0) {
  1048. switch($this->enctype) {
  1049. case self::ENC_FORMDATA:
  1050. // Encode body as multipart/form-data
  1051. $boundary = '---ZENDHTTPCLIENT-' . md5(microtime());
  1052. $this->setHeaders(self::CONTENT_TYPE, self::ENC_FORMDATA . "; boundary={$boundary}");
  1053. // Get POST parameters and encode them
  1054. $params = self::_flattenParametersArray($this->paramsPost);
  1055. foreach ($params as $pp) {
  1056. $body .= self::encodeFormData($boundary, $pp[0], $pp[1]);
  1057. }
  1058. // Encode files
  1059. foreach ($this->files as $file) {
  1060. $fhead = array(self::CONTENT_TYPE => $file['ctype']);
  1061. $body .= self::encodeFormData($boundary, $file['formname'], $file['data'], $file['filename'], $fhead);
  1062. }
  1063. $body .= "--{$boundary}--\r\n";
  1064. break;
  1065. case self::ENC_URLENCODED:
  1066. // Encode body as application/x-www-form-urlencoded
  1067. $this->setHeaders(self::CONTENT_TYPE, self::ENC_URLENCODED);
  1068. $body = http_build_query($this->paramsPost, '', '&');
  1069. break;
  1070. default:
  1071. if (isset($mbIntEnc)) {
  1072. mb_internal_encoding($mbIntEnc);
  1073. }
  1074. /** @see Zend_Http_Client_Exception */
  1075. require_once 'Zend/Http/Client/Exception.php';
  1076. throw new Zend_Http_Client_Exception("Cannot handle content type '{$this->enctype}' automatically." .
  1077. " Please use Zend_Http_Client::setRawData to send this kind of content.");
  1078. break;
  1079. }
  1080. }
  1081. // Set the Content-Length if we have a body or if request is POST/PUT
  1082. if ($body || $this->method == self::POST || $this->method == self::PUT) {
  1083. $this->setHeaders(self::CONTENT_LENGTH, strlen($body));
  1084. }
  1085. if (isset($mbIntEnc)) {
  1086. mb_internal_encoding($mbIntEnc);
  1087. }
  1088. return $body;
  1089. }
  1090. /**
  1091. * Helper method that gets a possibly multi-level parameters array (get or
  1092. * post) and flattens it.
  1093. *
  1094. * The method returns an array of (key, value) pairs (because keys are not
  1095. * necessarily unique. If one of the parameters in as array, it will also
  1096. * add a [] suffix to the key.
  1097. *
  1098. * This method is deprecated since Zend Framework 1.9 in favour of
  1099. * self::_flattenParametersArray() and will be dropped in 2.0
  1100. *
  1101. * @deprecated since 1.9
  1102. *
  1103. * @param array $parray The parameters array
  1104. * @param bool $urlencode Whether to urlencode the name and value
  1105. * @return array
  1106. */
  1107. protected function _getParametersRecursive($parray, $urlencode = false)
  1108. {
  1109. // Issue a deprecated notice
  1110. trigger_error("The " . __METHOD__ . " method is deprecated and will be dropped in 2.0.",
  1111. E_USER_NOTICE);
  1112. if (! is_array($parray)) {
  1113. return $parray;
  1114. }
  1115. $parameters = array();
  1116. foreach ($parray as $name => $value) {
  1117. if ($urlencode) {
  1118. $name = urlencode($name);
  1119. }
  1120. // If $value is an array, iterate over it
  1121. if (is_array($value)) {
  1122. $name .= ($urlencode ? '%5B%5D' : '[]');
  1123. foreach ($value as $subval) {
  1124. if ($urlencode) {
  1125. $subval = urlencode($subval);
  1126. }
  1127. $parameters[] = array($name, $subval);
  1128. }
  1129. } else {
  1130. if ($urlencode) {
  1131. $value = urlencode($value);
  1132. }
  1133. $parameters[] = array($name, $value);
  1134. }
  1135. }
  1136. return $parameters;
  1137. }
  1138. /**
  1139. * Attempt to detect the MIME type of a file using available extensions
  1140. *
  1141. * This method will try to detect the MIME type of a file. If the fileinfo
  1142. * extension is available, it will be used. If not, the mime_magic
  1143. * extension which is deprected but is still available in many PHP setups
  1144. * will be tried.
  1145. *
  1146. * If neither extension is available, the default application/octet-stream
  1147. * MIME type will be returned
  1148. *
  1149. * @param string $file File path
  1150. * @return string MIME type
  1151. */
  1152. protected function _detectFileMimeType($file)
  1153. {
  1154. $type = null;
  1155. // First try with fileinfo functions
  1156. if (function_exists('finfo_open')) {
  1157. if (self::$_fileInfoDb === null) {
  1158. self::$_fileInfoDb = @finfo_open(FILEINFO_MIME);
  1159. }
  1160. if (self::$_fileInfoDb) {
  1161. $type = finfo_file(self::$_fileInfoDb, $file);
  1162. }
  1163. } elseif (function_exists('mime_content_type')) {
  1164. $type = mime_content_type($file);
  1165. }
  1166. // Fallback to the default application/octet-stream
  1167. if (! $type) {
  1168. $type = 'application/octet-stream';
  1169. }
  1170. return $type;
  1171. }
  1172. /**
  1173. * Encode data to a multipart/form-data part suitable for a POST request.
  1174. *
  1175. * @param string $boundary
  1176. * @param string $name
  1177. * @param mixed $value
  1178. * @param string $filename
  1179. * @param array $headers Associative array of optional headers @example ("Content-Transfer-Encoding" => "binary")
  1180. * @return string
  1181. */
  1182. public static function encodeFormData($boundary, $name, $value, $filename = null, $headers = array()) {
  1183. $ret = "--{$boundary}\r\n" .
  1184. 'Content-Disposition: form-data; name="' . $name .'"';
  1185. if ($filename) {
  1186. $ret .= '; filename="' . $filename . '"';
  1187. }
  1188. $ret .= "\r\n";
  1189. foreach ($headers as $hname => $hvalue) {
  1190. $ret .= "{$hname}: {$hvalue}\r\n";
  1191. }
  1192. $ret .= "\r\n";
  1193. $ret .= "{$value}\r\n";
  1194. return $ret;
  1195. }
  1196. /**
  1197. * Create a HTTP authentication "Authorization:" header according to the
  1198. * specified user, password and authentication method.
  1199. *
  1200. * @see http://www.faqs.org/rfcs/rfc2617.html
  1201. * @param string $user
  1202. * @param string $password
  1203. * @param string $type
  1204. * @return string
  1205. * @throws Zend_Http_Client_Exception
  1206. */
  1207. public static function encodeAuthHeader($user, $password, $type = self::AUTH_BASIC)
  1208. {
  1209. $authHeader = null;
  1210. switch ($type) {
  1211. case self::AUTH_BASIC:
  1212. // In basic authentication, the user name cannot contain ":"
  1213. if (strpos($user, ':') !== false) {
  1214. /** @see Zend_Http_Client_Exception */
  1215. require_once 'Zend/Http/Client/Exception.php';
  1216. throw new Zend_Http_Client_Exception("The user name cannot contain ':' in 'Basic' HTTP authentication");
  1217. }
  1218. $authHeader = 'Basic ' . base64_encode($user . ':' . $password);
  1219. break;
  1220. //case self::AUTH_DIGEST:
  1221. /**
  1222. * @todo Implement digest authentication
  1223. */
  1224. // break;
  1225. default:
  1226. /** @see Zend_Http_Client_Exception */
  1227. require_once 'Zend/Http/Client/Exception.php';
  1228. throw new Zend_Http_Client_Exception("Not a supported HTTP authentication type: '$type'");
  1229. }
  1230. return $authHeader;
  1231. }
  1232. /**
  1233. * Convert an array of parameters into a flat array of (key, value) pairs
  1234. *
  1235. * Will flatten a potentially multi-dimentional array of parameters (such
  1236. * as POST parameters) into a flat array of (key, value) paris. In case
  1237. * of multi-dimentional arrays, square brackets ([]) will be added to the
  1238. * key to indicate an array.
  1239. *
  1240. * @since 1.9
  1241. *
  1242. * @param array $parray
  1243. * @param string $prefix
  1244. * @return array
  1245. */
  1246. static protected function _flattenParametersArray($parray, $prefix = null)
  1247. {
  1248. if (! is_array($parray)) {
  1249. return $parray;
  1250. }
  1251. $parameters = array();
  1252. foreach($parray as $name => $value) {
  1253. // Calculate array key
  1254. if ($prefix) {
  1255. if (is_int($name)) {
  1256. $key = $prefix . '[]';
  1257. } else {
  1258. $key = $prefix . "[$name]";
  1259. }
  1260. } else {
  1261. $key = $name;
  1262. }
  1263. if (is_array($value)) {
  1264. $parameters = array_merge($parameters, self::_flattenParametersArray($value, $key));
  1265. } else {
  1266. $parameters[] = array($key, $value);
  1267. }
  1268. }
  1269. return $parameters;
  1270. }
  1271. }