$post_data="dispnumber=567567567&extension=6";
$url="//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
Saravanan M PSaravanan M P
5211 gold badge7 silver badges12 bronze badges
answered Sep 20, 2013 at 9:36
Shakti PatelShakti Patel
3,6424 gold badges21 silver badges29 bronze badges
6
$curl = curl_init[];
curl_setopt_array[$curl, array[
CURLOPT_URL => "//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
Try something like:
$post_data="dispnumber=567567567&extension=6";
$url="//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
Not the answer you're looking for? Browse other questions tagged php api curl php-5.3 or ask your own question.
With application-x-www-form-urlencoded post in cURL, we should use http_build_query to generate URL-encode data and put them in CURLOPT_POSTFIELDS.
Example
function post[$url, $data, $headers]
{
$curl = curl_init[];
curl_setopt_array[$curl, array[
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true, // return the transfer as a string of the return value
CURLOPT_TIMEOUT => 0, // The maximum number of seconds to allow cURL functions to execute.
CURLOPT_POST => true, // This line must place before CURLOPT_POSTFIELDS
CURLOPT_POSTFIELDS => $data // Data that will send
]];
// Set Header
if [!empty[$headers]] {
curl_setopt[$curl, CURLOPT_HTTPHEADER, $headers];
}
$response = curl_exec[$curl];
$errno = curl_errno[$curl];
if [$errno] {
return false;
}
curl_close[$curl];
return $response;
}
Use it
The following is an example of sending an array.
$arr = [
"name" => "test",
"password" => "mypasswd",
"arr[0]" => 0,
"arr[1]" => 1,
];
$data = http_build_query[$arr];
$headers = ["User-Agent: Mozilla/5.0 [Macintosh; Intel Mac OS X 10_14_4] AppleWebKit/537.36 [KHTML, like Gecko] Chrome/79.0.3945.117 Safari/537.36"];
echo post["//localhost/response.php", $data, $headers];
response.php
print_r[$_REQUEST];
$contentType = $_SERVER["CONTENT_TYPE"];
print_r[$contentType];
Output
Array
[
[name] => test
[password] => mypasswd
[arr] => Array
[
[0] => 0
[1] => 1
]
]
application/x-www-form-urlencoded
The request header Content-ype is automatically set to application/x-www-form-urlencoded.