Inside this article we will see the concept i.e How To Send cURL POST request with Parameters. This tutorial will give you the classified information about initiating cURL request in PHP using POST request.
cURL is a tool to transfer data from or to a server, using one of the supported protocols (HTTP, HTTPS, FTP, FTPS, GOPHER, DICT, TELNET, LDAP or FILE)
We can also work with JSON data to send with cURL as post request parameters. Click here to learn.
For consuming fake apis, we will this OWT fakeAPIs.
Learn More –
- PHP MySQLi File Upload with Progress Bar Using jQuery Ajax
- How To Send PHP cURL POST Request with JSON Parameters
- PHP MySQLi jQuery Ajax File Upload with Type Validation
- PHP MySQLi How To Use Select2 for Multiple Select Tutorial
Let’s get started.
Example: cURL POST Request
Create a file index.php inside your localhost directory. Open file and write this code into it.
<?php // API URL $url = 'https://api.onlinewebtutorblog.com/employees'; // POST Data $data = array( "username" => "sanjay_owt", "name" => "Sanjay Kumar", "email" => "sanjay@example.net", "gender" => "male", "designation" => "Web Developer", "phone_number" => "1234567890", "complete_address" => "Sample location" ); foreach ($data as $key => $value) { $fields_string .= $key . '=' . $value . '&'; } rtrim($fields_string, '&'); // Init cURL $ch = curl_init(); // Set the url, number of POST vars, POST data curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POST, count($data)); curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // Execute post $result = curl_exec($ch); // Close connection curl_close($ch); // Output Result echo "<pre>"; print_r($result);
Concept
API
// API URL
$url = 'https://api.onlinewebtutorblog.com/employees';
Add POST data
// POST Data
$data = array(
"username" => "sanjay_owt",
"name" => "Sanjay Kumar",
"email" => "sanjay@example.net",
"gender" => "male",
"designation" => "Web Developer",
"phone_number" => "1234567890",
"complete_address" => "Sample location"
);
Sending Array data
foreach ($data as $key => $value) {
$fields_string .= $key . '=' . $value . '&';
}
rtrim($fields_string, '&');
// Init cURL
$ch = curl_init();
// Set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, count($data));
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
Application Testing
Now,
URL: http://localhost/php-curl/index.php
We hope this article helped you to Learn How To Send PHP cURL POST Request with Parameters Tutorial in a very detailed way.
Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.
If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.