Using PHP cURL with POST Data Tutorial

In this tutorial, we will tackle about how to Use PHP cURL with POST Data. This tutorial aims to give the IT/CS Students or those programmers that are new in PHP Language a reference or guide for using PHP cURL module with POST data. I will be providing a simple program that demonstrates our goal for this tutorial.

What is PHP cURL?

cURL stands for "Client URL's". It is one of the modules of of PHP. This module allows you to send and receive files over HTTP. It also allows you to pass data such as POST and GET Data. This will make various websites and domains connect simply.

Getting Started

First, we will need a virtual servers such as XAMPP/WAMPP to run our PHP Script.

Using XAMPP

Follow the following step before proceeding to the coding part of this tutorial.

  • Make sure that cURL Extension is already enabled in your php.ini file.
  • Open the XAMPP's Control Panel and start the Apache server.

Using PHP cURL with HTTP POST Data

The following scripts are an example of how to use PHP cURL with POST Data for your site.

Creating a Form

First we will be creating the form where you or users can encode data to submit. The script below is an HTML and PHP script for simple form for this tutorial. In my case, I save this file as index.php. The file also contains the script for sending POST Data and Receiving output from another URL using PHP cURL.

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <meta charset="UTF-8">
  4. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>PHP cURL POST</title>
  7. <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" integrity="sha384-gH2yIJqKdNHPEq0n4Mqa/HGKIhSkIHeL5AyhkYV8i59U5AR6csBvApHHNl/vI1Bx" crossorigin="anonymous">
  8. </head>
  9. <?php
  10.  
  11. if($_SERVER['REQUEST_METHOD'] == "POST"){
  12. $ch = curl_init();
  13.  
  14. curl_setopt($ch, CURLOPT_URL,"http://localhost/php_curl_post/post_api.php");
  15. curl_setopt($ch, CURLOPT_POST, 1);
  16. curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($_POST));
  17.  
  18. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  19. // curl_setopt($ch, CURLOPT_HEADER, true);
  20. // curl_setopt($ch, CURLOPT_NOBODY, true);
  21. $output = curl_exec($ch);
  22. $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  23.  
  24. curl_close($ch);
  25.  
  26. }
  27. ?>
  28. <h2 class="text-center">PHP cURL with POST Data</h2>
  29. <hr>
  30. <div class="card rounded-0 mx-auto col-lg-6 col-md-8 col-sm-12">
  31. <div class="card-header">
  32. <div class="card-title">Sample Form</div>
  33. </div>
  34. <div class="card-body">
  35. <div class="container-fluid">
  36. <form action="" method="POST" id="sample-form">
  37. <div class="mb-3">
  38. <label for="name" class="control-label">Full Name</label>
  39. <input type="text" class="form-control" id="name" name="name" required="required">
  40. </div>
  41. <div class="mb-3">
  42. <label for="contact" class="control-label">Contact #</label>
  43. <input type="text" class="form-control" id="contact" name="contact" required="required">
  44. </div>
  45. <div class="mb-3">
  46. <label for="address" class="control-label">Address</label>
  47. <textarea rows="3" class="form-control" id="address" name="address" required="required"></textarea>
  48. </div>
  49. </form>
  50. </div>
  51. </div>
  52. <div class="card-footer py-1">
  53. <div class="d-grid justify-content-center">
  54. <button class="btn btn-primary rounded-0" form="sample-form">Submit</button>
  55. </div>
  56. </div>
  57. </div>
  58. <div class="card rounded-0 mx-auto col-lg-6 col-md-8 col-sm-12 mt-3">
  59. <div class="card-header">
  60. <div class="card-title">cURL Response</div>
  61. </div>
  62. <div class="card-body">
  63. <?php if(isset($output)): ?>
  64. <div class="container-fluid">
  65. <p><b>HTTP Response Code:</b></p>
  66. <div class="bg-secondary bg-opacity-50 p-2">
  67. <?= $http_code ?>
  68. </div>
  69. <p><b>Output:</b></p>
  70. <div class="bg-secondary bg-opacity-50 p-2">
  71. <?= $output ?>
  72. </div>
  73. </div>
  74. <?php else: ?>
  75. <div class="text-center"><b>Submit Data using the Form above first.</b></div>
  76. <?php endif; ?>
  77. </div>
  78. </div>
  79.  
  80. </body>
  81. </html>

Creating a Sample Remote Site

Let's assume that script below is located on a different site. This script will check if there's a POST Data sent for the current request. Here, for demonstration purposes only, we will just output back the received POST data to the sender. There are a lot of things you can do on this side but this is not the purpose of the tutorial.

post_api.php

  1. <?php
  2. if($_SERVER['REQUEST_METHOD'] == "POST"){
  3. echo "<pre>";
  4. print_r($_POST);
  5. echo "</pre>";
  6. }
  7. ?>
  8.  

Sample Snapshot of the result of the snippets above.

Using PHP cURL with POST Data

Explaination

As you can see, the following script is written at the index.php file. This is the cURL script that allows the current site to submit or send HTTP POST data to another URL. First, you mus initialize the cURL Module then set some options needed such as the URL and the POST. The CURLOPT_URL option in the script indicates where to send the POST Data. The CURLOPT_POSTFIELDS option indicates for the data we want to send. The CURLOPT_RETURNTRANSFER indicates whether to capture the returned page content of the certaing request.

  1. $ch = curl_init();
  2.  
  3. curl_setopt($ch, CURLOPT_URL,"http://localhost/php_curl_post/post_api.php");
  4. curl_setopt($ch, CURLOPT_POST, 1);
  5. curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($_POST));
  6.  
  7. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  8.  
  9. $output = curl_exec($ch);
  10. $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  11.  
  12. curl_close($ch);
  13.  
DEMO VIDEO

There you go! You can try the sample PHP cURL Script on your end for submitting or sending HTTP POST Data into another URL. You can also download the provided source code zip file I created for this tutorial that demonstrates the purpose and goal of this tutorial. The download button is located below this article.

I hope this tutorial helps you with what you are looking for and you'll find this useful for your current and future PHP Projects. Explore more on this website for more Tutorials and Free Source Codes.

Happy Coding :)

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.
7 + 3 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.