RESTful API Usage Functions in PHP
- Article
- Comment (6)
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.
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.
Hello!
Thanks for sharing the code. Do have the htaccess as well?
I think you didn’t see your hidden file. It’s already there.
Great job! Only problem: I couldn’t find the KvQuery in the downloaded API.
Thanks
G.
It was available at the time of writing this article. But one reader suggested me, Allowing query to write there is not good. It has some vulnerability and advised me to remove it.
Why I get failed in your REST API response for insert function but data success insert into database ?
‘{“status”:”Failed”,”msg”:”Failed To Insert INSERT INTO kiriman (NORESI,IDSTATUS,IDUSER,IDCABANG,PENGIRIM,TANGGALKIRIM,WAKTUKIRIM,PENERIMA,BARANG,JUMLAH,BERAT,PACKING,CARAKIRIM,CABANGTUJUAN) VALUES (\’BBT0066\’,\’2\’,\’4\’,\’63\’,\’ARDHO\’,\’2018-11-24\’,\’17:28:53\’,\’AYIK\’,\’BIR\’,\’2\’,\’2\’,\’DUS\’,\’DIAMBIL\’,\’BLI\’)”}’
I use your script and the code is :
$post = [
‘username’ => ‘babycorp’,
‘password’ => ‘fuck99’,
‘table’ => ‘kiriman’,
‘data’ => array(‘NORESI’ => “BBT0066”, ‘IDSTATUS’ => “2”, ‘IDUSER’ => “4”, ‘IDCABANG’ => “63”, ‘PENGIRIM’ => “ARDHO”, ‘TANGGALKIRIM’ => date(“Y-m-d”), ‘WAKTUKIRIM’ => date(“H:i:s”), ‘PENERIMA’ => “AYIK”, ‘BARANG’ => “BIR”, ‘JUMLAH’ => “2”, ‘BERAT’ => “2”, ‘PACKING’ => “DUS”, ‘CARAKIRIM’ => “DIAMBIL”, ‘CABANGTUJUAN’ => “BLI” )
];
$url = ‘http://kibcepat.com/rest/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);
could you give me some solution for this ?
The MySQL query failed to work. So copy the query to your database and try it directly. It will through you the error. Just fix the MySQL query error