-2

I testing using curl for post. i created 3 php files "app, source, result". first php is app to post. second is source. third to process post value. How to post from app to source.php and get result,php. I get nothing.

first php

$data = [ 'user' => 'myself' ];
$headers = [
"User-Agent: Opera/9.80 (J2ME/MIDP; Opera Mini/4.0.10992/35.5561; U; hr) Presto/2.8.119 Version/11.10",
 "Content-Type: application/x-www-form-urlencoded",
];

$options = [
CURLOPT_POST => TRUE,
CURLOPT_POSTFIELDS => $data,
CURLOPT_HTTPHEADER => $headers
];

$ch = curl_init("localhost/source.php');
curl_setopt_array($ch, $options);
$res = curl_exec($ch);
curl_close($ch);
echo $res;

Source php

<form action="result.php" method="post">
<input type="hidden" name="user">
</form>

Result php

<?php
if(isset($_POST['user'])){ echo $_POST['user']; }
?>
Nipun Tharuksha
  • 2,496
  • 4
  • 17
  • 40
olivia
  • 9
  • 1
  • 5

2 Answers2

0

When sending data via a POST or PUT request, two common formats (specified via the Content-Type header) are:

application/json application/x-www-form-urlencoded Many APIs will accept both formats, so if you're using curl at the command line, it can be a bit easier to use the form urlencoded format instead of json because

the json format requires a bunch of extra quoting curl will send form urlencoded by default, so for json the Content-Type header must be explicitly set This gist provides examples for using both formats, including how to use sample data files in either format with your curl requests.

curl usage For sending data with POST and PUT requests, these are common curl options:

request type

-X POST -X PUT content type header

-H "Content-Type: application/x-www-form-urlencoded"

-H "Content-Type: application/json"

data

form urlencoded: -d "param1=value1&param2=value2" or -d @data.txt json: -d '{"key1":"value1", "key2":"value2"}' or -d @data.json Examples POST application/x-www-form-urlencoded application/x-www-form-urlencoded is the default:

curl -d "param1=value1&param2=value2" -X POST http://localhost:3000/data explicit:

curl -d "param1=value1&param2=value2" -H "Content-Type: application/x-www-form-urlencoded" -X POST http://localhost:3000/data with a data file

curl -d "@data.txt" -X POST http://localhost:3000/data POST application/json curl -d '{"key1":"value1", "key2":"value2"}' -H "Content-Type: application/json" -X POST http://localhost:3000/data with a data file

curl -d "@data.json" -X POST http://localhost:3000/data

Paolo Mossini
  • 1,064
  • 2
  • 15
  • 23
-2

send parameter with post method on cmd.exe:

Sample Command :

curl -d "param1=value1&param2=value2" -X POST http://sample.com/index.php