Skip to content

Use mb_convert_encoding when encoding detected, else try utf8_encode #112

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

Merged
merged 3 commits into from
Jul 12, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions src/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,7 @@ public function request($access, $request, $endpointurl)
]);

if ($response->getStatusCode() === 200) {
if (function_exists('mb_convert_encoding')) {
$body = mb_convert_encoding($body, 'UTF-8', mb_detect_encoding($body));
}
$body = $this->convertEncoding($body);

$xml = new SimpleXMLElement($body);
if (isset($xml->Response) && isset($xml->Response->ResponseStatusCode)) {
Expand Down Expand Up @@ -218,4 +216,22 @@ public function getEndpointUrl()
{
return $this->endpointUrl;
}

/**
* @param $body
* @return string
*/
protected function convertEncoding($body)
{
if (!function_exists('mb_convert_encoding')) {
return $body;
}

$encoding = mb_detect_encoding($body);
if ($encoding) {
return mb_convert_encoding($body, 'UTF-8', $encoding);
}

return utf8_encode($body);
}
}
1 change: 0 additions & 1 deletion tests/Ups/Tests/EntityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,5 +192,4 @@ public function getListOfNodesWithoutNodeInterface()
return [$item];
}, $this->nodeListNoNodeInterface);
}

}
2 changes: 0 additions & 2 deletions tests/Ups/Tests/ExceptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

class ExceptionTest extends PHPUnit_Framework_TestCase
{

public function testInvalidResponseException()
{
$exception = new InvalidResponseException('Test Message', 1);
Expand All @@ -24,5 +23,4 @@ public function testRequestException()
$this->assertEquals('Test Message', $exception->getMessage());
$this->assertEquals(1, $exception->getCode());
}

}