Simple API Generator Plugin

The Simple API Generator plugin is downloadable at wordpress repository.

The Simple API Generator plugin helps developer to quickly setup the API functions and generate API credentials for their users to use. The API credential generated includes an email address of the user and an API key. In order for user to call the API function, the minimum parameters that need to be passed is the API Email and API Key. Below is an example of the API call in PHP.

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => 'https://yourdomain.com/wp-json/simple_api/v1/call?apikey=<YOUR API KEY>&email=<YOUR API EMAIL>',
  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',
));

$response = curl_exec($curl);

curl_close($curl);
echo $response;

After receiving the $response, user can manipulate the data based on what is return by the API function.

So for example if the developer return a json such as the following:

{
"status":"success"
}

The user can retrieve the status by capturing the status key.

$getvariable = json_decode($response,true);
$getstatus = $getvariable['status'];

How to write the API function?

If you want to allow user to post more than just API Email and API Key, you can capture the value of other parameters by using $params[‘<the key>’]. So for example if you want user to submit a variable call ‘country’, you can capture it by the following code:

$country = $params['country'];

This also means that the sample code will need to be changed to the following:

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => 'https://yourdomain.com/wp-json/simple_api/v1/call?apikey=<YOUR API KEY>&email=<YOUR API EMAIL>&country=<ENTER COUNTRY NAME>',
  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',
));

$response = curl_exec($curl);

curl_close($curl);
echo $response;

This is the basic usage of the plugin.