RESTful API Usage Functions in PHP - Kvcodes
X

Congrats, You are Subscribed to Receive Updates.

RESTful API Usage Functions in PHP


Introduction

This part of article requires to read and use my previous article Creating A RESTful API With PHP and MySQL. Now, let’s extend the API class from the remote system. Let’s start RESTful API Usage Functions in PHP the system with login function. Every call and request directly affecting the site database. So we need authentication to provide access to the third-party API users. For that we have login System. I used simple login system in my API  Class. But your system may have different encryption standard. Please use the same login functions here in this API class by overriding it.

RESTful API Usage Functions in PHP

Here is the common API cURL with objects and url.

<?php $ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));
$response = curl_exec($ch); 
var_export($response); ?>

And for this $url is the parameter which carries our remote API url. And its Post Method. So we have to pass the $post value with parameters.

RESTful-API-Usage-Functions-in-PHP

Insert

Inserting data’s into the Remote Site Database directly.

<?php 

$post = [
    'email' => 'mail@yoursite.com',
    'pwd' => 'password',
    'table'    =>  'users',   
    'data' => array('email' => "summa@kccodes.com", 'password' => "password")
];

$url = 'http://yourwebsite.com/api/Insert';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));
$response = curl_exec($ch); 

var_export($response); ?>

This function will check the access first, Once the user gets access it will allow the user to perform actions.

Delete

Deleting the records from remote website with help of delete Call. Here is the example function of delete.

<?php $post = [
    'email' => 'mail@yoursite.com',
    'pwd' => 'password',
    'table'    =>    'users',   
    'conditions' => array('id' => 3)
];

$url = 'http://yoursite.com/api/Delete';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));
$response = curl_exec($ch); 

var_export($response); ?>

Like this every function requires access credentials like email and password table name and condition.

GetRow

Getting a row of record is different than other querying method,  So let’s do it separately,

<?php 
$post = [
    'email' => 'mail@yoursite.com',
    'pwd' => 'password',
    'table'    =>    'users',
    'conditions' => array('id' => 3)
];

$url = 'http://yoursite.com/api/GetRow'; 

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));
$response = curl_exec($ch);

var_export($response);?>

And Let’s see the Get all values from a table on the site.

GetAll

Getting all records of a table with conditions is also easy with this function.

<?php 
$post = [
    'email' => 'mail@yoursite.com',
    'pwd' => 'password',
    'table'    =>    'users',
    'conditions' => array('status' => 1)
];

$url = 'http://yoursite.com/api/GetRow'; 

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));
$response = curl_exec($ch);

var_export($response);?>

Finally, you can get single  value with below function.

GetSingleValue

Getting a single value from a table is easy with the below function.

<?php 
$post = [
    'email' => 'mail@yoursite.com',
    'pwd' => 'password',
    'table'    =>    'users',
    'column_single'    =>    'email',
    'conditions' => array('id' => 2)
];

$url = 'http://yoursite.com/api/GetSingleValue'; 

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));
$response = curl_exec($ch);

var_export($response);?>

And finally, we have to do the update records with below one.

Update

Updating the records is another important feature which can be done through it.

<?php 
$post = [
    'email' => 'mail@yoursite.com',
    'pwd' => 'password',
    'table'    =>    'ct_users',
    'data' => array('user_email' => 'newmail@yoursite.com', 'user_pwd' => 'newpass'),
    'primary_key' => array('id' => 18, 'user_email' => 'summa@kccodes.com', 'user_pwd' => 'password')
];

$url = 'http://yoursite.com/api/Update'; 

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));
$response = curl_exec($ch);

var_export($response);?>

Finally, This functions are not enough to go ahead with full things. So, Let’s create a querying feature with below function for all the custom querying features.

KvQuery

This function helps you handle all custom query calls.

<?php 
$post = [
    'email' => 'mail@yoursite.com',
    'pwd' => 'password',
    'table'    =>    'ct_users',
    'sql'  => "SELECT * FROM ct_users WHERE status='Active'"
];

$url = 'http://yoursite.com/api/KvQuery'; 

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));
$response = curl_exec($ch);

var_export($response);?>

This function helps to handle all your custom queries.

That’s it. If you have any doubts ask in next tab.  Also subscribe me to get more updates and also you can follow me through social sites to know more about my works.

commenter

About Varadharaj V

The founder of Kvcodes, Varadharaj V is an ERP Analyst and a Web developer specializing in WordPress(WP), WP Theme development, WP Plugin development, Frontaccounting(FA), Sales, Purchases, Inventory, Ledgers, Payroll & HRM, CRM, FA Core Customization, OpenCart Theme Development, PHP, HTML, CSS, jQuery, Bootstrap development and content SEO.

2 comments

  1. commenter

    Hello!

    Thanks for sharing the code. Do have the htaccess as well?

Comment Below

Your email address will not be published. Required fields are marked *

*

Current ye@r *

Menu

Sidebar

Subscribe E-mail Updates

Congrats, You are Subscribed to Receive Updates.

Advertisement

Category Posts