Page 1 of 1

Invalid UTF-8 in XML SOAP response

Posted: Mon Jun 04, 2007 9:37 am
by Begby
I am working with the amazon API for seller central and have been working on this one issue for literally a week. I think the problem may be related to an invalid character being returned by Amazon but that might not be the case. I am at the point where I am really needing help and Amazon won't give me much help unless I can conclusively prove that the problem is in their response.

Here is my code

Code: Select all

<?php
class AmazonMerchant
{
	public $merchantIdentifier ;
	public $merchantName ;
}

class AmazonDocumentInfoCollection implements IteratorAggregate 
{
	public $MerchantDocumentInfo = array() ;
	
	public function getIterator()
	{
		return new ArrayIterator($this->MerchantDocumentInfo) ;
	}
}


class AmazonDocumentInfo 
{
	public $documentID ;
	public $generatedDateTime ;
}


$classmap = array(
	'Merchant' => 'AmazonMerchant',
	'MerchantDocumentInfo' => 'AmazonDocumentInfo',
	'ArrayOfMerchantDocumentInfo' => 'AmazonDocumentInfoCollection'
) ;

$merchant = new AmazonMerchant() ;
$merchant->merchantIdentifier = 'M_DISEASETEE_544913' ;
$merchant->merchantName = 'Disease Tees' ;

$params = array(
	'classmap' => $classmap,
	'login' => 'mylogin',
	'password' => 'mypassword',
	'trace' => 1,
	'exceptions' => 1
) ;

ini_set("soap.wsdl_cache_enabled", "0");

$client = new SoapClient('wsdl/amazon_merchant-interface-mime.wsdl', $params) ;



try
{
	// Works fine and returns an ArrayOfMerchantDocumentInfo
	$docs = $client->getAllPendingDocumentInfo($merchant, '_GET_ORDERS_DATA_') ;
}
catch(SoapFault $fault)
{
	print_r($fault) ;
}


try
{
	// This passes an id from the DocumentInfo array object
        /*  THIS IS THE CALL THAT ALWAYS FAILS */
	$response = $client->getDocument($merchant, '369767253') ;
}
catch(SoapFault $fault)
{
	print_r($fault) ;
}

//  nada
print_r($response) ;

// Debug stuff, this prints out the response from amazon which appears to be correct.
print "<pre>\n";
print "Request :\n".htmlspecialchars($client->__getLastRequest()) ."\n";
print "Response:\n".htmlspecialchars($client->__getLastResponse())."\n";
print "Headers:\n".htmlspecialchars($client->__getLastResponseHeaders()."\n") ;
print "</pre>"; 


?>
I have no problem authenticating or getting responses from Amazon except for the getDocument() method. It throws an exception with the message 'looks like we got no XML document' from the client. Googling for that error I found that the problem could be related to either whitespace in the PHP before or after the end tags, or libxml puking on invalid UTF-8. I ruled out the whitespace problem.

The response returned from Amazon appears to be OK from looking at it, I do not know how I can verify if there is invalid UTF-8 though. I also ran tcpdump and have a dump of the response. I can post that if you like, but its pretty big.

Anybody know what I can do next to analyze the response to try and figure out how to deterimine if that is the problem, or does anyone see any other possible problems with my code?

Posted: Sat Jun 16, 2007 5:02 am
by Weirdan
I do not know how I can verify if there is invalid UTF-8 though.
To check if you're receiving proper utf8 you could do the following:

Code: Select all

if ($string !== iconv('utf8', 'utf8//IGNORE', $string)) {
   echo 'Bad, REALLY BAD UTF8';
}