ZendClient.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. /**
  7. * Magento HTTP Client
  8. *
  9. * @author Magento Core Team <core@magentocommerce.com>
  10. */
  11. namespace Magento\Framework\HTTP;
  12. class ZendClient extends \Zend_Http_Client
  13. {
  14. /**
  15. * Internal flag to allow decoding of request body
  16. *
  17. * @var bool
  18. */
  19. protected $_urlEncodeBody = true;
  20. /**
  21. * @param null|string $uri
  22. * @param null|array $config
  23. */
  24. public function __construct($uri = null, $config = null)
  25. {
  26. $this->config['useragent'] = \Magento\Framework\HTTP\ZendClient::class;
  27. parent::__construct($uri, $config);
  28. }
  29. /**
  30. * @return $this
  31. */
  32. protected function _trySetCurlAdapter()
  33. {
  34. if (extension_loaded('curl')) {
  35. $this->setAdapter(new \Magento\Framework\HTTP\Adapter\Curl());
  36. }
  37. return $this;
  38. }
  39. /**
  40. * @param null|string $method
  41. * @return \Zend_Http_Response
  42. */
  43. public function request($method = null)
  44. {
  45. $this->_trySetCurlAdapter();
  46. return parent::request($method);
  47. }
  48. /**
  49. * Change value of internal flag to disable/enable custom prepare functionality
  50. *
  51. * @param bool $flag
  52. * @return \Magento\Framework\HTTP\ZendClient
  53. */
  54. public function setUrlEncodeBody($flag)
  55. {
  56. $this->_urlEncodeBody = $flag;
  57. return $this;
  58. }
  59. /**
  60. * Adding custom functionality to decode data after
  61. * standard prepare functionality
  62. *
  63. * @return string
  64. */
  65. protected function _prepareBody()
  66. {
  67. $body = parent::_prepareBody();
  68. if (!$this->_urlEncodeBody && $body) {
  69. $body = urldecode($body);
  70. $this->setHeaders('Content-length', strlen($body));
  71. }
  72. return $body;
  73. }
  74. }