Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 15 additions & 14 deletions src/Helpers/Http.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,20 @@ public static function request(string $method, string $url, $data = null, array
}

/**
* Alternative method using file_get_contents with stream context
* File get contents wrapper to handle PHP version differences in fetching headers after a request.
*/
private static function fileGetContents(string $url, array $options): array {
if (!function_exists('http_get_last_response_headers')) {
require_once __DIR__.'/lib/PrePHP84Http.php';
} else {
require_once __DIR__.'/lib/PHP84Http.php';
}

return mchef_fetch_content($url, $options);
}

/**
* Alternative method using fileGetContents with stream context
* Useful when cURL is not available
*/
public static function getWithStream(string $url, array $headers = [], array $options = []): HttpResponse
Expand Down Expand Up @@ -219,21 +232,9 @@ public static function getWithStream(string $url, array $headers = [], array $op
$contextOptions = array_merge_recursive($contextOptions, $options['context']);
}

$context = stream_context_create($contextOptions);
$body = @file_get_contents($url, false, $context);
[$body, $headerLines] = self::fileGetContents($url, $contextOptions);

if ($body === false) {
$error = error_get_last();
throw new \RuntimeException("HTTP request failed: url - {$url} error - " . ($error['message'] ?? 'Unknown error'));
}

// Get response headers using the polyfill-compatible approach
$responseHeaders = [];
$headerLines = [];
if (isset($http_response_header)) {
$GLOBALS['http_response_header'] = $http_response_header;
}
$headerLines = http_get_last_response_headers() ?? [];

if (empty($headerLines)) {
// If no $http_response_header, it means the request completely failed
Expand Down
20 changes: 20 additions & 0 deletions src/Helpers/lib/PHP84Http.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

// Deliberately no namespace here as this is included directly in Http.php for PHP 8.4 and above support

/**
* PHP 8.4+ implementation of content fetching using file_get_contents
* This is used for PHP 8.4 and above where http_get_last_response_headers() is available to get headers after a request
*/
function mchef_fetch_content(string $url, ?array $options = null): array {
$context = stream_context_create($options);
$body = @file_get_contents($url, false, $context);

if ($body === false) {
$error = error_get_last();
throw new \RuntimeException("HTTP request failed: url - {$url} error - " . ($error['message'] ?? 'Unknown error'));
}
$headers = http_get_last_response_headers() ?? [];

return [$body, $headers];
}
20 changes: 20 additions & 0 deletions src/Helpers/lib/PrePHP84Http.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

// Deliberately no namespace here as this is included directly in Http.php for PHP < 8.3 support

/**
* PHP < 8.4 implementation of content fetching using file_get_contents
* This is used for PHP versions 8.3 and below where http_get_last_response_headers() is not available
*/
function mchef_fetch_content(string $url, ?array $options = null): array {
$context = stream_context_create($options);
$body = @file_get_contents($url, false, $context);

if ($body === false) {
$error = error_get_last();
throw new \RuntimeException("HTTP request failed: url - {$url} error - " . ($error['message'] ?? 'Unknown error'));
}
$headers = $http_response_header ?? [];

return [$body, $headers];
}
27 changes: 0 additions & 27 deletions src/Polyfills/http_get_last_response_headers.php

This file was deleted.