开发者

Send SimpleXmlElement via Curl using POST request

开发者 https://www.devze.com 2023-03-29 06:11 出处:网络
How could I send SimpleXmlElement object via Curl using POST request type and receive SimpleXmlElement object back.

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 n开发者_高级运维ow 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.


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.


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( '<', '&lt;', $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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号