guzzle/parser
guzzle/parser provides lightweight message parsing utilities for Guzzle, helping you parse HTTP request/response messages, headers, and related components. Useful when working with raw HTTP strings or building tooling around Guzzle’s message format.
Installation
composer require guzzle/parser
First Use Case: Parsing Cookies
use Guzzle\Parser\Cookie\CookieParser;
$cookieHeader = 'name1=value1; name2=value2; expires=Wed, 21 Oct 2023 07:28:00 GMT';
$cookies = CookieParser::parseHeader($cookieHeader);
// Output: Array of `Guzzle\Parser\Cookie\Cookie` objects
First Use Case: Parsing URIs
use Guzzle\Parser\UriTemplate\UriTemplate;
$template = 'http://example.com/{user}';
$uri = UriTemplate::expand($template, ['user' => 'john']);
// Output: 'http://example.com/john'
Where to Look First
Guzzle\Parser\Cookie\CookieParser (for cookie handling).Guzzle\Parser\UriTemplate\UriTemplate (for URI templating).Guzzle\Parser\Header\HeaderParser (for HTTP headers).Set-Cookie headers or Cookie headers).Cookie objects with properties like name, value, expires, domain, etc.$parser = new CookieParser();
$cookies = $parser->parseHeader($rawCookieHeader);
foreach ($cookies as $cookie) {
if ($cookie->isExpired()) {
// Handle expired cookies
}
}
$client = new \GuzzleHttp\Client();
$response = $client->request('GET', 'https://example.com');
$cookies = CookieParser::parseHeader($response->getHeaderLine('Set-Cookie'));
$template = 'https://api.example.com/v1/users/{userId}/posts/{postId}';
$uri = UriTemplate::expand($template, [
'userId' => 123,
'postId' => 456
]);
// Output: 'https://api.example.com/v1/users/123/posts/456'
$client = new \GuzzleHttp\Client();
$response = $client->get($uri);
Content-Range, Link).use Guzzle\Parser\Header\HeaderParser;
$contentRange = 'bytes 1000-2000/5000';
$range = HeaderParser::parseContentRange($contentRange);
// Output: ['firstBytePos' => 1000, 'lastBytePos' => 2000, 'totalBytes' => 5000]
$response = $client->request('GET', 'https://example.com/large-file');
$range = HeaderParser::parseContentRange($response->getHeaderLine('Content-Range'));
$userInput = 'name=test; domain=evil.com';
try {
$cookies = CookieParser::parseHeader($userInput);
// Reject if domain is suspicious
} catch (\InvalidArgumentException $e) {
// Handle malformed input
}
Guzzle 3 Compatibility
UriTemplate in Guzzle 3 uses expand(), while newer versions may use resolve().Cookie Parsing Quirks
= or ;) may throw InvalidArgumentException.try-catch block for robustness.DateTime objects. Ensure your server’s timezone is configured correctly to avoid expired misclassifications.URI Template Strictness
UriTemplate throws exceptions for unmatched variables.UriTemplate::expandStrict() for strict validation or expand() for lenient parsing (fills missing vars with null).Header Parser Limitations
Link) may not support all RFC 7230/7231 variants.Link headers before parsing).Log Raw Inputs
\Log::debug('Raw cookie header:', [$rawCookieHeader]);
Use var_dump for Objects
var_dump to understand their structure:
var_dump($cookies[0]->getDomain());
Test Edge Cases
name=用户).Set-Cookie: id=123;; Secure).Custom Cookie Parsing
Guzzle\Parser\Cookie\Cookie to add custom attributes:
class CustomCookie extends \Guzzle\Parser\Cookie\Cookie {
public function getCustomAttribute() { ... }
}
CookieParser to handle your custom format.URI Template Extensions
UriTemplate to add custom variable resolvers:
class CustomUriTemplate extends \Guzzle\Parser\UriTemplate\UriTemplate {
protected function resolveVariable($name, $value) { ... }
}
Header Parser Hooks
HeaderParser methods (e.g., parseContentRange) if you need pre/post-processing.How can I help you explore Laravel packages today?