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

🐛 Fix xml UTF8 encoding #496

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
14 changes: 14 additions & 0 deletions src/Core/CoreHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,4 +159,18 @@ public static function GetRequestLogging($serviceContext)

return $requestLogger;
}

/**
* Clean up xml response
* @param $string
* @return array|string|null
*/
public static function encodeUTF8($string)
{
$string = preg_replace('/[\x00-\x1F\x7F]/u', '', $string);
if (mb_detect_encoding($string, 'UTF-8', true) === false) {
$string = utf8_encode($string);
}
return $string;
}
}
2 changes: 1 addition & 1 deletion src/Core/HttpClients/FaultHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ public function setResponseBody($body)
/**
* Return the status code of the Response
*
* @return Integer The response status code
* @return int The response status code
*/
public function getHttpStatusCode()
{
Expand Down
1 change: 1 addition & 0 deletions src/Core/HttpClients/SyncRestHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,7 @@ public function LogAPIRequestToLog($requestBody, $requestUri, $httpHeaders){
* @return string|bool The DOM structured XML
*/
private function parseStringToDom($string){
$string = CoreHelper::encodeUTF8($string);
$dom = new \DOMDocument();
$dom->preserveWhiteSpace = FALSE;
$dom->loadXML($string);
Expand Down
11 changes: 6 additions & 5 deletions src/DataService/DataService.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
use QuickBooksOnline\API\Data\IPPPreferences;

/**
* Class DataServicd
* Class DataService
*
* This file contains DataService performs CRUD operations on IPP REST APIs.
*/
Expand Down Expand Up @@ -802,13 +802,13 @@ public function Add($entity)
$this->verifyOperationAccess($entity, __FUNCTION__);
if ($this->isJsonOnly($entity)) {
$this->forceJsonSerializers();
}
}

$httpsPostBody = $this->executeObjectSerializer($entity, $urlResource);
// Builds resource Uri
$resourceURI = implode(CoreConstants::SLASH_CHAR, array('company', $this->serviceContext->realmId, $urlResource));

$uri = $this->handleTaxService($entity, $resourceURI);
$uri = $this->handleTaxService($entity, $resourceURI);
// Send request
return $this->sendRequestParseResponseBodyAndHandleHttpError($entity, $uri, $httpsPostBody, DataService::ADD);
}
Expand Down Expand Up @@ -948,7 +948,7 @@ public function DownloadPDF($entity, $dir=null, $returnPdfString = false)
return $responseBody;
} else {
$this->lastError = false;

return $this->processDownloadedContent(new ContentWriter($responseBody), $responseCode, $dir, $this->getExportFileNameForPDF($entity, "pdf"));
}
}
Expand Down Expand Up @@ -1008,7 +1008,7 @@ public function Query($query, $startPosition = null, $maxResults = null, $includ

$httpsUri = implode(CoreConstants::SLASH_CHAR, array('company', $this->serviceContext->realmId, 'query'));
$httpsPostBody = $this->appendPaginationInfo($query, $startPosition, $maxResults);

if(!is_null($includes)) {
$httpsUri .= "?include=$includes";
}
Expand All @@ -1024,6 +1024,7 @@ public function Query($query, $startPosition = null, $maxResults = null, $includ
$this->lastError = false;
$parsedResponseBody = null;
try {
$responseBody = CoreHelper::encodeUTF8($responseBody);
$responseXmlObj = simplexml_load_string($responseBody);
if ($responseXmlObj && $responseXmlObj->QueryResponse) {
$tmpXML = $responseXmlObj->QueryResponse->asXML();
Expand Down