How to POST data to php from ash (bash) - php

I have url (www.blabla.web.id/proses_data.php) online. And I want to submit data to that url from my bash script. I'm using ash, bash inside OpenWRT.
I'm trying this technique,
#!/bin/sh
elinks -dump http://www.blabla.web.id/proses_data.php?data=thisisthedata
But it use GET method. How to use POST method?

elinks is a program for browsing the web in text mode. you may not post data throgh it.
but Linux provide beautiful tools for POST data
with this nice and little command
curl --data "param1=value1&param2=value2" http://hostname/resource
Also POST if web response in JSON then
curl -X POST -H "Accept: Application/json" -H "Content-Type: application/json" http://someHostName/someEndpoint -d '{"id":"IDVALUE","name":"Mike"}' | grep }| python -mjson.tool
Aslo some little trick here
Add this function in php script
function getInput() {
$fr = fopen("php://stdin", "r");
while (!feof ($fr)) {
$input .= fgets($fr);
}
fclose($fr);
return $input;
}
$takethis = getInput();
Now in bash do like
echo 22333333 | php my.php

Related

How to send PHP input stream data using curl?

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"}'

cURL and PHP post request

I am new at cURL and can`t understand. I have for example this code
curl -d '
{
"data": {
"title": "Bubble Nebula",
"body": "It`s found today at 21:00",
"icon": "https://peter-gribanov.github.io/serviceworker/Bubble-Nebula.jpg",
"image": "https://peter-gribanov.github.io/serviceworker/Bubble-Nebula_big.jpg",
"click_action": "https://www.nasa.gov/feature/goddard/2016/hubble-sees-a-star-inflating-a-giant-bubble"
}
"to": "YOUR-TOKEN-ID"
}' \
-H "Content-Type: application/json" \
-H "Authorization: key=AAAAaGQ_q2M:APA91bGCEOduj8HM6gP24w2LEnesqM2zkL_qx2PJUSBjjeGSdJhCrDoJf_WbT7wpQZrynHlESAoZ1VHX9Nro6W_tqpJ3Aw-A292SVe_4Ho7tJQCQxSezDCoJsnqXjoaouMYIwr34vZTs" \
-X POST "https://fcm.googleapis.com/fcm/send"
How should i do it work well with php?
Most of the time I just use file_get_contents() for my requests, but that trick is sadly only for GET requests.
However, I recently discovered a new trick that can be used with Post and Get requests(Including everything else that curl can) and still avoids the horrible php library phpcURL, And the best part is, that you don't even need to change your curl command. Try the code below.
<?php
$curl = "your curl command" // paste your curl command here
exec("$curl > curloutput.txt");
$response = file_get_contents("curloutput.txt");
echo($response);
?>
This code should execute your given curl command and return the output. Yes, I know this sounds like a really janky method but it works every time bugless and it is super easy to use.

Curl request not working using php script

I have curl using voxbone API action method pass "did_dropdown" parameter value to get country wise dids. But another thing I am passing action method "didlist_type_city" parameter value not any response with pass country.
Note : Get url is working on terminal command line but postman api not any response get
curl -u xxxxx:xxxxx -H "Content-type: application/json" -H "Accept: application/json" "https://sandbox.voxbone.com/ws-voxbone/services/rest/inventory/did?didIds='xxxxxx'&pageNumber=0&pageSize=1&countryCodeA3=IND"

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.

Restler3: DELETE Body JSON

I really don't understand what I should do to make a DELETE request.
I want to use JSON as in/output only, so also a DELETE request should receive JSON.
But that doesn't make the goal:
public function delete($request_data = NULL)
$request_data is nil every time. I'm trying it by using cURL:
curl -X DELETE http://…/API/index.php/test.json -H "Content-Type: application/json" -d '{"key":"test","value":"1337"}'
Can someone explain that to me?

Categories