Problems sending xml with php using HttpRequest

Posted on 2009-02-19
Last Modified: 2012-05-06
Any help much 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 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

Question by:michlcamp
    LVL 1

    Accepted 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.

    Author Comment

    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


    Author Comment

    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?

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
    Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
    The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
    The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now