Search code examples
phpweb-servicesrestpearhttp-put

How to send data in PUT method using PEAR PHP Http_Request2 Library


I'm using the PEAR library Http_Request2. I've been unsuccessfully searching for documentation on how to make a PUT request and pass parameters to a webservice. Can anyone provide some help?

For a POST request, it's easy:

 $request = new HTTP_Request2 ( "http://my.url.com");
 $request->setMethod(HTTP_Request2::METHOD_POST);
 $request->addPostParameter('data', "blah"); //easy to add post params...
 $response = $request->send();

However, I can't figure how to send data when changing the method to PUT:

 $request = new HTTP_Request2 ( "http://my.url.com");
 $request->setMethod(HTTP_Request2::METHOD_PUT);

 // ?????  missing secret sauce to add data to put request....

 $response = $request->send();

Anyone lend a hand?


Solution

  • You need to use setBody() to set your PUT data. Don't forget the header, e.g.

    $request->setHeader('Content-type: application/json');
    $request->setBody('{"foo":"bar"}');
    $response = $request->send();