Search code examples
phplaravelxmlwriter

XMLwriter returns empty string. Can anyone see a reason?


This function should create an xml file that eventually gets passed to a curl request in the controller, and eventually up to an accounting api. The curl request works fine when i pass in a static XML file.

The code here should generate a dynamic xml file based on the client that is being created.

For what ever reason, it is returning an empty string.

Some of the fields allow null.

Any ideas would be greatly appreciated.

$writer = new XMLWriter($xmlString);
// Output directly to the user 
$writer->openMemory();

$writer->openURI('php://output'); 
$writer->startDocument('1.0');
$writer->writeAttribute('encoding', 'utf-8');

$writer->startElement('request');
$writer->writeAttribute('method', 'client.create');

$writer->setIndent(true); 

$writer->startElement('client'); 
//---------------------------------------------------- 

$writer->writeElement('first_name', $org->first_name);
$writer->writeElement('last_name', $org->last_name);
$writer->writeElement('organization', $org->name); 
$writer->writeElement('email', $org->contact_email);
$writer->writeElement('username', $org->contact_name);
$writer->writeElement('password');

//---------------------------------------------------- 

$writer->startElement('contacts'); 

$writer->startElement('contact'); 

    $writer->writeElement('username', $org->contact_name);
    $writer->writeElement('first_name', $org->first_name);
    $writer->writeElement('last_name', $org->last_name);
    $writer->writeElement('email', $org->contact_email); 
    $writer->writeElement('phone1', $org->contact_phone);
    $writer->writeElement('phone2'); 

// contact 
$writer->endElement(); 

// End contacts 
$writer->endElement(); 
//---------------------------------------------------- 

$writer->writeElement('work_phone', $org->contact_phone);
$writer->writeElement('home_phone', $org->contact_phone);
$writer->writeElement('mobile', $org->contact_phone);
$writer->writeElement('fax', $org->contact_phone);
$writer->writeElement('language', 'en');
$writer->writeElement('currency_code', 'USD');
$writer->writeElement('phone1', $org->contact_phone);
$writer->writeElement('notes');

// primary address
$writer->writeElement('p_street1');
$writer->writeElement('p_city');
$writer->writeElement('p_state');
$writer->writeElement('p_code');

// secondary address
$writer->writeElement('s_street1');
$writer->writeElement('s_city');
$writer->writeElement('s_state');
$writer->writeElement('s_code');

$writer->writeElement('vat_name');
$writer->writeElement('vat_number');


// End channel 
$writer->endElement(); 

// End request
$writer->endElement();  

$writer->endDocument(); 

$currentMemory = $writer->outputMemory();

$writer->flush();

return $currentMemory;
}

Solution

  • I removed this snippet of code:

    $writer->openURI('php://output');
    

    and left in this snippet:

    $writer->openMemory();
    

    Since I was passing the xml to a the controller,

    openURI()
    

    was not the correct method, as it is designed to output directly to the user.

    openMemory()
    

    saves the data, and then closes at the bottom of the function, making it available in my controller function after I include this file.