PHP Classes

File: public_html/test.php

Recommend this page to a friend!
  Classes of Ramesh Narayan Jangid(Sharma)   PHP Microservices Framework   public_html/test.php   Download  
File: public_html/test.php
Role: Auxiliary script
Content type: text/plain
Description: Configuration script
Class: PHP Microservices Framework
Setup microservices apps with configuration arrays
Author: By
Last change: Changes
Date: 14 hours ago
Size: 3,377 bytes
 

Contents

Class file image Download
<?php

function getCurlConfig($method, $route, $header = [], $json = '')
{
   
$homeURL = 'http://api.client001.localhost/Microservices/public_html/index.php';

   
$curlConfig[CURLOPT_URL] = "{$homeURL}?r={$route}";
   
$curlConfig[CURLOPT_HTTPHEADER] = $header;
   
$curlConfig[CURLOPT_HTTPHEADER][] = "X-API-Version: v1.0.0";
   
$curlConfig[CURLOPT_HTTPHEADER][] = "Cache-Control: no-cache";
   
    switch (
$method) {
        case
'GET':
            break;
        case
'POST':
           
$curlConfig[CURLOPT_HTTPHEADER][] = 'Content-Type: text/plain; charset=utf-8';
           
$curlConfig[CURLOPT_POST] = true;
           
$curlConfig[CURLOPT_POSTFIELDS] = $json;
            break;
        case
'PUT':
           
$curlConfig[CURLOPT_HTTPHEADER][] = 'Content-Type: text/plain; charset=utf-8';
           
$curlConfig[CURLOPT_CUSTOMREQUEST] = 'PUT';
           
$curlConfig[CURLOPT_POSTFIELDS] = $json;
            break;
        case
'PATCH':
           
$curlConfig[CURLOPT_HTTPHEADER][] = 'Content-Type: text/plain; charset=utf-8';
           
$curlConfig[CURLOPT_CUSTOMREQUEST] = 'PATCH';
           
$curlConfig[CURLOPT_POSTFIELDS] = $json;
            break;
        case
'DELETE':
           
$curlConfig[CURLOPT_HTTPHEADER][] = 'Content-Type: text/plain; charset=utf-8';
           
$curlConfig[CURLOPT_CUSTOMREQUEST] = 'DELETE';
           
$curlConfig[CURLOPT_POSTFIELDS] = $json;
            break;
    }
   
$curlConfig[CURLOPT_RETURNTRANSFER] = true;

    return
$curlConfig;
}

function
trigger($method, $route, $header = [], $json = '')
{
   
$curl = curl_init();
   
$curlConfig = getCurlConfig($method, $route, $header, $json);
   
curl_setopt_array($curl, $curlConfig);
   
$responseJSON = curl_exec($curl);
   
$err = curl_error($curl);
   
curl_close($curl);

    if (
$err) {
        echo
"cURL Error #:" . $err;
    } else {
       
$response = json_decode($responseJSON, true);
        if (!empty(
$response) && isset($response['Status']) && $response['Status'] == 200) {
            echo
'Sucess:'.$route . PHP_EOL . PHP_EOL;
        } else {
            echo
'Failed:'.$route . PHP_EOL;
            echo
'O/P:' . $responseJSON . PHP_EOL . PHP_EOL;
           
$response = false;
        }
    }
    return
$response;
}

$response = [];
echo
'<pre>';

$response[] = trigger('GET', '/reload', [], '');

$res = trigger('POST', '/login', [], '{"username":"client_1_group_1_user_1", "password":"shames11"}');
if (
$res) {
   
$response[] = $res;
   
$token = $res['Results']['Token'];
   
$header = ["Authorization: Bearer {$token}"];

   
$response[] = trigger('GET', '/routes', $header, '');
   
$response[] = trigger('POST', '/category-1', $header, '[{"name":"ramesh0","subname":"ramesh1","subsubname":"ramesh2"},{"name":"ramesh0","subname":"ramesh1","subsubname":"ramesh2"}]');
   
$response[] = trigger('GET', '/category-1', $header, '');
   
$response[] = trigger('POST', '/category', $header, '[{"name":"ramesh0","sub":{"subname":"ramesh1","subsub":[{"subsubname":"ramesh"},{"subsubname":"ramesh"}]}},{"name":"ramesh1","sub":{"subname":"ramesh1","subsub":{"subsubname":"ramesh"}}}]');
   
$response[] = trigger('GET', '/category&orderby={"id":"DESC"}', $header, '');
   
$response[] = trigger('GET', '/category&orderby={"id":"ASC"}', $header, '');
   
$response[] = trigger('POST', '/category/config', $header, '');
}

print_r($response);