From 662cf5eee5ec39d56ff3b6054fabbe1cc7f6d808 Mon Sep 17 00:00:00 2001 From: Ryan Cramer Date: Fri, 22 Apr 2022 14:13:15 -0400 Subject: [PATCH] Add option to WireHttp for setting cookies to use in an http request (currently supported only by the curl http get/post methods) --- wire/core/WireHttp.php | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/wire/core/WireHttp.php b/wire/core/WireHttp.php index 019e23ea..40bba0be 100644 --- a/wire/core/WireHttp.php +++ b/wire/core/WireHttp.php @@ -229,6 +229,14 @@ class WireHttp extends Wire { * */ protected $responseHeaderArrays = array(); + + /** + * Cookies to set for next curl get/post request + * + * @var array + * + */ + protected $setCookies = array(); /** * Error messages @@ -745,6 +753,12 @@ class WireHttp extends Wire { }); curl_setopt($curl, CURLOPT_URL, $url); + + $cookie = empty($options['cookie']) ? $this->setCookies : $options['cookie']; + if(!empty($cookie)) { + if(is_array($cookie)) $cookie = http_build_query($cookie, '', '; ', PHP_QUERY_RFC3986); + if(is_string($cookie) && !empty($cookie)) curl_setopt($curl, CURLOPT_COOKIE, $cookie); + } // custom CURL options provided in $options array if(!empty($options['curl']) && !empty($options['curl']['setopt'])) { @@ -1147,6 +1161,31 @@ class WireHttp extends Wire { public function getHeaders() { return $this->headers; } + + /** + * Set cookie(s) for http GET/POST/etc. request (currently used by curl option only) + * + * ~~~~~ + * $http->setCookie('PHPSESSID', 'f3943z12339jz93j39iafai3f9393g'); + * $http->post('http://domain.com', [ 'foo' => 'bar' ], [ 'use' => 'curl' ]); + * ~~~~~ + * + * #pw-group-request-headers + * + * @param string $name Name of cookie to set + * @param string|int|null $value Specify value to set or null to remove + * @return self + * @since 3.0.199 + * + */ + public function setCookie($name, $value) { + if($value === null) { + unset($this->setCookies[$name]); + } else { + $this->setCookies[$name] = $value; + } + return $this; + } /** * Set an array of data, or string of raw data to send with next GET/POST/etc. request (overwriting the existing data or rawData)