50
$post_data="dispnumber=567567567&extension=6";
$url="http://xxxxxxxx.xxx/xx/xx";

I need to post this $post_data using cURL php with header application/x-www-form-urlencoded i am new for curl any one help this out.

Saravanan M P
  • 561
  • 1
  • 7
  • 12

3 Answers3

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

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL,"http://xxxxxxxx.xxx/xx/xx");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,
            "dispnumber=567567567&extension=6");
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));


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

$server_output = curl_exec ($ch);

curl_close ($ch);

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

?>
Shakti Patel
  • 3,762
  • 4
  • 22
  • 29
  • i need to post with this header application/x-www-form-urlencoded – Saravanan M P Sep 20 '13 at 09:42
  • ADD THIS curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded')); – Shakti Patel Sep 20 '13 at 09:47
  • 4
    I'm strongly agree that [**`http_build_query`**](http://php.net/manual/en/function.http-build-query.php) should be used to compose `CURLOPT_POSTFIELDS` value from array within `application/x-www-form-urlencoded`. – Paul T. Rawkeen Jun 07 '17 at 10:00
  • http_build_query needed with application/x-www-form-urlencoded. otherwise your data wont be found. Solved bug by your comment thanks @user3338098 ex:curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($arr)); – Munaf Hajir May 05 '20 at 08:31
10
 $curl = curl_init();
 curl_setopt_array($curl, array(
            CURLOPT_URL => "http://example.com",
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => "",
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 30,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => "POST",
            CURLOPT_POSTFIELDS => "value1=111&value2=222",
            CURLOPT_HTTPHEADER => array(
                "cache-control: no-cache",
                "content-type: application/x-www-form-urlencoded"
            ),
        ));
 $response = curl_exec($curl);
 $err = curl_error($curl);

 curl_close($curl);

 if (!$err)
 {
      var_dump($response);
 }
GeekHare
  • 101
  • 1
  • 3
4

Try something like:

$post_data="dispnumber=567567567&extension=6";
$url="http://xxxxxxxx.xxx/xx/xx";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));   
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
$result = curl_exec($ch);

echo $result;
Tristan CHARBONNIER
  • 1,119
  • 16
  • 12
anupam
  • 756
  • 5
  • 11