views:

67

answers:

3
function do_post_request($url, $data, $optional_headers = null) {

    $request = new HttpRequest($url, HttpRequest::METH_POST);
    $request->setBody($data);
        $response = $request->send();
        return $response->getBody();

}

This piece of code doesn't seem to be working, and seems to crash my script. I don't know if its because I don't have the php_http module, but is there an equivalent I can use?

For instance curl? I have tried curl, but I don't know much about it, and with curl I got a "bad request" returned from the server I was trying to connect to with a 400 status.

Anything would be good

Thanks

Tom

Edit:

function do_post_request($url, $data, $optional_headers = null) {


    $request = new HttpRequest($url, HttpRequest::METH_POST);
     $request->setBody($data);
  $response = $request->send();
  return $response->getBody();

}


echo "before";

$response = do_post_request($url, $data);
echo "After";

Doing that makes "before" appear on the page. But no "After".

After managing to turn error reporting on I get this:

Fatal error: Class 'HttpRequest' not found in /home/sites/ollysmithwineapp.com/public_html/mellowpages/geocode.php on line 25

So I need another way to do the HTTP Request.

A: 

There is no HttpRequest::setBody() method. You should use the addPostFields function instead, using an associative array:

function do_post_request($url, $data, $optional_headers = null) {
    $request = new HttpRequest($url, HttpRequest::METH_POST);
    $request->setPostFields($data);
    $response = $request->send();
    return $response->getBody();
}

$responseBody = do_post_request('http://www.example.com',array('examplefield'=>'exampledata'));
lonesomeday
I need to send the contents of the $data variable (which at the moment is the contents of an XML document). How would I do this?
Thomas Clayson
A: 

Sure HTTP extension is installed and configured correctly?

Installation/Configuration

Installation

This » PECL extension is not bundled with PHP.

Information for installing this PECL extension may be found in the manual chapter titled Installation of PECL extensions. Additional information such as new releases, downloads, source files, maintainer information, and a CHANGELOG, can be located here: » http://pecl.php.net/package/pecl_http.

and maybe cURl is the way to go

maggie
that second link did it. :) Thanks
Thomas Clayson
+1  A: 

Stolen from this question. You can insert $data directly where CURLOPT_POSTFIELDS is set in place of the query string.

<?php
//
// A very simple PHP example that sends a HTTP POST to a remote site
//

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL,"http://www.mysite.com/tester.phtml");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,
            "postvar1=value1&postvar2=value2&postvar3=value3");

// receive server response ...
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$server_output = curl_exec ($ch);

curl_close ($ch);

// further processing ....
if ($server_output == "OK") { ... } else { ... }

?>
Pekka
and that can take an XML string?
Thomas Clayson
@Thomas take how? What do you mean?
Pekka
basically $data is the contents of an XML file, I'm assuming it can, and I'm trying this. :)
Thomas Clayson
@Thomas you can, just make sure you add a field name (unless you're parsing the raw POST request on the receiving end).
Pekka
thanks a lot pekka :) the answer came from the link in @maggie's answer, however you were a great help, thank you. :)
Thomas Clayson