Create the following file:
lib/internal/Magento/Framework/HTTP/Client/Curl.php
Add the below code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 |
<?php /** * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ namespace Magento\Framework\HTTP\Client; /** * Class to work with HTTP protocol using curl library * * @author Magento Core Team <core@magentocommerce.com> * @SuppressWarnings(PHPMD.ExcessiveClassComplexity) */ class Curl implements \Magento\Framework\HTTP\ClientInterface { /** * Max supported protocol by curl CURL_SSLVERSION_TLSv1_2 * @var int */ private $sslVersion; /** * Hostname * @var string */ protected $_host = 'localhost'; /** * Port * @var int */ protected $_port = 80; /** * Stream resource * @var object */ protected $_sock = null; /** * Request headers * @var array */ protected $_headers = []; /** * Fields for POST method - hash * @var array */ protected $_postFields = []; /** * Request cookies * @var array */ protected $_cookies = []; /** * Response headers * @var array */ protected $_responseHeaders = []; /** * Response body * @var string */ protected $_responseBody = ''; /** * Response status * @var int */ protected $_responseStatus = 0; /** * Request timeout * @var int type */ protected $_timeout = 300; /** * TODO * @var int */ protected $_redirectCount = 0; /** * Curl * @var resource */ protected $_ch; /** * User overrides options hash * Are applied before curl_exec * * @var array */ protected $_curlUserOptions = []; /** * Header count, used while parsing headers * in CURL callback function * @var int */ protected $_headerCount = 0; /** * Set request timeout, msec * * @param int $value * @return void */ public function setTimeout($value) { $this->_timeout = (int)$value; } /** * @param int|null $sslVersion */ public function __construct($sslVersion = null) { $this->sslVersion = $sslVersion; } /** * Set headers from hash * * @param array $headers * @return void */ public function setHeaders($headers) { $this->_headers = $headers; } /** * Add header * * @param string $name name, ex. "Location" * @param string $value value ex. "http://google.com" * @return void */ public function addHeader($name, $value) { $this->_headers[$name] = $value; } /** * Remove specified header * * @param string $name * @return void */ public function removeHeader($name) { unset($this->_headers[$name]); } /** * Authorization: Basic header * * Login credentials support * * @param string $login username * @param string $pass password * @return void */ public function setCredentials($login, $pass) { $val = base64_encode("{$login}:{$pass}"); $this->addHeader("Authorization", "Basic {$val}"); } /** * Add cookie * * @param string $name * @param string $value * @return void */ public function addCookie($name, $value) { $this->_cookies[$name] = $value; } /** * Remove cookie * * @param string $name * @return void */ public function removeCookie($name) { unset($this->_cookies[$name]); } /** * Set cookies array * * @param array $cookies * @return void */ public function setCookies($cookies) { $this->_cookies = $cookies; } /** * Clear cookies * * @return void */ public function removeCookies() { $this->setCookies([]); } /** * Make GET request * * @param string $uri uri relative to host, ex. "/index.php" * @return void */ public function get($uri) { $this->makeRequest("GET", $uri); } /** * Make POST request * * String type was added to parameter $param in order to support sending JSON or XML requests. * This feature was added base on Community Pull Request https://github.com/magento/magento2/pull/8373 * * @param string $uri * @param array|string $params * @return void * * @see \Magento\Framework\HTTP\Client#post($uri, $params) */ public function post($uri, $params) { $this->makeRequest("POST", $uri, $params); } /** * Get response headers * * @return array */ public function getHeaders() { return $this->_responseHeaders; } /** * Get response body * * @return string */ public function getBody() { return $this->_responseBody; } /** * Get cookies response hash * * @return array */ public function getCookies() { if (empty($this->_responseHeaders['Set-Cookie'])) { return []; } $out = []; foreach ($this->_responseHeaders['Set-Cookie'] as $row) { $values = explode("; ", $row); $c = count($values); if (!$c) { continue; } list($key, $val) = explode("=", $values[0]); if ($val === null) { continue; } $out[trim($key)] = trim($val); } return $out; } /** * Get cookies array with details * (domain, expire time etc) * * @return array */ public function getCookiesFull() { if (empty($this->_responseHeaders['Set-Cookie'])) { return []; } $out = []; foreach ($this->_responseHeaders['Set-Cookie'] as $row) { $values = explode("; ", $row); $c = count($values); if (!$c) { continue; } list($key, $val) = explode("=", $values[0]); if ($val === null) { continue; } $out[trim($key)] = ['value' => trim($val)]; array_shift($values); $c--; if (!$c) { continue; } for ($i = 0; $i < $c; $i++) { list($subkey, $val) = explode("=", $values[$i]); $out[trim($key)][trim($subkey)] = trim($val); } } return $out; } /** * Get response status code * * @see lib\Magento\Framework\HTTP\Client#getStatus() * * @return int */ public function getStatus() { return $this->_responseStatus; } /** * Make request * * String type was added to parameter $param in order to support sending JSON or XML requests. * This feature was added base on Community Pull Request https://github.com/magento/magento2/pull/8373 * * @param string $method * @param string $uri * @param array|string $params - use $params as a string in case of JSON or XML POST request. * * @return void * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) */ protected function makeRequest($method, $uri, $params = []) { $this->_ch = curl_init(); $this->curlOption(CURLOPT_URL, $uri); if ($method == 'POST') { $this->curlOption(CURLOPT_POST, 1); $this->curlOption(CURLOPT_POSTFIELDS, is_array($params) ? http_build_query($params) : $params); } elseif ($method == "GET") { $this->curlOption(CURLOPT_HTTPGET, 1); } else { $this->curlOption(CURLOPT_CUSTOMREQUEST, $method); } if (count($this->_headers)) { $heads = []; foreach ($this->_headers as $k => $v) { $heads[] = $k . ': ' . $v; } $this->curlOption(CURLOPT_HTTPHEADER, $heads); } if (count($this->_cookies)) { $cookies = []; foreach ($this->_cookies as $k => $v) { $cookies[] = "{$k}={$v}"; } $this->curlOption(CURLOPT_COOKIE, implode(";", $cookies)); } if ($this->_timeout) { $this->curlOption(CURLOPT_TIMEOUT, $this->_timeout); } if ($this->_port != 80) { $this->curlOption(CURLOPT_PORT, $this->_port); } $this->curlOption(CURLOPT_RETURNTRANSFER, 1); $this->curlOption(CURLOPT_HEADERFUNCTION, [$this, 'parseHeaders']); if ($this->sslVersion !== null) { $this->curlOption(CURLOPT_SSLVERSION, $this->sslVersion); } if (count($this->_curlUserOptions)) { foreach ($this->_curlUserOptions as $k => $v) { $this->curlOption($k, $v); } } $this->_headerCount = 0; $this->_responseHeaders = []; $this->_responseBody = curl_exec($this->_ch); $err = curl_errno($this->_ch); if ($err) { $this->doError(curl_error($this->_ch)); } curl_close($this->_ch); } /** * Throw error exception * * @param string $string * @return void * @throws \Exception */ public function doError($string) { throw new \Exception($string); } /** * Parse headers - CURL callback function * * @param resource $ch curl handle, not needed * @param string $data * @return int * @throws \Exception * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ protected function parseHeaders($ch, $data) { if ($this->_headerCount == 0) { $line = explode(" ", trim($data), 3); if (count($line) < 2) { $this->doError("Invalid response line returned from server: " . $data); } $this->_responseStatus = (int)$line[1]; } else { $name = $value = ''; $out = explode(": ", trim($data), 2); if (count($out) == 2) { $name = $out[0]; $value = $out[1]; } if (strlen($name)) { if ("Set-Cookie" == $name) { if (!isset($this->_responseHeaders[$name])) { $this->_responseHeaders[$name] = []; } $this->_responseHeaders[$name][] = $value; } else { $this->_responseHeaders[$name] = $value; } } } $this->_headerCount++; return strlen($data); } /** * Set curl option directly * * @param string $name * @param string $value * @return void */ protected function curlOption($name, $value) { curl_setopt($this->_ch, $name, $value); } /** * Set curl options array directly * * @param array $arr * @return void */ protected function curlOptions($arr) { curl_setopt_array($this->_ch, $arr); } /** * Set CURL options overrides array * * @param array $arr * @return void */ public function setOptions($arr) { $this->_curlUserOptions = $arr; } /** * Set curl option * * @param string $name * @param string $value * @return void */ public function setOption($name, $value) { $this->_curlUserOptions[$name] = $value; } } |
If you want get query string params in controller file,...
Create di.xml and add the below code Magemonkey/Redirect/etc/frontend/di.xml [crayon-62838e781124a226369219/] Create...
You can try below code to change local date to...
Step 1: First you need to add registration.php file in...
Step1 : Override message.js in current theme file on the...