• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1046
  • Last Modified:

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"); 
echo $req->getResponseBody();
receiving file: dom2.php
$dom = new DOMDocument();
$in = trim($HTTP_RAW_POST_DATA );
if (!$dom) {
	echo "error loading dom";
$s = simplexml_import_dom($dom);
$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

  • 2
1 Solution
There are 2 issues in your files.

1: dom_send.php

  You should use an array for the addHeaders argument.
$req->addHeaders(array("Content-Type" => "text/xml"))
2: dom.php

In PHP depending on the headers and your php.ini configuration, you may not get the POST data in HTTP_RAW_POST_DATA. The best alternative is to read the socket php://input

You can use the following code to get the string:

$string = file_get_contents('php://input');

Then the remaining code should work correctly.
michlcampAuthor Commented:
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>

Open in new window

michlcampAuthor Commented:
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

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

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now