From 7841b6594601d36316c0324f00ce44f6311f2d29 Mon Sep 17 00:00:00 2001 From: Padraic Brady Date: Sun, 8 Mar 2015 20:59:51 +0000 Subject: [PATCH] Wrap up header support for now --- src/Humbug/FileGetContents.php | 9 ++++++++- src/function.php | 2 +- tests/Humbug/Test/FunctionTest.php | 11 +++++++++++ 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/Humbug/FileGetContents.php b/src/Humbug/FileGetContents.php index 2b70757..9536e99 100644 --- a/src/Humbug/FileGetContents.php +++ b/src/Humbug/FileGetContents.php @@ -70,7 +70,14 @@ public static function setHttpHeaders($context) $headers = self::getNextRequestHeaders(); if (!empty($headers)) { $options = stream_context_get_options($context); - $headers = empty($options) ? $headers : array_merge($options['http']['headers'], $headers); + if (!isset($options['http'])) { + $options['http'] = array('header'=>array()); + } elseif (!isset($options['http']['header'])) { + $options['http']['header'] = array(); + } elseif (is_string($options['http']['header'])) { + $options['http']['header'] = explode("\r\n", $options['http']['header']); + } + $headers = empty($options['http']['headers']) ? $headers : array_merge($options['http']['headers'], $headers); stream_context_set_option( $context, 'http', diff --git a/src/function.php b/src/function.php index 792b7df..21afc9d 100644 --- a/src/function.php +++ b/src/function.php @@ -51,7 +51,7 @@ function humbug_get_headers() { if (!function_exists('humbug_set_headers')) { - function humbug_set_headers($headers) { + function humbug_set_headers(array $headers) { return FileGetContents::setNextRequestHeaders($headers); } diff --git a/tests/Humbug/Test/FunctionTest.php b/tests/Humbug/Test/FunctionTest.php index 1602f6a..a6cebd9 100644 --- a/tests/Humbug/Test/FunctionTest.php +++ b/tests/Humbug/Test/FunctionTest.php @@ -76,5 +76,16 @@ public function testCanGetResponseHeaders() humbug_get_contents('http://padraic.github.io'); $this->assertTrue(count(humbug_get_headers()) > 0); } + + public function testCanSetRequestHeaders() + { + humbug_set_headers(array( + 'Accept-Language: da', + 'User-Agent: Humbug' + )); + $out = humbug_get_contents('http://myhttp.info/'); + $this->assertEquals(1, preg_match('%'.preg_quote('Accept languageda').'%', $out)); + $this->assertEquals(1, preg_match('%'.preg_quote('User agentHumbug').'%', $out)); + } } \ No newline at end of file