refiking Posted November 24, 2013 Share Posted November 24, 2013 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\>\®\<\/sup\>/', '/\<sup\>\™\<\/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 Quote Link to comment https://forums.phpfreaks.com/topic/284224-is-this-a-parsing-error-or-something-else/ Share on other sites More sharing options...
Ch0cu3r Posted November 24, 2013 Share Posted November 24, 2013 (edited) What ever this does $body = preg_replace(array('/\<sup\>\®\<\/sup\>/', '/\<sup\>\™\<\/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 November 24, 2013 by Ch0cu3r Quote Link to comment https://forums.phpfreaks.com/topic/284224-is-this-a-parsing-error-or-something-else/#findComment-1459844 Share on other sites More sharing options...
objnoob Posted November 24, 2013 Share Posted November 24, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/284224-is-this-a-parsing-error-or-something-else/#findComment-1459847 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.