Request data from another database using API call

  1. How do I create a function call or API call which should create a request with authorization and authentication to another database and retrieve data in JSON format? I’m using SLim V3. Along with this how do I connect two different databases to single API using Slim V3? Please do help me with this.

  2. In Slim v3 http://abcxyz/json?ab=(some dynamic value)3&cd=(some dynamic value)&ef=(some dynamic value)&gh=(some dynamic value)
    How is it possible for me to create a path like this can generate a response?

Hello @cool

  1. You could create a Pimple container entry for each database connection.

  2. In Slim 3 there is a pathFor method. The http_build_query function could also be helpful to create a query string.

hello @odan Thank you for your reply.

  1. it means we gotta create one more Pimple Container to use one another database?
    and what changes do we have to make in container.config.php?

2.Im unable to create call using this do you have any sample?

  1. I don’t know what your container.config.php looks like.

Pseudo example:

// First connection
$container['db1'] = function ($c) {
    return new PDO(...);
};

// Second connection
$container['db2'] = function ($c) {
   return new PDO(...);
};
  1. You need at least a named route.

Slim 3 example:

$app = new \Slim\App();

$app->get('/hello/{name}', function ($request, $response, array $args) {
    echo "Hello, " . $args['name'];

    return $response;
})->setName('hello');

Then use the routers pathFor method to build the url.

$router = $app->getContainer()->get('router');

$url = $router->pathFor('hello', [
    'name' => 'tom'
]);

// Generate URL-encoded query string
$queryData = [
    'foo' => 'bar'
];

// Append the query string
$url .= '?' . http_build_query($queryData);

// The result: hello/tom?foo=bar

Edit: Better migrate to Slim 4.

@odan
Thank you for your replay

  1. Im able to connect to database thanks allot for this.
  2. Sorry i was not clear with my second question
    Actually im able to get “/data/json?foo=foo&aaa=test” something like this as my output
    What i actually want is my url path should be like https://example.example.com/json?id=&apikey=********* and if i share this url path with someone they must be able to see data in json format with the id and apikey provided form our side.
    sorry if im taking your time but please can you help me with this?

In Slim 3 you can get the query parameters as an associative array on the Request object using getQueryParams() .

Example:

$params = $request->getQueryParams();

$id = $params['id'];
$apiKey = $params['apikey'];

// do something with it

More details

@odan
how should be my the route pattern?
if we use any special symbol in the route pattern /data/?id=*** it is showing me page not found message.
How should we covert [pathFor] (http://www.slimframework.com/docs/v3/start/upgrade.html#urlfor-is-now-pathfor-in-the-router) or replace our existing route patter with the new pathFor in address bar?
if im typing this in my address bar or postman https://example.example.com/data/aaa/foo
Im getting this as my response “/data/json?foo=foo&aaa=test”
Im unable to use “https://example.example.com/data/json?foo=foo&aaa=test” something like this in my address bar.

$app = new \Slim\App;
$app->get(’/data/{name}[/{foo}]’, function ($request, $response, $args) {
$query= [‘foo’ => ‘foo’,‘aaa’ => $args[‘name’]];
$url = $this->router->pathFor(‘data’,[‘name’ => “json”],$query);
$response->write($url);
})->setName(‘data’);
$app->run();

The second parameter of pathFor is for the route parameters only, and not for the query parameters.