How to Send a Post Request With PHP

Send POST request with PHP

You can do a POST request using curl in PHP.

// initiate the curl request
$request = curl_init();

curl_setopt($request, CURLOPT_URL,"http://www.otherDomain.com/getdata");
curl_setopt($request, CURLOPT_POST, 1);
curl_setopt($request, CURLOPT_POSTFIELDS,
"var1=value1&var2=value2");

// catch the response
curl_setopt($request, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($request);

curl_close ($request);

// do processing for the $response

How to make a POST request with PHP given the following HTTP + JSON

The first blob are the headers that you need to send with your request. The JSON is the post body payload.

<?php

$url = 'https://wwrm.workwave.com/api/v1/callback';

$data = '
{
"url": "https://my.server.com/new-callback",
"signaturePassword": "g394g732vhsdfiv34",
"test": true
}
';

$additional_headers = array(
'Accept: application/json',
'X-WorkWave-Key: YOUR API KEY',
'Host: wwrm.workwave.com',
'Content-Type: application/json'
);

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $additional_headers);

$server_output = curl_exec ($ch);

echo $server_output;

Make POST request from php file to another php file

This is the code sample I used. Thanks to @Amirhossein Shahbazi for the suggestion.
Link to the question: send http request with CURL to local file

<?php
$url = 'http://localhost:8000/fe.php';
// The submitted form data, encoded as query-string-style
// name-value pairs
$body = 'monkey=uncle&rhino=aunt';
$c = curl_init ($url);
curl_setopt ($c, CURLOPT_POST, true);
curl_setopt ($c, CURLOPT_POSTFIELDS, $body);
curl_setopt ($c, CURLOPT_RETURNTRANSFER, true);
$page = curl_exec ($c);
curl_close ($c);
?>

When I send a POST request from a JavaScript file to a PHP file in the same application, the PHP script always reads it as a GET request. Why?

OK, I can't tell you why my form data wasn't being posted and I have confirmed that it was being sent as a GET request. But this is how I solved this problem.

I added a hidden field in the form and changed the button of type "submit" to type "button". I then grabbed that button via JS and set a click listener on it. When the user presses the button I first assign my JSON serialized survey object to the hidden field. Then I manually submitted the form and it was sent via POST to the PHP script. The form's method was always "post" but that didn't seem to matter initially. I don't know why but here is my input in the form:

<input name="survey" id="survey" type="hidden" value="">

then I grab the button which looks like this:

<button type="button" class="btn btn-success" id="saveSurvey">Save Survey</button>

notice it's no longer of type "submit".
I then add a click event listener to it and call this function when clicked:

function saveSurvey(e) {
document.getElementById('survey').value = JSON.stringify(survey)
surveyForm.submit()
}

This worked. I don't know if I fully answered my own question so if someone else comes along with a better answer I will adjust this post. Thank you everyone for your help.

Sending POST request to PHP file via arduino

the solution as per Tangentially Perpendicular comments was to change content type header to

    http.addHeader("Content-Type", "application/x-www-form-urlencoded");

and Posted data to

    int httpCode = http.POST("temp=15");


Related Topics



Leave a reply



Submit