Skip to content

Instantly share code, notes, and snippets.

@khairulhasanmd
Last active December 17, 2016 04:02
Show Gist options
  • Save khairulhasanmd/ceccfa70ab8e192f86937a004c2734e1 to your computer and use it in GitHub Desktop.
Save khairulhasanmd/ceccfa70ab8e192f86937a004c2734e1 to your computer and use it in GitHub Desktop.
Rest API
//receiver
CREATE TABLE `sync` (
`name` varchar(100) NOT NULL,
`id` int(11) NOT NULL,
`address` varchar(100) NOT NULL,
`phone` varchar(100) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Dumping data for table `sync`
--
INSERT INTO `sync` (`name`, `id`, `address`, `phone`) VALUES
('aa', 1, 'bb', 'cc'),
('aa', 2, 'bb', 'cc');
<?php
$con=mysqli_connect("localhost","root","","test");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
if (isset($_POST['name'])){
if(mysqli_query($con,"INSERT INTO sync (id,name,address,phone)
VALUES ('".$_POST['id']."','".$_POST['name']."','".$_POST['address']."','".$_POST['phone']."')")){
$arrayName = array('result' => 'success');
echo json_encode($arrayName);
}else{
$arrayName = array('result' => 'failed');
echo json_encode($arrayName);
}
}else{
$result = mysqli_query($con,"SELECT MAX(id) AS id FROM sync");
$row = mysqli_fetch_array($result, MYSQLI_NUM);
// echo json_encode($row);
$arrayName = array('index' => $row['id']);
echo json_encode($arrayName);
}
mysqli_close($con);
// $arrayName = array('dad' => 2323,
// 'sda' => 43434 );
// print_r($arrayName);
// echo($_POST['name']);
// echo isset($_POST['name']);
?>
//------------------------------------------------------------------------------------------------------------------------------
sender
<?php
// set post fields
// $post['name'] = 'user1';
// $post['phone'] = 'userww1';
// $post['address'] = 'passuser1';
// $post['id'] = 5;
$ch = curl_init('http://192.168.103.114:8080/restapi/api.php');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
// execute!
// $response = curl_exec($ch);
// close the connection, release resources used
// curl_close($ch);
// do anything you want with your response
// echo '<pre>';
// print_r($response);
/**/
$con=mysqli_connect("localhost","root","","test");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
//get the last updated id from server
//send the new rows
$result = mysqli_query($con,"SELECT * FROM sync");
while($row = mysqli_fetch_array($result)){
//echo ''.$row['id'].' '.$row['name'].' '.$row['address'].' '.$row['phone'].' <br>';
$post['id'] = $row['id'];
$post['name'] = $row['name'];
$post['phone'] = $row['phone'];
$post['address'] = $row['address'];
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
// execute!
$response = curl_exec($ch);
// do anything you want with your response
echo '<pre>';
print_r($response);
}
mysqli_close($con);
// close the connection, release resources used
curl_close($ch);
?>
-- phpMyAdmin SQL Dump
-- version 2.11.6
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: Dec 15, 2016 at 12:44 PM
-- Server version: 5.0.51
-- PHP Version: 5.2.6
SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
--
-- Database: `test`
--
-- --------------------------------------------------------
--
-- Table structure for table `sync`
--
CREATE TABLE `sync` (
`name` varchar(100) NOT NULL,
`id` int(11) NOT NULL,
`address` varchar(100) NOT NULL,
`phone` varchar(100) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Dumping data for table `sync`
--
INSERT INTO `sync` (`name`, `id`, `address`, `phone`) VALUES
('aa', 0, 'bb', 'cc'),
('aa', 0, 'bb', 'cc');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment