How to send PHP input stream data using curl? - php

POST
On my local machine, I want to use cURL as an HTTP client like so:
curl -X POST -F 'email=derp#example.com' -F 'password=blink182' http://example.com
The above curl statement uses the HTTP POST method which can be retrieved in PHP like so:
echo $_POST['email']; // derp#example.com
echo $_POST['password']; // blink182
php://input
However, what I really wanted is the data from the PHP input stream php:://input and not from the POST method $_POST.
The PHP input stream can be retrieved in PHP like so:
$input = json_decode( file_get_contents( 'php://input' ) );
echo $input->email; // derp#example.com
echo $input->password; // blink182
Which brings me to my question, how to send PHP input stream data using curl?

From the PHP website:
php://input is a read-only stream that allows you to read raw data from the request body. php://input is not available with enctype="multipart/form-data".
So if you specify the Content-Type as application/json using -H "Content-Type: application/json" you ought to get it in the input stream
complete example:
curl -X POST https://reqbin.com/echo/post/json
-H 'Content-Type: application/json'
-d '{"email":"derp#example.com","password":"blink182"}'

Related

How to get Header & data-raw Of Curl Response from PHP

Using third party service; they are providing webhook services; I have given my url to send the response on my page:
Example: https://abcd.com/postback.php
Getting curl response on postback.php; to read the POST data; I am using following code:
$postdata = file_get_contents("php://input");
but not able to read the header & data content.
Sample Response that they are sending:
curl -L -X POST 'https://abcd.com/postback.php' -H 'x-changenow-signature: xcgh123POPI6727kslsldjsd' -H 'Content-Type: application/json' --data-raw '{"status":"finished","payinHash":"31mrEanXyv","payoutHash":"485d8dz","payinAddress":"3Nups0bF","payoutAddress":"1K1LcQSPZVj2dM","fromCurrency":"sol","toCurrency":"btc","amountSend":0.06676453,"amountReceive":0.00010273,"refundAddress":"Bk1qVzc3GULcJQ0NoW4DsepRJHevzAD8ynznEQ1aNzGq","id":"f7bed8a9e4a350","updatedAt":"2022-06-23T11:22:38.728Z","createdAt":"2022-06-23T11:09:56.403Z","expectedReceiveAmount":0.0001024,"isPartner":false}
Please suggest how to get header and data part.
The headers are in $_SERVER. Use $_SERVER['HTTP_X_CHANGENOW_SIGNATURE'] to get the x-changenow-signature header.
The data content is in $postdata. Use
$data = json_decode($postdata);
to convert it to an object. Then you can use $data->status, $data->payinHash, etc. to get the fields of the post data.

How to Post data from external HTML form to Eloqua Form using cURL

I want to post data from an external HTML form to a simple form created in Eloqua, using cURL method. I tried to follow this documentation but I am not able to post data to Eloqua.
When I try from command line (Windows) -
curl --user "usercreds" --header "Content-Type: application/json" --request POST --data "{"testfirstname":"abc","testlastname":"def","singleCheckbox":1}" https://secure.p0{POD Number}.eloqua.com/api/REST/1.0/data/form/{formid}
I get below error:
[{"type":"ObjectValidationError","property":"fieldValues","requirement":{"type":"NoDuplicatesRequirement"},"value":""}]
When I try from PHP, as mentioned here https://www.eehelp.com/question/using-curl-to-repost-to-eloqua-data/
or https://github.com/fredsakr/eloqua-php-request the curl returns HTTP code 0.
This is a simple form created in Eloqua without any validations.
I do not know what I am doing wrong here.

How can I send a json data along with a file in a curl request to handle by Flask?

Problem:
I want to send a file with -F option and along with a json data with -d to the flask route.But only either i can implement.
Code I tried.
curl -X POST -H "Content-Type: application/json" -d '{"username":"xyz","password":"xyz"}' http://192.168.150.113/test
Flask Code:
#app.route('/test',methods = ['GET', 'POST'])
def test():
if request.method == 'POST':
data = request.data
data = json.loads(data)
return 'success'
With only File :
curl -X POST -F file=#sample.txt http://192.168.150.113/test
#app.route('/process' , methods = ['GET', 'POST'])
def process():
if request.method == 'POST':
f = request.files['file']
if f:
try:
filename = secure_filename(f.filename)
f.save( os.path.join(app.config['UPLOAD_FOLDER'], filename ))
return 'success'
But without sending seperate request I want to combine these two POST request and handle using flask..
Is there any way I can do this?
Use the -F option to send data with multipart/form-data
curl -X POST -H "Content-Type: multipart/form-data" -F "file=#sample.txt" -F "username=xyz" -F "password=xyz" http://localhost:5000/test
You cannot send file with json together as they have different Content-Type. Alternatively, you can stringify your json and send them with multipart/form-data. For example, you can send a form like the following:
curl -X POST -H "Content-Type: multipart/form-data" -F "file=#sample.txt" -F "json_data='{\"username\":\"xyz\",\"password\":\"xyz\"}'" http://localhost:5000/test
And in python, you can get this json by request.form.get("json_data"). It is more robust than passing key-value pairs through plain multipart/form-data as it support much more complicated structure.

How put the data back after getting it from the stream?

I am getting post data like this
$data = file_get_contents('php://input');
I want the data put back to the stream, so I modified the method following instaed of above method
$stream = fopen('php://temp', 'w+');
stream_copy_to_stream(fopen('php://input', 'r'), $stream);
rewind($stream);
But it is not working. Can any one tell where I did wrong?
This case is need in wordpress plugin developement. If one plugin reads the post data, other plugin not able to get that data.
Thanks
As stated in the documentation, php://input can only be read once and doesn't support seek operations.
The only way to 'read' the input stream multiple times is through passing its copy in your program.
PHP input stream contains request body and your sample will work only if request will contain body.
curl -X POST -H "Content-Type: application/json" -H "Cache-Control: no-cache" -d 'sample body' "YOUR_ADDRESS"
For check add to code:
echo fgets($stream);
php://input is not available with enctype="multipart/form-data".
http://php.net/manual/en/wrappers.php.php

receiving XML data via POST then displaying it

I am having difficulties figuring this out. I can see I am posting XML and I see the length of it but I cannot figure out how to display or access the POST DATA
CONTENT-TYPE: text/xml<br />
CONTENT-LENGTH: 640<br />
separately I have
curl -k -H 'content-type: text/xml' -d 'XML_DATA_HERE' https://ip/page.php
Try
$xml = file_get_contents('php://input');
http://docs.php.net/manual/en/wrappers.php.php says:
php://input allows you to read raw POST data. It is a less memory intensive alternative to $HTTP_RAW_POST_DATA and does not need any special php.ini directives. php://input is not available with enctype="multipart/form-data".

Categories