Send mysql data through api with limit and update the mysql data with api status

 <?php
//ini_set('display_errors', 1);
//ini_set('display_startup_errors', 1);
//error_reporting(E_ALL);
include("dbconnect.php");
$cdate = date('Y-m-d');
$query_client_info = "select tbl_client_info_id, client_id, client_name,
client_mobile, client_email, client_dob from table1 where
flag_moengage=0 order by tbl_client_info_id limit 10";
$result_client_info = mysqli_query($conn, $query_client_info);
if(mysqli_num_rows($result_client_info)>0)
{
    $x=0;   $y=0;
    while($row_client_info=mysqli_fetch_array($result_client_info))
    {
//  $x++;
   
    $type = "customer";
    $customer_id = $row_client_info['client_id'];
    $name = $row_client_info['client_name'];
    $mobile = $row_client_info['client_mobile'];
    $email = $row_client_info['client_email'];
    $date_of_birth = $row_client_info['client_dob']."T00:00:00.000Z";
    $dob = $row_client_info['client_dob'];
   

        $postdata = array(
            "type" => $type,
            "customer_id" => $customer_id,
   
            "attributes" => array(
                "name" => $name,
                "mobile" => $mobile,
                "email" => $email,
                "date_of_birth" => $date_of_birth,
                "dob" => $dob
            ));
   
        // sent
       
   
    $curl = curl_init();
   
    curl_setopt_array($curl, array(
      CURLOPT_URL => 'api_url/app_id?app_id=',
    //  CURLOPT_URL => 'https://website.com/v1/customer/appid1223?app_id=appid1223',  
      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_ENCODING => '',
      CURLOPT_MAXREDIRS => 10,
      CURLOPT_TIMEOUT => 0,
      CURLOPT_FOLLOWLOCATION => true,
      CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
      CURLOPT_CUSTOMREQUEST => 'POST',
      CURLOPT_POSTFIELDS => json_encode($postdata),

      CURLOPT_HTTPHEADER => array(
        'Content-Type: application/json',
        'Authorization: Basic pankajasdasdUl4T1F3R1FRc051d0JVcVNSVw=='
      ),
    ));
    //print_r($curl);
    $response = curl_exec($curl);
   
    curl_close($curl);
    //echo "<br>".$response."<br>";
        // sent
   
    $obj = json_decode($response);
        if($obj->status=="success")
        {
            $x++;
            //echo "update";
            // update if sent
            $query_update_client_info = "update  table1 set flag_moengage = 1 where
            tbl_client_info_id = '$row_client_info[tbl_client_info_id]'";
            $result_update_client_info = mysqli_query($conn, $query_update_client_info);
            //echo $query_update_client_info."<br>";
            // update if sent
           
            // insert to log
            $query_log_client_info = "insert into table1_log (tbl_client_info_id, flag_moengage, status, cdate)
            values ('$row_client_info[tbl_client_info_id]', '1', '$obj->status', '$cdate')";
            $result_log_client_info = mysqli_query($conn, $query_log_client_info);
            //echo $query_log_client_info."<br>";
            // insert to log
            echo "Sent ".$x."<br>";
        }
        else
        {
            $y++;
            // insert to log
            $query_log_client_info = "insert into table1_log (tbl_client_info_id, flag_moengage, status, cdate)
            values ('$row_client_info[tbl_client_info_id]', '0', '$obj->status', '$cdate')";
            $result_log_client_info = mysqli_query($conn, $query_log_client_info);
            //echo $query_log_client_info."<br>";
            // insert to log
            echo "Not Sent ".$y."<br>";
        }  
    }
}
else
{
            // insert to log
            $query_log_client_info = "insert into table1_log (tbl_client_info_id, flag_moengage, status, cdate)
            values ('0', '0', 'no data found', '$cdate')";
            $result_log_client_info = mysqli_query($conn, $query_log_client_info);
            //echo $query_log_client_info."<br>";
            echo "No Data Found";
            // insert to log
}  
echo $obj->status;  

?> 


Explanation : - 


Here, I select the 10 number of data from mysql database (table1) and send it through api, 

then update the sent data status in mysql database (table1) and also insert the data sent status in table1_log.


Features :-  

If we have large number of data then we can set the cron job of this file and they will send data with limit of 10 every time when crone file is executed.

Comments