我正在使用cURL将以下XML发送到api:
$xml = "<request type='auth' timestamp='$timestamp'>
<merchantid>$merchantid</merchantid>
<account>$account</account>
<orderid>$orderid</orderid>
<amount currency='$currency'>$amount</amount>
<card>
<number>$cardnumber</number>
<expdate>$expdate</expdate>
<type>$cardtype</type>
<chname>$cardname</chname>
</card>
<sha1hash>$sha1hash</sha1hash>
</request>";
避免对这种XML进行硬编码的最佳方法是什么?我当时在考虑使用XMLWriter,但似乎很奇怪,因为它不会改变。
我应该使用模板吗?还是使用XMLWriter /简单XML生成它?
正如我在评论中提到的那样,不一定有正确的答案,但最近我也不得不围绕XML API Feed编写一个项目。我决定选择使用XMLWriter
它,并且通过使用他们受人尊敬的功能可以很容易地与其他人互换.loadXML()
。
class SomeApi extends XMLwriter {
public function __construct() {
$this->openMemory();
$this->setIndent( true );
$this->setIndentString ( "	" );
$this->startDocument( '1.0', 'UTF-8', 'no' );
$this->startElement( 'root' );
}
public function addNode( $Name, $Contents ) {
$this->startElement( $Name );
$this->writeCData( $Contents );
$this->endElement();
}
public function output() {
$this->endElement();
$this->endDocument();
}
//Returns a String of Xml.
public function render() {
return $this->outputMemory();
}
}
$newRequest = new SomeApi();
$newRequest->addNode( 'some', 'Some Lots of Text' );
$Xml = $newRequest->render();
我认为这是一种用PHP编写XML Feed的好方法,此外,您还可以添加以下内部函数:
$this->addHeader();
private function addHeader() {
$this->addNode( 'login', 'xxxxx' );
$this->addNode( 'password', 'xxxxx' );
}
然后,该节点会附加一遍又一遍地使用的节点。然后,如果您突然需要使用一个DOMDocument
对象(例如,我也需要XSL)。
$Dom = new DOMDocument();
$Dom->loadXML( $Xml );
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句