Append to all requests. Using Silex Provider

264 views
Skip to first unread message

Scott Robertson

unread,
Apr 5, 2013, 5:48:40 PM4/5/13
to guz...@googlegroups.com
Hello,

Is there a way to append the following to all requests, instead of having to add it each time i do a request:

?apikey=blah,

This can be either POST or GET, makes no difference.

I have tried request.params, but from what i read, its not meant for that.

Thanks
Scott
Message has been deleted

David Jellesma

unread,
Apr 9, 2013, 10:02:20 PM4/9/13
to guz...@googlegroups.com
I have the same question but have been unable to solve it. I pass the apiKey to the client constructor but do not see how to access it in the service description. Anyone have any thoughts on how to solve this?

Scott Robertson

unread,
Apr 10, 2013, 4:11:29 AM4/10/13
to guz...@googlegroups.com
The solution i found was to wrap requests in a method, and add the apikey to the params array in there.

Michael

unread,
Apr 10, 2013, 2:17:20 PM4/10/13
to guz...@googlegroups.com
I'd recommend using a service description with a sort of abstract command that you extend other commands from. If you don't want to use a service description, you could add an event listener to add these query string values to each request.

$client->getEventDispatcher()->addListener('client.create_request', function ($e) {
    $e['request']->getQuery()->set('apiKey', 'blah');
});

-Michael

Jarrett Croll

unread,
Jun 11, 2013, 7:26:18 PM6/11/13
to guz...@googlegroups.com
+1 What a fantastic answer mtdowling. Before I had an abstract command in my client's service description with the api hard coded in and was wondering if there was a way to dynamically insert the api key from my Symfony2 applications parameters.yml so I used your event dispatcher example. I insert the api key from the service container into my client's $config parameter as key "api-key" and then find that key again from the event object. Here is an example client class:

class ExampleClient extends Client
{
    public static function factory($config = array())
    {
        $default = array(
            'base_url' => "http://www.example.com",
        );
        $required = array('base_url', 'api-key');
        $config = Collection::fromConfig($config, $default, $required);

        $client = new self(
            $config->get('base_url'),
            $config
        );
        $client->setConfig($config);

        $client->setDescription(ServiceDescription::factory(__DIR__ . DIRECTORY_SEPARATOR . 'Command/commands.json'));

        $client->getEventDispatcher()->addListener('client.create_request', function ($e) {
            $e['request']->getQuery()->set('api-key', $e['client']->getConfig()->get('api-key'));
        });

        return $client;
    }
}

This will append a query parameter "api-key" to every request in the above client as long as you pass into the $config argument a key called "api-key" with a vlue of your choosing.

Dowling, Michael

unread,
Jun 11, 2013, 7:31:03 PM6/11/13
to guz...@googlegroups.com
With Guzzle 3.7, this actually got even easier with default request options:

// Provided a client, you can set a single default query string 
// parameter to add to each request
$client->setDefaultOption('query/api-key',  'test123');

// or you can specify an array of default query string parameters
$client->setDefaultOption('query',  array('api-key' => 'test123'));

You can find out more in the recently updated docs at http://guzzlephp.org

Hope this helps.

-Michael

--
You received this message because you are subscribed to the Google Groups "Guzzle - PHP HTTP client and REST client framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to guzzle+un...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Jarrett Croll

unread,
Jun 11, 2013, 8:29:37 PM6/11/13
to guz...@googlegroups.com
Awesome Michael thanks!


--
You received this message because you are subscribed to a topic in the Google Groups "Guzzle - PHP HTTP client and REST client framework" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/guzzle/Cz6uf4aZctA/unsubscribe?hl=en.
To unsubscribe from this group and all its topics, send an email to guzzle+un...@googlegroups.com.

For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
Jarrett Croll

Reply all
Reply to author
Forward
0 new messages