[SOLVED] Posting XML to a Server Using PEAR HTTP_Request

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
TipPro
Forum Commoner
Posts: 35
Joined: Wed Mar 15, 2006 6:39 pm

[SOLVED] Posting XML to a Server Using PEAR HTTP_Request

Post by TipPro »

I need to post xml to a server. The url includes a username, password, and action. I have never done this before so I am trying to use PEAR's HTTP_Request but it is not working. If there is a better way of achieving this, please send me down that direction.

Code: Select all

 
  $sendURL = 'https://www.domain.com/apps/mod?username=someUser&password=somePass&action=addNote';
 
  $sendData = '<?xml version="1.0" ?>
                 <Tips>
                   <Note>
                     <Id>123456</Id>
                     <Active>1</Active>
                     <Text>This is a note.</Text>
                   </Note>
                  </Tips>';
 
  $req =& new HTTP_Request($sendURL);
  if(!PEAR::isError($req->sendRequest())){
    $req->addHeader("Content-Type", "text/xml");
    $req->addHeader("Content-Length", strlen($sendData));
    $req->setMethod(HTTP_REQUEST_METHOD_POST);
    $req->addRawPostData($sendData, true);
    $req->sendRequest();
    echo "success (".$req->getResponseBody().")";
  }
 
Response...

Code: Select all

 
success (
Request Entity Too Large
The requested resource
/cgi-bin/apavm/apps/mod
does not allow request data with POST requests, or the amount of data provided in the request exceeds the capacity limit.
Request Entity Too Large
The server encountered an internal error or misconfiguration and was unable to complete your request.
 
Please contact the server administrator, someone@domain.com and inform them of the time the error occurred, and anything you might have done that may have caused the error.
 
More information about this error may be available in the server error log.
 
Additionally, a 500 Internal Server Error error was encountered while trying to use an ErrorDocument to handle the request.
 
)
 
Last edited by TipPro on Thu Mar 27, 2008 1:01 pm, edited 2 times in total.
timrkoop
Forum Newbie
Posts: 5
Joined: Fri Oct 19, 2007 11:01 am

Re: Posting XML to a Server Using PEAR HTTP_Request

Post by timrkoop »

I've never done this before either, but here are my thoughts:

When you (your browser normally) sends post data to a web server, the post data needs to be formatted correctly just like the data in the query string needs to be formatted correctly. I think the method addRawPostData you are using is for bypassing the formatting of the data. I'm pretty sure you want HTTP_Request to format the data for you into the correct format for post data. So try using the method addPostFields instead, something like this (untested):

Code: Select all

$req->addPostFields(array('xmldata'=>$sendData));
This way you can get the username with $_GET['username'] and the data with $_POST['xmldata'].

If I were you I would consider putting all the information into the post variables, like this:

Code: Select all

$sendURL = 'https://www.domain.com/apps/mod';
$req->addPostFields(array('xmldata'=>$sendData, 'username'=>'someUser', 'password'=>'somePass','action'=>'addNote'));
Then all your information is available with $_POST[].

--
Tim
TipPro
Forum Commoner
Posts: 35
Joined: Wed Mar 15, 2006 6:39 pm

Re: Posting XML to a Server Using PEAR HTTP_Request

Post by TipPro »

Thanks for your help Tim.

I did get it working after using addPostData() instead of addRawPostData()
Post Reply