You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
Copy file name to clipboardExpand all lines: README.md
+48-2Lines changed: 48 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -1,15 +1,25 @@
1
1
# guzzle-transcoder
2
2
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.
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).
4
12
5
13
## Installation
14
+
6
15
It is recommended to install the library using [Composer]:
7
16
8
17
```ShellSession
9
18
$ composer require fossar/guzzle-transcoder
10
19
```
11
20
12
-
## Basic usage
21
+
## Usage
22
+
### Basic example
13
23
14
24
<!-- Headers: {"content-type": "text/html; charset=iso-8859-1; someOtherRandom=\"header in here\""} -->
15
25
<!-- Mock response: iso-8859-1.html -->
@@ -28,6 +38,42 @@ $req = $client->get($url);
28
38
echo $req->getBody();
29
39
```
30
40
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)
0 commit comments