Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support BOM removal #3

Open
judgej opened this issue Apr 27, 2017 · 2 comments
Open

Support BOM removal #3

judgej opened this issue Apr 27, 2017 · 2 comments

Comments

@judgej
Copy link
Member

judgej commented Apr 27, 2017

This is a bit messy. The JSON response from Authorize.Net includes a BOM sequence at the start. This is invisible to the human eye, but causes json_decode() to throw a wobbly. It simply cannot decode the JSON with the BOM.

This is suggested in many places to remove the BOM:

preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $json_string);

I would probably be a little more specific by looking at only the first (up to) three characters:

preg_replace('/^[\x00-\x1F\x80-\xFF]{1,3}/', '', $json_string);

There is also a plugin for Guzzle that removes the BOM, but that appears to be for older Guzzle versions. Not sure about the latest.

Anyway, it has not really got anything to do with these messages, because it's not a part of the data, but if we are not aware of it, it will certainly come to bite us when not dealt with at the transport level.

@judgej
Copy link
Member Author

judgej commented Apr 27, 2017

Authorize.Net did inform many customers that the BOM would be removed from JSON responses in August 2016, to comply with RFC-7159. That obviously hasn't happened, at least not on the sandbox account.

Implementations MUST NOT add a byte order mark to the beginning of a JSON text. In the interests of interoperability, implementations that parse JSON texts MAY ignore the presence of a byte order mark rather than treating it as an error.

@judgej
Copy link
Member Author

judgej commented Apr 27, 2017

Sample code showing this:

// $create_transaction_request is an authorise transaction, for example.
// $client is a Guzzle client and $endpoint is the sandbox.

$response = $client->request('POST', $endpoint, [
    'json' => $create_transaction_request, // Guzzle will convert this to JSON.
]);

$result = (string)$response->getBody();
echo "<p>Length with BOM: " . strlen($result) . "</p>";
// With BOM: 540
var_dump(json_decode($result)); // NULL :-(

// Remove the BOM
$result = preg_replace('/^[\x00-\x1F\x80-\xFF]{1,3}/', '', $result);

echo "<p>Length without BOM: " . strlen($result) . "</p>";
// Without BOM: 537
var_dump(json_decode($result)); // ...the full response :-)

Just using trim() does not fix it, so it's not trailing or preceding white space that is the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant