├── .gitignore ├── CHANGELOG.md ├── composer.json ├── LICENSE ├── src └── Cviebrock │ └── Guzzle │ └── Plugin │ └── StripBom │ └── StripBomPlugin.php └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | Changelog 2 | ========= 3 | 4 | 5 | ## Version 0.1.3 - 23-Sep-2013 6 | 7 | - initial release 8 | - several corrections to get composer working (thus the 0.1.3 version) 9 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "cviebrock/guzzle-stripbom-plugin", 3 | "description": "Plug-in for Guzzle that strips BOMs from server responses", 4 | "homepage": "https://github.com/cviebrock/guzzle-stripbom-plugin", 5 | "keywords": ["plugin", "guzzle", "bom", "json", "xml"], 6 | "license": "MIT", 7 | "authors": [ 8 | { 9 | "name": "Colin Viebrock", 10 | "email": "colin@viebrock.ca", 11 | "homepage": "https://github.com/cviebrock" 12 | } 13 | ], 14 | "require": { 15 | "php": ">=5.3.2", 16 | "guzzle/guzzle": ">=3.7.3" 17 | }, 18 | "autoload": { 19 | "psr-0": { 20 | "Cviebrock\\Guzzle\\Plugin\\StripBom": "src/" 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2013 Colin Viebrock 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software is furnished to do so, 10 | subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /src/Cviebrock/Guzzle/Plugin/StripBom/StripBomPlugin.php: -------------------------------------------------------------------------------- 1 | 'onRequestComplete', 18 | ); 19 | } 20 | 21 | /** 22 | * When the request is complete, check the message body and strip any BOMs, if they exist. 23 | * 24 | * @param Event $event 25 | */ 26 | public function onRequestComplete(Event $event) 27 | { 28 | if ($body = $event['response']->getBody()) { 29 | if (substr($body, 0, 3) === "\xef\xbb\xbf") { 30 | // UTF-8 31 | $event['response']->setBody(substr($body, 3)); 32 | } else if (substr($body, 0, 4) === "\xff\xfe\x00\x00" || 33 | substr($body, 0, 4) === "\x00\x00\xfe\xff" 34 | ) { 35 | // UTF-32 36 | $event['response']->setBody(substr($body, 4)); 37 | } else if (substr($body, 0, 2) === "\xff\xfe" || 38 | substr($body, 0, 2) === "\xfe\xff" 39 | ) { 40 | // UTF-16 41 | $event['response']->setBody(substr($body, 2)); 42 | } 43 | } 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | guzzle-stripbom-plugin 2 | ====================== 3 | 4 | Plug-in for [Guzzle](http://guzzlephp.org/) that strips BOMs from server responses. 5 | 6 | 7 | ## Usage 8 | 9 | In your project's `composer.json` file: 10 | 11 | ``` 12 | "require": { 13 | "cviebrock/guzzle-stripbom-plugin": "0.1.*", 14 | } 15 | ``` 16 | 17 | In your code: 18 | 19 | ```php 20 | $client = new Guzzle\Http\Client('http://example.com'); 21 | 22 | $client->addSubscriber( new Cviebrock\Guzzle\Plugin\StripBom\StripBomPlugin() ); 23 | 24 | $request = $client->get('some/request'); 25 | 26 | $response = $client->send($request); 27 | 28 | $data = $response->json(); 29 | ``` 30 | 31 | 32 | ## Why? 33 | 34 | Some API services (mostly .NET services) include a [BOM](http://en.wikipedia.org/wiki/Byte_order_mark) in their response body. 35 | The BOM is 2-4 bytes that indicate what character encoding the response is in (e.g. UTF8). The problem is that PHP's 36 | `json_decode()` function and `SimpleXML` classes barf when trying to parse strings that include a BOM. 37 | If you are getting a "Can't parse JSON" error when handling a request, but it looks like JSON to you, this is likely what's happening. 38 | 39 | This plugin strips those bytes off if they exist, before any JSON/XML parsing. 40 | 41 | 42 | ## Kudos? Questions? Complaints? 43 | 44 | Please use the [issue tracker](https://github.com/cviebrock/guzzle-stripbom-plugin/issues). --------------------------------------------------------------------------------