Cheluis
Cheluis

Reputation: 1412

Http_Request2 POST request not working

Good morning fellows. I'm doing some work with Http_Request2 lib and I'm facing this situation. Im trying to make a POST request to certain URL. If I add the parameters on the query string of the url, i.e.

$request->setUrl('127.0.0.1/something.php?parm1=x&parm2=y');
$request->setMethod(HTTP_Request2::METHOD_POST);

It works fine, but I want to set the parameters with the addPostParameter method:

$request->setUrl('127.0.0.1:8888/something.php');
$request->addPostParameter('parm1', $somevariable);
$request->addPostParameter($some_array_with_parms);

This way doesnt work. The URL indeed is to localhost port 8888.

Is something that I'm missing? Some type of configuration for POST request?

Thanks in advance

Upvotes: 3

Views: 2969

Answers (1)

Jirilmon
Jirilmon

Reputation: 1944

Here is a sample code:

try {

    $request = new HTTP_Request2('http://127.0.0.1:8888/something.php');
    $request->setMethod(HTTP_Request2::METHOD_POST)
        ->addPostParameter('parm', 'value1') //single param
        ->addPostParameter(array('parmType' => 'val11','parm2' => 'val12')); //multiple
    $response = $request->send()->getBody();

} catch (Exception $exc) {
    echo $exc->getMessage();
}

Note: make sure the port 8888 is open.

Upvotes: 1

Related Questions