Link to home
Start Free TrialLog in
Avatar of michlcamp
michlcamp

asked on

Problems sending xml with php using HttpRequest

Any help much appreciated...in advance. thanks.

I'm trying to send an xml string from an HttpRequest in a file named 'dom_send2.php'  to a receiver file named 'dom2.php' on a different server
Problem is with the sending file using HttpRequest
I've been working with the server tech admin and he tells me the HttpRequest class is installed properly and should work if I can get my syntax correct. Ok....well...I don't know how!

* It appears that my $string is not being sent or is sent empty
* addHeaders error

Perhaps you can point me in the right direction - I don't get it...my receiver file does fine when I use CURL to post the data - but I can't get the HttpRequest method to work.

I am getting the following errors:

Warning: HttpRequest::addHeaders() expects at most 1 parameter, 2 given in /sendpath/dom_send2.php on line 5

Warning: DOMDocument::loadXML() [domdocument.loadxml]: Empty string supplied as input in /mypath/dom2.php on line 5

Warning: simplexml_import_dom() [function.simplexml-import-dom]: Invalid Nodetype to import in /mypath/dom2.php on line 10

here is sending and receiving files code:
sending file: 'dom_send2.php'
 
$string = '<?xml version="1.0" encoding="UTF-8"?><root><response1>y</response1><response2>n</response2><response3>l</response3></root>';
$url ='http://sendpathdomain/dom2.php';
$req = &new HttpRequest($url, HttpRequest::METH_POST);
$req->addHeaders("Content-Type", "text/xml"); 
$req->setBody($string);
$req->send();
echo $req->getResponseBody();
------------------------------------------------------------------
------------------------------------------------------------------
receiving file: dom2.php
 
$dom = new DOMDocument();
$in = trim($HTTP_RAW_POST_DATA );
$dom->loadXML($in);
if (!$dom) {
	echo "error loading dom";
	exit;
}
$s = simplexml_import_dom($dom);
 
//var_dump($s);
//var_dump((bool)$s);
$responseabc = $s->response1;
$responseabc2 = $s->response2;
$responseabc3 = $s->response3;
echo "Response:<br><br>";
 
echo $responseabc;
echo "<br>";
echo $responseabc2;
echo "<br>";
echo $responseabc3;

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of jcallu
jcallu
Flag of Sweden image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of michlcamp
michlcamp

ASKER

Thanks,  that worked and is a step closer (I did figure out the headers issue)
I'm trying to parse the xml and convert the node values to variables...

what I get from this is the xml output (below) - and I assume that's coming from the
echo $req->getBody(); command - but as you can see in my receive file code, I'm trying to convert the xml to variables using simplexml, which I can then post to a db -
simplexml doesn't seem to be doing what it's supposed to, or the string isn't being imported into it.

I am able to parse xml  as desired with the receive file when I send it using CURL,  and so far I'm at least getting a response back from the HttpRequest method - but it's not taking my xml apart so I can create variables from each node.

do I want something other than "getBody()" at the end of my request?

thanks in advance

 <?xml version="1.0" encoding="UTF-8" ?> 
- <root>
  <response1>y</response1> 
  <response2>n</response2> 
  <response3>l</response3> 
  </root>

Open in new window

also,
the way I'm confirming that I'm receiving the xml and parsing it as desired is by echo'ing the results back by
$variablename1 = $simplexmloutput1
echo $variablename1
etc.

I know when I see that that the code has been received and is being manipulated on the receiving end.
make sense?