0
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.

Thomas Clayson
  • 28,448
  • 25
  • 135
  • 216

4 Answers4

1

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

Community
  • 1
  • 1
teemitzitrone
  • 2,230
  • 15
  • 15
1

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 { ... }

?>
Community
  • 1
  • 1
Pekka
  • 418,526
  • 129
  • 929
  • 1,058
1

I also found a solution using stream_context_create(). It gives you more control over what you're sending in the POST.

Here's a blog post explaining how to do it. It lets you easily specify the exact headers and body.

http://wezfurlong.org/blog/2006/nov/http-post-from-php-without-curl/

trestacos
  • 11
  • 1
0

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
  • 215,182
  • 48
  • 300
  • 305
  • 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 Oct 14 '10 at 08:33