使用 POST 请求通过 Curl 发送 SimpleXmlElement

发布于 2024-11-30 23:28:29 字数 894 浏览 0 评论 0原文

如何使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

青芜 2024-12-07 23:28:29

您不会发送 SimpleXMLElement 对象,而是发送 XML 数据。

从发送端,您将:

$xml = '<?xml version="1.0" encoding="UTF-8"?><SomeXml></SomeXml>';

// assuming you have a previously initialized $curl_handle
curl_setopt( $curl_handle, CURLOPT_POSTFIELDS, $xml);

然后从接收端,您将获取请求并使用 SimpleXml 解析它。

You wouldn't send the SimpleXMLElement object, you would send the XML data.

From your send side, you would:

$xml = '<?xml version="1.0" encoding="UTF-8"?><SomeXml></SomeXml>';

// assuming you have a previously initialized $curl_handle
curl_setopt( $curl_handle, CURLOPT_POSTFIELDS, $xml);

Then from your receive side you would just get the request and parse it using SimpleXml.

并安 2024-12-07 23:28:29

唯一可以通过 cURL 传递的数据类型是字符串。您可以使用如下函数解析元素(参考:http://www.nicolaskuttler。 com/post/php-innerhtml/)

function innerHTML( $contentdiv ) {
            $r = '';
            $elements = $contentdiv->childNodes;
            foreach( $elements as $element ) { 
                    if ( $element->nodeType == XML_TEXT_NODE ) {
                            $text = $element->nodeValue;
                            // IIRC the next line was for working around a
                            // WordPress bug
                            //$text = str_replace( '<', '<', $text );
                            $r .= $text;
                    }    
                    // FIXME we should return comments as well
                    elseif ( $element->nodeType == XML_COMMENT_NODE ) {
                            $r .= '';
                    }    
                    else {
                            $r .= '<';
                            $r .= $element->nodeName;
                            if ( $element->hasAttributes() ) { 
                                    $attributes = $element->attributes;
                                    foreach ( $attributes as $attribute )
                                            $r .= " {$attribute->nodeName}='{$attribute->nodeValue}'" ;
                            }    
                            $r .= '>';
                            $r .= $this->innerHTML( $element );
                            $r .= "</{$element->nodeName}>";
                    }    
            }    
            return $r;
    }

然后 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/)

function innerHTML( $contentdiv ) {
            $r = '';
            $elements = $contentdiv->childNodes;
            foreach( $elements as $element ) { 
                    if ( $element->nodeType == XML_TEXT_NODE ) {
                            $text = $element->nodeValue;
                            // IIRC the next line was for working around a
                            // WordPress bug
                            //$text = str_replace( '<', '<', $text );
                            $r .= $text;
                    }    
                    // FIXME we should return comments as well
                    elseif ( $element->nodeType == XML_COMMENT_NODE ) {
                            $r .= '';
                    }    
                    else {
                            $r .= '<';
                            $r .= $element->nodeName;
                            if ( $element->hasAttributes() ) { 
                                    $attributes = $element->attributes;
                                    foreach ( $attributes as $attribute )
                                            $r .= " {$attribute->nodeName}='{$attribute->nodeValue}'" ;
                            }    
                            $r .= '>';
                            $r .= $this->innerHTML( $element );
                            $r .= "</{$element->nodeName}>";
                    }    
            }    
            return $r;
    }

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文