Skip to content

Commit cbba26b

Browse files
committed
README: Update documentation
List features, add full example, add Packagist link.
1 parent c698f53 commit cbba26b

File tree

3 files changed

+61
-3
lines changed

3 files changed

+61
-3
lines changed

LICENSE renamed to LICENSE.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
Copyright © 2014 Pascal Landau, http://www.myseosolution.de/ <[email protected]>
2-
Copyright © 2017 Jan Tojnar
2+
Copyright © 2017–2023 Jan Tojnar
33

44
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
55

README.md

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,25 @@
11
# guzzle-transcoder
22

3-
This [Guzzle] 6/7 middleware converts documents obtained by Guzzle to UTF-8 using [Transcoder] library. It is largely based on Pascal Landau’s [guzzle-auto-charset-encoding-subscriber] and [web-utility] libraries. Thanks to Transcoder, when mbstring is not available, iconv will be used.
3+
[![Packagist Version](https://img.shields.io/packagist/v/fossar/guzzle-transcoder)](https://packagist.org/packages/fossar/guzzle-transcoder)
4+
5+
This package provides a [Guzzle] 6/7 middleware that transparently converts documents obtained by Guzzle from its native encoding to UTF-8 (or any other specified encoding). It supports the following features:
6+
7+
- Detection of charset from [`Content-Type`] HTTP header.
8+
- Detection of charset from [`meta` element] in HTML document.
9+
- Detection of charset from [XML declaration] in RSS and other XML documents.
10+
- Updating the `Content-Type` header in the `Response` object according to target encoding.
11+
- Updating the metadata in the `Response` body according to target encoding (not enabled by default).
412

513
## Installation
14+
615
It is recommended to install the library using [Composer]:
716

817
```ShellSession
918
$ composer require fossar/guzzle-transcoder
1019
```
1120

12-
## Basic usage
21+
## Usage
22+
### Basic example
1323

1424
<!-- Headers: {"content-type": "text/html; charset=iso-8859-1; someOtherRandom=\"header in here\""} -->
1525
<!-- Mock response: iso-8859-1.html -->
@@ -28,6 +38,42 @@ $req = $client->get($url);
2838
echo $req->getBody();
2939
```
3040

41+
### Full example
42+
43+
<!-- Headers: {"content-type": "text/html; charset=iso-8859-1; someOtherRandom=\"header in here\""} -->
44+
<!-- Mock response: iso-8859-1.html -->
45+
<!-- Expected: readme-full -->
46+
```php
47+
use Fossar\GuzzleTranscoder\GuzzleTranscoder;
48+
use GuzzleHttp\Client;
49+
use GuzzleHttp\HandlerStack;
50+
51+
$stack = HandlerStack::create();
52+
$stack->push(new GuzzleTranscoder([
53+
'targetEncoding' => 'windows-1252',
54+
// Swap the default settings:
55+
'replaceHeaders' => false,
56+
'replaceContent' => true,
57+
]));
58+
$client = new Client(['handler' => $stack]);
59+
60+
$url = 'https://www.myseosolution.de/scripts/encoding-test.php?enc=iso'; // request website with iso-8859-1 encoding
61+
$req = $client->get($url);
62+
echo $req->getHeaderLine('Content-Type') . "\n"; // HTTP header will remain unchanged
63+
echo $req->getBody();
64+
```
65+
66+
## Credits
67+
68+
It is largely based on Pascal Landau’s [guzzle-auto-charset-encoding-subscriber] and [web-utility] libraries.
69+
70+
We are using [Transcoder] library. This allows us to fall back to `iconv` when `mbstring` is not available or an encoding is not supported by it.
71+
72+
The source code is available under the terms of [MIT license](LICENSE.md)
73+
74+
[`Content-Type`]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Type
75+
[`meta` element]: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/meta#charset
76+
[XML declaration]: https://developer.mozilla.org/en-US/docs/Web/XML/XML_introduction#xml_declaration
3177
[Composer]: https://getcomposer.org/
3278
[Guzzle]: https://github.com/guzzle/guzzle
3379
[Transcoder]: https://github.com/fossar/transcoder

tests/resources/readme-full

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
text/html; charset=iso-8859-1; someOtherRandom="header in here"
2+
<!DOCTYPE html>
3+
<html>
4+
<head>
5+
<meta charset="windows-1252">
6+
<title>Umlauts everywhere �������</title>
7+
</head>
8+
<body>
9+
<h1>�������</h1>
10+
11+
</body>
12+
</html>

0 commit comments

Comments
 (0)