Client.php 45 KB

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