سؤال

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?

هل كانت مفيدة؟

المحلول

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();
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top