# Request data from another database using API call

**URL:** <https://discourse.slimframework.com/t/request-data-from-another-database-using-api-call/3586>\
**Category:** Questions\
**Created:** [October 25, 2019, 10:03pm UTC](https://discourse.slimframework.com/t/request-data-from-another-database-using-api-call/3586 "2019-10-25T22:03:49Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![cool](https://avatars.discourse-cdn.com/v4/letter/c/c6cbf5/32.png) [@cool](https://discourse.slimframework.com/u/cool)\
**Post date:** [October 25, 2019, 10:03pm UTC](https://discourse.slimframework.com/t/request-data-from-another-database-using-api-call/3586/1 "2019-10-25T22:03:49Z")

</div>

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=](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?

---

<div class="post-metadata">

**Author:** ![odan](https://avatars.discourse-cdn.com/v4/letter/o/9de053/32.png) [@odan](https://discourse.slimframework.com/u/odan)\
**Post date:** [October 26, 2019, 7:38am UTC](https://discourse.slimframework.com/t/request-data-from-another-database-using-api-call/3586/2 "2019-10-26T07:38:38Z")

</div>

Hello @cool

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

2. In Slim 3 there is a [pathFor](http://www.slimframework.com/docs/v3/start/upgrade.html#urlfor-is-now-pathfor-in-the-router) method. The [http\_build\_query](https://www.php.net/manual/en/function.http-build-query.php) function could also be helpful to create a query string.

---

<div class="post-metadata">

**Author:** ![cool](https://avatars.discourse-cdn.com/v4/letter/c/c6cbf5/32.png) [@cool](https://discourse.slimframework.com/u/cool)\
**Post date:** [November 10, 2019, 11:16pm UTC](https://discourse.slimframework.com/t/request-data-from-another-database-using-api-call/3586/3 "2019-11-10T23:16:36Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![odan](https://avatars.discourse-cdn.com/v4/letter/o/9de053/32.png) [@odan](https://discourse.slimframework.com/u/odan)\
**Post date:** [November 11, 2019, 11:27am UTC](https://discourse.slimframework.com/t/request-data-from-another-database-using-api-call/3586/4 "2019-11-11T11:27:54Z")

</div>

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

Pseudo example:

```php
// 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](http://www.slimframework.com/docs/v3/objects/router.html#how-to-create-routes).

Slim 3 example:

```php
$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.

```php
$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.

---

<div class="post-metadata">

**Author:** ![cool](https://avatars.discourse-cdn.com/v4/letter/c/c6cbf5/32.png) [@cool](https://discourse.slimframework.com/u/cool)\
**Post date:** [November 11, 2019, 9:37pm UTC](https://discourse.slimframework.com/t/request-data-from-another-database-using-api-call/3586/5 "2019-11-11T21:37:31Z")

</div>

@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=](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?

---

<div class="post-metadata">

**Author:** ![odan](https://avatars.discourse-cdn.com/v4/letter/o/9de053/32.png) [@odan](https://discourse.slimframework.com/u/odan)\
**Post date:** [November 11, 2019, 10:41pm UTC](https://discourse.slimframework.com/t/request-data-from-another-database-using-api-call/3586/6 "2019-11-11T22:41:09Z")

</div>

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

Example:

```php
$params = $request->getQueryParams();

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

// do something with it

```

[More details](http://www.slimframework.com/docs/v3/objects/request.html)

---

<div class="post-metadata">

**Author:** ![cool](https://avatars.discourse-cdn.com/v4/letter/c/c6cbf5/32.png) [@cool](https://discourse.slimframework.com/u/cool)\
**Post date:** [November 11, 2019, 11:42pm UTC](https://discourse.slimframework.com/t/request-data-from-another-database-using-api-call/3586/8 "2019-11-11T23:42:19Z")

</div>

@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](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](http://www.slimframework.com/docs/v3/start/upgrade.html#urlfor-is-now-pathfor-in-the-router) in address bar?  
if im typing this in my address bar or postman [https://example.example.com/data/aaa/foo](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”](https://example.example.com/data/json?foo=foo&aaa=test%E2%80%9D) 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();

---

<div class="post-metadata">

**Author:** ![odan](https://avatars.discourse-cdn.com/v4/letter/o/9de053/32.png) [@odan](https://discourse.slimframework.com/u/odan)\
**Post date:** [November 12, 2019, 6:55am UTC](https://discourse.slimframework.com/t/request-data-from-another-database-using-api-call/3586/9 "2019-11-12T06:55:23Z")

</div>

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