tags:

views:

291

answers:

4

Hiya.

i would like to know how to send a post request in curl and get the response page

thanks

+1  A: 

try the one in the comments: http://php.net/manual/en/curl.examples-basic.php

(but add curl_setopt($ch, CURLOPT_POST, 1) to make it a post instead of get)

or this example: http://php.dzone.com/news/execute-http-post-using-php-cu

that's a GET, not a POST
qntmfred
+1  A: 

I think you need to add

curl_setopt($curlHandle, CURLOPT_POSTFIELDS, $postFields);
Sinan
+1  A: 

You need to set the request to post using CURLOPT_POST and if you want to pass data with it, use CURLOPT_POSTFIELDS:

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, "http://www.example.com/");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);

$data = array(
    'username' => 'foo',
    'password' => 'bar'
);

curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

$contents = curl_exec($ch);

curl_close($ch);
Tatu Ulmanen
+1  A: 

What about something like this :

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.example.com/yourscript.php");
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, array(
    'field1' => 'some date',
    'field2' => 'some other data'
));
$result = curl_exec($ch);
curl_close($ch);

// result sent by the remote server is in $result


For a list of options that can be used with curl, you can take a look at the page of curl_setopt.

Here, you'll have to use, at least :

  • CURLOPT_POST : as you want to send a POST request, and not a GET
  • CURLOPT_RETURNTRANSFER : depending on whether you want curl_exec to return the result of the request, or to just output it.
  • CURLOPT_POSTFIELDS : The data that will be posted -- can be written directly as a string, like a querystring, or using an array


And don't hesitate to read the curl section of the PHP manual ;-)

Pascal MARTIN