Client.php 46 KB

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