0

This question is an extension of a prevous question started here..

XML Parse Error - Extra content at the end of the document

i have added a snippet of code to list the names and phone numbers..

if(isset(${'Action'}) && ${'Action Type'}){
                if(${'Action'} == 'get'){
                    if(${'Limit'} != ''){
                        $limit = ' LIMIT '.${'Limit'}.'';
                    } else {
                        $limit = '';
                    }
                    $i = 1;
                    ${'Response'}['numbers'] = array();
                    ${'Query'} = mysql_query('SELECT * FROM `crm`.`accounts` WHERE `acquired_via` = "Scrubbed account" AND `sent_to_dialer` = "0"'.$limit);
                    while(${'Row'} = mysql_fetch_assoc(${'Query'})){
                        ${'Response'}['numbers']['number_'.$i] = array('Company_Name' => ${'Row'}['company_name'], 'Contact_First_Name' => ${'Row'}['contact_fname'], 'Contact_Last_Name' => ${'Row'}['contact_lname'], 'Office_Phone' => removeCHARSphone(${'Row'}['office_phone']), 'Mobile_Phone' => removeCHARSphone(${'Row'}['mobile_phone']));
                        $i++;
                    }
                }elseif(${'Action'} == 'details'){

                }
            }

This breaks the XML

http://lmsapi.com/?api_key=b3e04e54f0d92f8845d394b61c607d60&act=get&format=xml http://lmsapi.com/?api_key=b3e04e54f0d92f8845d394b61c607d60&act=get&format=json

But the JSON stays in tact...

Community
  • 1
  • 1
JD Vangsness
  • 697
  • 3
  • 10
  • 27

1 Answers1

2

You need to escape any ampersands and angle brackets in your XML content.

So & should become, &amp;, < should become &lt; and > should become &gt;.

The easiest way to do that is probably with str_replace call like this:

$string = str_replace(
  array('&', '<', '>'),
  array('&amp;', '&lt;', '&gt;'),
  $string);
James Holderness
  • 22,721
  • 2
  • 40
  • 52
  • Isn't `htmlentities()` even easier than `str_replace()`? – Barmar Jun 06 '13 at 23:51
  • @Barmar The problem with htmlentities is that it will also replace any number of entities that you don't want replaced and woldn't be supported in xml. – James Holderness Jun 06 '13 at 23:57
  • Surprised there's no similar `xmlentities()` function. – Barmar Jun 06 '13 at 23:58
  • @Barmar I may have to take that back - looking at the docs, it appears there are flags you can set for XML. This is new to me. Looks like it was added in 5.4.0. So depends what versions you need to support. – James Holderness Jun 06 '13 at 23:59