Gowri
Gowri

Reputation: 16855

How to use third party api in symfony2

basically I am trying to integrate the thrid party api in symfony2 application. I have the php class from api documention. I want integrate that in my custom bundle controller.

I also looking to add the option to configure the api key and secrets. I don't know how to start. Please look at below that i want to do

Example:-

namespace Acme\DemoBundle\Controller;

class DemoController extends Controller
{
    public function indexAction()
    {

    $myApi = new MyApi($key,$secret); // this is the stuff, I am trying to do!

        return array();
    }
}

First I try to create the library format symfony package. I don't know how to achive this this type. Can Some one tell me what are thinks I need to do.

Upvotes: 1

Views: 1658

Answers (2)

Fabien MEYNARD
Fabien MEYNARD

Reputation: 597

TO do this in a proper way, you have to declare you api class as a service & use dependancy injection to inject your parameters :

parameters:
    your_api.class:      Acme\HelloBundle\Lib\YourApiClass
    your_api.key:        "key value for your api"
    your_api.token:      "token value for your api"

services:
   yourApiService:
       class:        "%your_api.class%"
       arguments:    ["%your_api.key%", "%your_api.token%"]

And in your controller & many other places you'll have access like that :

$api = $this->get('yourApiService');

Take a look for more informations : http://symfony.com/doc/current/book/service_container.html

Upvotes: 6

lsroudi
lsroudi

Reputation: 81

you can use it as a service: http://symfony.com/doc/current/book/service_container.html when i try to implement an extern api, i think about adapter pattern: http://en.wikipedia.org/wiki/Adapter_pattern

Upvotes: 0

Related Questions