How send post request with x www form urlencoded body php?

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

asked Sep 20, 2013 at 9:32

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

?>

answered Sep 20, 2013 at 9:36

Shakti PatelShakti Patel

3,6724 gold badges21 silver badges29 bronze badges

6

 $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);
 }

answered May 5, 2017 at 13:34

GeekHareGeekHare

811 silver badge2 bronze badges

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;

answered Sep 20, 2013 at 9:42

anupamanupam

7365 silver badges11 bronze badges

0

Php Curl Post Application/X-Www-Form-Urlencoded With Code Examples

Hello everyone, in this post we will look at how to solve the Php Curl Post Application/X-Www-Form-Urlencoded problem in the programming language.

//this send application/x-www-form-urlencoded                                                                                                     
 function httpPostXform($url, $data) {                                                                 
    $curl = curl_init($url);                                                                            
    curl_setopt($curl, CURLOPT_POST, true);                                                             
    curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));                                    
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);                                                   
    curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));   
    $response = curl_exec($curl);                                                                       
    curl_close($curl);                                                                                  
    return $response;                                                                      
}                                                                                                       
$r = httpPostXform("https://www.somesite.com",array("user_id"=>10,"sex"=>"male"));

We were able to fix the Php Curl Post Application/X-Www-Form-Urlencoded problem by looking at a number of different examples.

How do you pass X www form Urlencoded parameters in php Curl?

To post form data with Curl, you can use one of two command-line parameters: -F (–form) or -d (–data). The -F command-line parameter sends form data with the multipart/form-data content type, and the -d command-line parameter sends form data with the application/x-www-form-urlencoded content type.01-Jul-2022

How send post request with X www form Urlencoded body in PHP?

“php curl post application/x-www-form-urlencoded” Code Answer

  • //this send application/x-www-form-urlencoded.
  • function httpPostXform($url, $data) {
  • $curl = curl_init($url);
  • curl_setopt($curl, CURLOPT_POST, true);
  • curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));

How to post form data using Curl in php?

curl_error($curl) . '" – Code: ' . curl_errno($curl)); } curl_close($curl); var_dump($result); ?> Show activity on this post.

  • no, it shouldn't work, and if this works for you, you should upgrade your PHP installation, because your PHP version is EOL.
  • no it doesnt.

What is Curlopt_postfields?

CURLOPT_POSTFIELDS. The full data to post in a HTTP "POST" operation. To post a file, prepend a filename with @ and use the full path. The filetype can be explicitly specified by following the filename with the type in the format ';type=mimetype'.

How do you send a POST request using curl command?

When the -F option is used, curl sends the data using the multipart/form-data Content-Type. Another way to make a POST request is to use the -d option. This causes curl to send the data using the application/x-www-form-urlencoded Content-Type.23-Jul-2020

How do I curl POST request?

Users can send data with the POST request using the -d flag. The following POST request sends a user and a pass field along with their corresponding values. POSTing with curl's -d option will include a default header that looks like: Content-Type: application/x-www-form-urlencoded .

How can I send form data in POST request?

To post HTML form data to the server in URL-encoded format, you need to make an HTTP POST request to the server and provide the HTML form data in the body of the POST message. You also need to specify the data type using the Content-Type: application/x-www-form-urlencoded request header.15-Sept-2022

How do you send data in a post method?

To send data using the HTTP POST method, you must include the data in the body of the HTTP POST message and specify the MIME type of the data with a Content-Type header. Below is an example of an HTTP POST request to send JSON data to the server. The size and data type for HTTP POST requests is not limited.15-Sept-2022

How do I send a file in a post request?

Steps to reproduce the behavior:

  • create a new request.
  • set body to form-data.
  • type in 'image' for key and select a file for value.
  • type in 'affiliation' for key and type in affiliation value.
  • type in 'name' for key and type in 'test' for value.
  • select “Post”
  • type in the URL.
  • click the send button.

What are the methods to submit forms in PHP?

PHP – A Simple HTML Form When the user fills out the form above and clicks the submit button, the form data is sent for processing to a PHP file named "welcome.php". The form data is sent with the HTTP POST method. Your email address is: <?php echo $_POST["email"]; ?>

How send POST request with X www form Urlencoded body in PHP?

“php curl post application/x-www-form-urlencoded” Code Answer.
//this send application/x-www-form-urlencoded..
function httpPostXform($url, $data) {.
$curl = curl_init($url);.
curl_setopt($curl, CURLOPT_POST, true);.
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));.

How do I send a POST request using PHP?

Send POST Request in PHP.
Use the CURL-less Method Using the stream_context_create() and file_get_contents() Functions to Send a POST Request in PHP..
Use the CURL to Send the POST Request in PHP..
Create a User-Defined Function That Uses the CURL to Send the POST Request in PHP..

How post URL in PHP?

If you're looking to post data to a URL from PHP code itself (without using an html form) it can be done with curl. It will look like this: $url = 'http://www.someurl.com'; $myvars = 'myvar1=' . $myvar1 .

How do you pass X www form Urlencoded parameters in curl?

To post form data with Curl, you can use one of two command-line parameters: -F (--form) or -d (--data). The -F command-line parameter sends form data with the multipart/form-data content type, and the -d command-line parameter sends form data with the application/x-www-form-urlencoded content type.