Learn How To Store Data In Post Method Using Curl In PHP

admin_img Posted By Bajarangi soft , Posted On 16-09-2020

JSON used to exchanging data between a browser and a server. The JSON data format interchange data through API and sending JSON data via POST request is the most required functionality. PHP cURL makes it easy to POST JSON data to URL.

posting-data-using-curl

First Need to Create HTML File
1. Add The HTML5 Doctype

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Curl POst data php</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js"></script>
</head>
<body>
</body>
</html>

2. Create a HTML File
index.php
<?php
if (isset($_POST['submit'])){
    $name = $_POST['name'];
    $phone = $_POST['phone'];
    $email = $_POST['email'];
    $password = $_POST['password'];

    $data = array('name' => $name,
        'phone' => $phone,
        'email' => $email,
        'password' => $password
        );

//    echo '<pre>';
//    print_r($data);
    $string = http_build_query($data);
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,"http://localhost/Curl/post_data/store.php");
    curl_setopt($ch, CURLOPT_POST,1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $string);
    $output = curl_exec($ch);
    curl_close($ch);
//    echo $output;

}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <title>Curl POst data php</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js"></script>
</head>
<body>
<div>
<!--    --><?php
//      if (isset($output)){
//          echo $output;
//      }
//    ?>
</div>
<div class="container mt-5">
    <div class="card bg-light">
        <form class="card-body" action="" method="post">
            <div class="form-group">
                <label class="h4 form-control-label" for="input1">Name</label>
                <input type="text" class="form-control" placeholder="Enter Name" name="name" id="name" required>
            </div>
            <div class="form-group">
                <label class="h4 form-control-label" for="input1">Phone</label>
                <input type="text" placeholder="123-456-7890"  class="form-control" name="phone" id="phone" required>
            </div>
            <div class="form-group">
                <label class="h4 form-control-label" for="input3">Email</label>
                <input type="text" placeholder="Enter Email" class="form-control" name="email" id="email" required>
            </div><div class="form-group">
                <label class="h4 form-control-label" for="input3">Password</label>
                <input type="password" placeholder="Enter password" class="form-control" name="password" id="password" required>
            </div>
            <div>
                <button type="submit" class="btn btn-secondary" name="submit" value="submit">Send Data</button>
            </div>
        </form>
    </div>
</div>
</body>
</html>


store.php
<?php

$con = mysqli_connect('localhost', 'root', '', 'curl');

if (isset($_POST['name'])){
    $name = $_POST['name'];
    $phone = $_POST['phone'];
    $email = $_POST['email'];
    $password = $_POST['password'];

    $sql = mysqli_query($con,"INSERT INTO `curl_post_data` SET `name`='$name', `phone`='$phone', `email`='$email', `password`='$password'")
    or die(mysqli_errno());
    if ($sql){
        echo "Data add successfully";
    }else{
        echo "No data added";
    }
//    echo $name;
}
?>

Database 

--
-- Database: `curl`
--

-- --------------------------------------------------------



--
-- Table structure for table `curl_post_data`

CREATE TABLE IF NOT EXISTS `curl_post_data` (
  `id` int(11) NOT NULL,
  `name` varchar(255) NOT NULL,
  `phone` varchar(255) NOT NULL,
  `email` varchar(255) NOT NULL,
  `password` text NOT NULL,
 

) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=latin1;

--
-- Indexes for table `curl_post_data`
--
ALTER TABLE `curl_post_data`
  ADD PRIMARY KEY (`id`);

--
-- AUTO_INCREMENT for dumped tables
--

--
-- AUTO_INCREMENT for table `curl_post_data`
--
ALTER TABLE `c`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=1;

Related Post