POST data to a URL in PHP
If you're looking to post data to a URL from PHP code itself (without using an html form) it can be done with curl. It will look like this:
$url = 'http://www.someurl.com';
$myvars = 'myvar1=' . $myvar1 . '&myvar2=' . $myvar2;
$ch = curl_init( $url );
curl_setopt( $ch, CURLOPT_POST, 1);
curl_setopt( $ch, CURLOPT_POSTFIELDS, $myvars);
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt( $ch, CURLOPT_HEADER, 0);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec( $ch );
This will send the post variables to the specified url, and what the page returns will be in $response.
Get values through post method from URL
As per your screenshot you are sending your empid
through query parameter so you need to access that as follows
<?php
if (isset($_GET['empid'])) {
echo $_GET['empid'];
}else{
// else part
}
also for that you need to Request Url in Postman using GET
method.
But as you have stated that you want to send empid
through POST
in postman, you have to send it through form-data
in Postman and access it as $_POST["empid"];
. following is the screenshot for your reference
else there is another option where you can send the POST
data through body
as row json
and access it as
$rawPostData = file_get_contents('php://input');
$jsonData = json_decode($rawPostData);
and $post
will contain the raw data
. And you can send it through postman as in following screenshot.
Post data to external URL
CURL should be enough to post data to different url (in this case xyz.com). Make sure the csrf is disabled since it's a POST request from different laravel app In Laravel 5, How to disable VerifycsrfToken middleware for specific route? if this still fail, check the log on laravel app
Send/Post Form Data to Url with PHP
http_build_query
(PHP 5, PHP 7) http_build_query — Generate URL-encoded query string
Example:
<?php
$data = array(
'foo' => 'bar',
'baz' => 'boom',
'cow' => 'milk',
'php' => 'hypertext processor'
);
echo http_build_query($data) . "\n";
echo http_build_query($data, '', '&');
?>
The above example will output:
foo=bar&baz=boom&cow=milk&php=hypertext+processor
foo=bar&baz=boom&cow=milk&php=hypertext+processor
The rest depends on your flow logic. To post to another script:
From this answer:
Possibly the easiest way to make PHP perform a POST request is to use
cURL, either as an extension or simply shelling out to another
process. Here's a post sample:
// where are we posting to?
$url = 'http://foo.com/script.php';
// what post fields?
$fields = array(
'field1' => $field1,
'field2' => $field2,
);
// build the urlencoded data
$postvars = http_build_query($fields);
// open connection
$ch = curl_init();
// set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, count($fields));
curl_setopt($ch, CURLOPT_POSTFIELDS, $postvars);
// execute post
$result = curl_exec($ch);
// close connection
curl_close($ch)
How to send POST data to external url and open the target url
I think you're looking for something like this: PHP Redirect with POST data
You will need a second page, that submits the post data to a completely different url.
How can I pass POST parameters in a URL?
You could use a form styled as a link. No JavaScript is required:
<form action="/do/stuff.php" method="post">
<input type="hidden" name="user_id" value="123" />
<button>Go to user 123</button>
</form>
CSS:
button {
border: 0;
padding: 0;
display: inline;
background: none;
text-decoration: underline;
color: blue;
}
button:hover {
cursor: pointer;
}
See: http://jsfiddle.net/SkQRN/
How to read post url request from javascript in PHP
Ok I will explain with example,
If you want to get url parameter values to you have to use,
<script>
let my_variable='<?php echo $_GET['url_param_name'];?>';
</script>
above for more help and understanding. Now you want to send form data to php for processing as I got your answer.
This is sample form.
<form id="my_form" name"my_form" method="POST" onsubmit="return send();">
First name:<br>
<input type="text" name="first_name" value="Mickey">
<br>
Last name:<br>
<input type="text" name="last_name" value="Mouse">
<br><br>
<input type="submit" value="Submit">
</form>
To post above form I will use javascript function,
<script>
function send() {
$.ajax
({
type: 'POST',
url: './path/your_php_file_where_form_data_processed.php',
data:$('#my_form').serialize(),
success: function () {
// do what you need to do on succeess
},
error: function (x, e) {
// for error handling
if (x.status == 0) {
console.log('You are offline!! - Please Check Your Network.');
} else if (x.status == 404) {
console.log('Requested URL not found.');
} else if (x.status == 500) {
console.log('Internal Server Error.');
} else if (e == 'parsererror') {
console.log('Error. - Parsing JSON Request failed.');
} else if (e == 'timeout') {
console.log('Request Time out.');
} else {
console.log('Unknown Error. - ' + x.responseText);
}
}
});
return false;
}
</<script>
Now you need to check carefully your form element names. In php file. Let's look it.
<?php
//include_once './../../classes/Database.php'; import if you have database configurations
//session_start(); make sure to use sessions if your site using sessions
if(isset($_POST))
{
var_dump($_POST); //this will echo your form inputed data.
//if you want use one by one posted data
echo $_POST['first_name'];
echo $_POST['last_name'];
}
else
{
echo 'Data not comes here';
}
?>
Thought this might help your task.
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
Related Topics
How to Automatically Start a Download in PHP
Index.PHP Not Loading by Default
PHPmailer: Smtp Error: Could Not Connect to Smtp Host
PHP Fatal Error: Cannot Redeclare Class
Automatic Post-Registration User Authentication
Calling Non-Static Method With Double-Colon(::)
Get Content Between Two Strings PHP
Url Rewriting: Css, Js, and Images Not Loading
Remove Index.PHP from Url - Codeigniter 2
Pdo Parameterized Query - Reuse Named Placeholders
Simple Explanation PHP Oop VS Procedural
How to Measure the Speed of Code Written in PHP
Laravel: Error [Pdoexception]: Could Not Find Driver in Postgresql