普通网友 2016-11-15 17:51
浏览 84
已采纳

XMLwriter返回空字符串。 任何人都可以看到原因吗?

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;
}
  • 写回答

1条回答 默认 最新

  • dongtuan1980 2016-11-15 18:23
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向