使用 POST 请求通过 Curl 发送 SimpleXmlElement
如何使用 POST
请求类型通过 Curl
发送 SimpleXmlElement
对象并接收返回的 SimpleXmlElement
对象。
我在本地服务器上创建了两个文件并创建了对象。
URL:
http://someaddress/fileOne.php
http://someaddress/fileTwo.php
第一个文件中的对象:
$Xml = new SimpleXMLElement( '<?xml version="1.0" encoding="UTF-8"?><SomeXml></SomeXml>' );
$Translation = $Xml->addChild( 'Translation' );
$Translation->addChild( 'Phrase', 'test' );
现在我想发送此$Xml
通过curl对象并在其他文件中解析它并发回,
$Xml = new SimpleXMLElement( '<?xml version="1.0" encoding="UTF-8"?><SomeXml></SomeXml>' );
$Translation = $Xml->addChild( 'Translation' );
$Translation->addChild( 'Phrase', "Got your phrase: $phrase" );
如果您能提供代码示例,我将非常感激。 谢谢大家的帮助。
How could I send SimpleXmlElement
object via Curl
using POST
request type and receive SimpleXmlElement
object back.
I made two files on my local server and created object.
URLs:
http://someaddress/fileOne.php
http://someaddress/fileTwo.php
Object in from first file:
$Xml = new SimpleXMLElement( '<?xml version="1.0" encoding="UTF-8"?><SomeXml></SomeXml>' );
$Translation = $Xml->addChild( 'Translation' );
$Translation->addChild( 'Phrase', 'test' );
and now I would like to send this $Xml
object via curl and parse it in other file and send back
$Xml = new SimpleXMLElement( '<?xml version="1.0" encoding="UTF-8"?><SomeXml></SomeXml>' );
$Translation = $Xml->addChild( 'Translation' );
$Translation->addChild( 'Phrase', "Got your phrase: $phrase" );
I would appreciate very much if you could provide code examples.
Thanks everyone for help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不会发送 SimpleXMLElement 对象,而是发送 XML 数据。
从发送端,您将:
然后从接收端,您将获取请求并使用 SimpleXml 解析它。
You wouldn't send the SimpleXMLElement object, you would send the XML data.
From your send side, you would:
Then from your receive side you would just get the request and parse it using SimpleXml.
唯一可以通过 cURL 传递的数据类型是字符串。您可以使用如下函数解析元素(参考:http://www.nicolaskuttler。 com/post/php-innerhtml/)
然后 urlencode(innerHTML ( $XML ) ) 并通过curl。
警告 - 如果您正在使用大型 DOM 元素,上述函数可能会导致服务器压力。
The only data type that can be passed through cURL is string. You could parse the elements using a function like the below (ref: http://www.nicolaskuttler.com/post/php-innerhtml/)
then urlencode( innerHTML ( $XML ) ) and pass through curl.
A word of warning - if you are working with a large DOM Element, the above function can cause server strain.