Jump to content

Is this a parsing error or something else?


refiking

Recommended Posts

I have a script that retrieves USPS parcel options for purchases and I'm pasting the relevant code here:

 

 

$body = '';
$http = new httpClient();
if ($http->Connect('production.shippingapis.com', 80))
{
$http->addHeader('Host', 'production.shippingapis.com');
$http->addHeader('User-Agent', 'osCommerce');
$http->addHeader('Connection', 'Close');
if ($http->Get('/shippingapi.dll?' . $request)) $body = preg_replace(array('/\<sup\>\&reg;\<\/sup\>/', '/\<sup\>\&trade;\<\/sup\>/', '/\" /', '/\",/', '/\"<br>/', '/<br>/'), array('RM', 'TM', '",', '" ', '"<br>', 'BREAK'), htmlspecialchars_decode($http->getBody()));
$http->Disconnect();
echo '<pre>'; print_r($body); echo '</pre>';
return json_decode(json_encode(simplexml_load_string($body)),TRUE);
}

 

 

This used to work a few months back.  Not sure what changed, but now I get an error result (3xxxxx is the account number obviously).  I added the pre - $body - pre line recently to try to figure out what is wrong with it.

 
3xxxxxxxxxVARIABLEREGULAR1Media Mail<sup>®</sup>2.539Certificate of Mailingtruefalse1.2001Insurancetruefalse1.950truefalse13USPS Tracking<sup>™</sup>truetrue0.900.207Return Receipt for Merchandisetruefalse4.10015Signature Confirmation<sup>™</sup>truetrue2.702.206Collect on Deliverytruefalse6.450truetrue305283015710VARIABLEREGULAR1Priority Mail 1-Day<sup>™</sup>5.605.059Certificate of Mailingtruefalse1.2001Insurancetruetrue1.951.95truefalse5Registered Mail<sup>™</sup>truefalse11.200truefalse4Registered without Insurancetruefalse11.2000Certified Mail<sup>®</sup>truefalse3.10013USPS Tracking<sup>™</sup>truetrue0.000.007Return Receipt for Merchandisetruefalse4.10015Signature Confirmation<sup>™</sup>truetrue2.702.2019Adult Signature Requiredfalsetrue04.9520Adult Signature Restricted Deliveryfalsetrue05.156Collect on Deliverytruefalse6.450truetrue
 
Warning: simplexml_load_string() [function.simplexml-load-string]: Entity: line 1: parser error : String not closed expecting " or ' in /home/xxx/public_html/store/includes/modules/shipping/usps.php on line 297

Warning: simplexml_load_string() [function.simplexml-load-string]: <?xml version="1.0",encoding="UTF-8"?> in /home/xxx/public_html/store/includes/modules/shipping/usps.php on line 297

Warning: simplexml_load_string() [function.simplexml-load-string]: ^ in /home/xxx/public_html/store/includes/modules/shipping/usps.php on line 297

Warning: simplexml_load_string() [function.simplexml-load-string]: Entity: line 1: parser error : Blank needed here in /home/xxx/public_html/store/includes/modules/shipping/usps.php on line 297

Warning: simplexml_load_string() [function.simplexml-load-string]: <?xml version="1.0",encoding="UTF-8"?> in /home/xxx/public_html/store/includes/modules/shipping/usps.php on line 297

Warning: simplexml_load_string() [function.simplexml-load-string]: ^ in /home/xxx/public_html/store/includes/modules/shipping/usps.php on line 297

Warning: simplexml_load_string() [function.simplexml-load-string]: Entity: line 1: parser error : parsing XML declaration: '?>' expected in /home/xxx/public_html/store/includes/modules/shipping/usps.php on line 297

Warning: simplexml_load_string() [function.simplexml-load-string]: <?xml version="1.0",encoding="UTF-8"?> in /home/xxx/public_html/store/includes/modules/shipping/usps.php on line 297

Warning: simplexml_load_string() [function.simplexml-load-string]: ^ in /home/xxx/public_html/store/includes/modules/shipping/usps.php on line 297
Link to comment
Share on other sites

What ever this does

$body = preg_replace(array('/\<sup\>\&reg;\<\/sup\>/', '/\<sup\>\&trade;\<\/sup\>/', '/\" /', '/\",/', '/\"<br>/', '/<br>/'), array('RM', 'TM', '",', '" ', '"<br>', 'BREAK'), htmlspecialchars_decode($http->getBody()));

It is most probably causing the XML parse error(s)

 

Try commenting it out. What should that be replacing?

 

You probably want to apply the preg_replace to a specific node in the XML not the XML itself.

Edited by Ch0cu3r
Link to comment
Share on other sites

return json_decode(json_encode(simplexml_load_string($body)),TRUE);

 

I have a question. Why are you encoding just to decode?

 

Also, like Ch0cu3r said, if the USPS API is returning valid XML, you shouldn't need to do any replacing on it....  Just use simple XML to turn it into an object for parsing.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.