Skip to content
This repository has been archived by the owner on Mar 15, 2020. It is now read-only.

Commit

Permalink
Add some support for setting request headers, and getting response he…
Browse files Browse the repository at this point in the history
…aders
  • Loading branch information
padraic committed Mar 8, 2015
1 parent c672980 commit bc2c025
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
composer.phar
composer.lock
vendor/

# Commit your application's lock file http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file
Expand Down
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
language: php

php:
- nightly # 7.0
- 5.6
- 5.5
- 5.4
Expand Down
55 changes: 52 additions & 3 deletions src/Humbug/FileGetContents.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ class FileGetContents

protected $options = array('http' => array());

protected static $lastResponseHeaders;

protected static $nextRequestHeaders;

public function __construct()
{
$this->checkConfig();
Expand All @@ -28,7 +32,53 @@ public function __construct()
public function get($filename, $context = null)
{
$context = $this->getStreamContext($filename);
return file_get_contents($filename, null, $context);
self::setHttpHeaders($context);
$result = file_get_contents($filename, null, $context);
self::setLastResponseHeaders($http_response_header);
return $result;
}

public static function setLastResponseHeaders($headers)
{
self::$lastResponseHeaders = $headers;
}

public static function getLastResponseHeaders()
{
return self::$lastResponseHeaders;
}

public static function setNextRequestHeaders(array $headers)
{
self::$nextRequestHeaders = $headers;
}

public static function hasNextRequestHeaders()
{
return !empty(self::$nextRequestHeaders);
}

public static function getNextRequestHeaders()
{
$return = self::$nextRequestHeaders;
self::$nextRequestHeaders = null;
return $return;
}

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);
stream_context_set_option(
$context,
'http',
'header',
$headers
);
}
return $context;
}

protected function checkConfig()
Expand Down Expand Up @@ -300,5 +350,4 @@ protected static function validateCaFile($contents) {

return (bool) openssl_x509_parse($contents);
}

}
}
36 changes: 35 additions & 1 deletion src/function.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,50 @@
use Humbug\FileGetContents;

if (!function_exists('humbug_get_contents')) {

function humbug_get_contents($filename, $use_include_path = false, $context = null) {
static $fileGetContents = null;

if ('https' == parse_url($filename, PHP_URL_SCHEME) && PHP_VERSION_ID < 50600) {
if (!isset($fileGetContents)) {
$fileGetContents = new FileGetContents;
}
return $fileGetContents->get($filename, $context);
} elseif (FileGetContents::hasNextRequestHeaders()) {
if ($context === null) {
$context = stream_context_create();
}
$context = FileGetContents::setHttpHeaders($context);
}
return file_get_contents($filename, $use_include_path, $context);
$return = file_get_contents($filename, $use_include_path, $context);
FileGetContents::setLastResponseHeaders($http_response_header);
return $return;
}

} else {
throw new \RuntimeException(
'Function has already been defined'
);
}

if (!function_exists('humbug_get_headers')) {

function humbug_get_headers() {
return FileGetContents::getLastResponseHeaders();
}

} else {
throw new \RuntimeException(
'Function has already been defined'
);
}

if (!function_exists('humbug_set_headers')) {

function humbug_set_headers($headers) {
return FileGetContents::setNextRequestHeaders($headers);
}

} else {
throw new \RuntimeException(
'Function has already been defined'
Expand Down
9 changes: 9 additions & 0 deletions tests/Humbug/Test/FunctionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

namespace Humbug\Test;

use Humbug\FileGetContents;

class FunctionTest extends \PHPUnit_Framework_TestCase
{

Expand Down Expand Up @@ -67,5 +69,12 @@ public function testFileGetContentsWillPassThrough()
file_put_contents(sys_get_temp_dir() . '/humbug.tmp', ($expected = uniqid()), LOCK_EX);
$this->assertEquals(file_get_contents(sys_get_temp_dir() . '/humbug.tmp'), $expected);
}

public function testCanGetResponseHeaders()
{
humbug_set_headers(['Accept-Language: da\r\n']);
humbug_get_contents('http://padraic.github.io');
$this->assertTrue(count(humbug_get_headers()) > 0);
}

}

0 comments on commit bc2c025

Please sign in to comment.