]> git.mxchange.org Git - friendica.git/commitdiff
New function for fetching associated header array
authorMichael <heluecht@pirati.ca>
Tue, 1 Oct 2019 18:22:33 +0000 (18:22 +0000)
committerMichael <heluecht@pirati.ca>
Tue, 1 Oct 2019 18:22:33 +0000 (18:22 +0000)
src/Network/CurlResult.php

index 998644a8765fb8501b2b64ae5a2f744e9c4d4d81..b9ab07be9f2f27c93dedb29b772e5efc7f325bb4 100644 (file)
@@ -230,7 +230,7 @@ class CurlResult
         *
         * @return string|bool the Curl headers, "false" when field isn't found
         */
-       public function getHeader($field = '')
+       public function getHeader(string $field = '')
        {
                if (empty($field)) {
                        return $this->header;
@@ -241,13 +241,36 @@ class CurlResult
                        $parts = explode(':', $line);
                        $headerfield = array_shift($parts);
                        if (strtolower(trim($field)) == strtolower(trim($headerfield))) {
-                               return implode(':', $parts);
+                               return trim(implode(':', $parts));
                        }
                }
 
                return false;
        }
 
+       /**
+        * Returns the Curl headers as an associated array
+        *
+        * @return array associated header array
+        */
+       public function getHeaderArray()
+       {
+               $headers = [];
+
+               $lines = explode("\n", $this->header);
+               foreach ($lines as $line) {
+                       $parts = explode(':', $line);
+                       $headerfield = strtolower(trim(array_shift($parts)));
+                       $headerdata = trim(implode(':', $parts));
+
+                       if (!empty($headerdata)) {
+                               $headers[$headerfield] = $headerdata;
+                       }
+               }
+
+               return $headers;
+       }
+
        /**
         * @return bool
         */