Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1034
  • 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"); 
$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

0
michlcamp
Asked:
michlcamp
  • 2
1 Solution
 
jcalluCommented:
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
http://php.net/wrappers.php

You can use the following code to get the string:

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

Then the remaining code should work correctly.
0
 
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>
  <response1>y</response1> 
  <response2>n</response2> 
  <response3>l</response3> 
  </root>

Open in new window

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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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