且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何从此xml在php中生成soap请求?

更新时间:2021-08-05 22:53:30

SoapHeader 处理数组相当随意.如果您想使用数组,请考虑使用 ArrayObject 而不是原生结构.

SoapHeader treats arrays rather arbitrarily. If you ever want to use an array, consider using ArrayObject instead of the native construct.

但是,您根本不需要数组,因为您只是想在标题中构造单个元素.并且因为您的每个内部元素(例如 ClientIP)都有一个唯一的命名空间,所以您不能只传入一个基本对象.相反,您必须使用 SoapVar 类为每个元素指定一个特定的命名空间,这允许您将普通 PHP 数据包装在 SoapClient 可以的SOAP-ready"容器中理解和翻译.

However, you don't need an array at all since you're only trying to construct a single element in your header. And because each of your internal elements (eg. ClientIP) has a unique namespace, you can't just pass in a basic object. Instead, you have to specify a particular namespace for each element using the SoapVar class, which allows you to wrap normal PHP data in a "SOAP-ready" container that SoapClient can understand and translate.

$innerNS = "http://www.w3.org/BaufestProductivityFramework";
$outerNS = "http://schemas.datacontract.org/2004/07/Bpf.Common.Service";

$tag = new stdClass();
$tag->ClientIP = new SoapVar("200.125.145.10", XSD_STRING, null, null, null, $innerNS);
$tag->CompanyId = new SoapVar(1, XSD_INT, null, null, null, $innerNS);
$tag->UserName = new SoapVar("someUser", XSD_STRING, null, null, null, $innerNS);

$client->__setSoapHeaders(new SoapHeader($outerNS, 'InfoTag', $tag));

最后,作为一项规则,不要手动编写 XML! 考虑像此处显示的标头代码那样重新编写 SOAP 主体代码.您应该能够专门处理 XML 的内容,而不是它的结构.

Finally, as a rule, don't manually write XML! Consider re-writing your SOAP body code like the header code shown here. You ought to be able to deal specifically with the content of the XML, not its structure.